PancrasL的博客

dubbo 的原理和使用(完善中)

2021-04-14

1. 基础知识

1.1 什么是RPC

远程过程调用,简单的理解是一个节点请求另一个节点提供的服务

2. dubbo配置

2.1 安装ZooKeeper(Windows10环境)

1
2
3
4
5
6
7
8
9
10
11
12
13
# windows
$ cd bin
$ zkServer.cmd
...
some info
...

# linux
$ cd bin
$ ./zkServer.sh start
...
some info
...
  • zookeeper会占用8080端口,通过在 zoo.cfg 中添加 admin.serverPort=8888 解除8080端口的占用

2.2 安装监控中心(可选)

  • 克隆项目
1
2
3
$ git clonegit clone https://github.com/apache/dubbo-admin.git
$ cd dubbo-admin
$ mvn clean package
  • 前端
1
2
3
$ cd dubbo-admin-ui
npm install
npm run dev
  • 后端
1
2
3
4
$ cd dubbo-admin-server
$ mvn clean package
$ cd target
$ java -jar dubbo-admin-server-0.3.0-SNAPSHOT.jar

2.3 Spring Demo

2.3.1 Provider

  • pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>indi.pancras</groupId>
<artifactId>provider</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>

</project>
  • provider.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- 1. 指定当前服务/应用的名字(不要和其他服务同名) -->
<dubbo:application name="provider"/>

<!-- 2. 指定注册中心的位置 -->
<dubbo:registry address="zookeeper://localhost:2181"/>

<!-- 3. 指定通信规则(通信协议、通信端口) -->
<dubbo:protocol name="dubbo" port="31111"/>

<!-- 4. 暴露服务 -->
<dubbo:service interface="provider.interfaces.ComputationalStorageService" ref="css"/>
<bean id="css" class="provider.interfaces.impl.ComputationalStorageServiceImpl"/>
</beans>
  • MainApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

import provider.interfaces.ComputationalStorageService;

/**
* @author pancras
* @create 2021/5/19 17:26
*/
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
ComputationalStorageService bean = ioc.getBean(ComputationalStorageService.class);
System.out.println(bean);
System.in.read();
}
}
  • ComputationalStorageService.java
1
2
3
4
5
6
7
8
9
10
11
package provider.interfaces;

/**
* @author pancras
* @create 2021/5/19 17:26
*/
public interface ComputationalStorageService {
public String sayHello(String msg);

public Integer wordCount(String filename);
}
  • ComputationalStorageServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package provider.interfaces.impl;

import provider.interfaces.ComputationalStorageService;

/**
* 1. 将服务提供者注册到注册中心
* 1.1 导入dubbo依赖
* 1.2 配置服务提供者
* 2. 让服务消费者去注册中心订阅服务提供者的服务地址
*
* @author pancras
* @create 2021/5/19 17:26
*
*/
public class ComputationalStorageServiceImpl implements ComputationalStorageService {
public String sayHello(String msg) {
return "Hello: " + msg;
}

@Override
public Integer wordCount(String filename) {
return null;
}
}

2.3.2 Consumer

  • pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>indi.pancras</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>

</project>
  • consumer.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- 1. 指定当前服务/应用的名字(不要和其他服务同名) -->
<dubbo:application name="consumer"/>

<!-- 2. 指定注册中心的位置 -->
<dubbo:registry address="zookeeper://localhost:2181"/>

<dubbo:reference interface="provider.interfaces.ComputationalStorageService"
id="demoService" timeout="5000" retries="3" version="*">
<!-- <dubbo:method name="sayHello" timeout="1000"></dubbo:method>-->
</dubbo:reference>

</beans>
  • MainApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

import provider.interfaces.ComputationalStorageService;

/**
* @author pancras
* @create 2021/5/19 17:36
*/
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");

ComputationalStorageService bean = applicationContext.getBean(ComputationalStorageService.class);
String hello = bean.sayHello("Mike");
System.out.println(hello);
}
}

  • ComputationalStorageService.java
1
2
3
4
5
6
7
8
9
10
11
package provider.interfaces;

/**
* @author pancras
* @create 2021/5/19 17:26
*/
public interface ComputationalStorageService {
public String sayHello(String msg);

public Integer wordCount(String filename);
}