01 dubbo系列 | dubbo快速入门
前言
本文主要对dubbo做一个简介和一个入门实例。
dubbo是什么?
Dubbo是阿里巴巴开源的一个高性能的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。 Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
dubbo官网: http://dubbo.apache.org/zh-cn
git地址:https://github.com/apache/incubator-dubbo
谁在使用dubbo?
参考下图:
简单示例
环境
- jdk 1.8
- maven 3.3.9
- zookeeper 3.4.5:可以是单节点
该demo使用zookeeper作为注册中心,所以需要前提安装好zookeeper,并启动。
第一步:创建一个maven项目
创建一个maven项目,这里项目名为:dubbo-demo,并创建三个Module
- dubbo-demo-api: 这个模块用于定义接口
- dubbo-demo-provider: 服务生产者模块
- dubbo-demo-consumer: 服务消费者模块
其中dubbo-demo-provider和dubbo-demo-consumer都依赖于dubbo-demo-api模块。
第二步:配置dubbo-demo的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<packaging>pom</packaging>
<modules>
<module>dubbo-demo-api</module>
<module>dubbo-demo-provider</module>
<module>dubbo-demo-consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.joyxj</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<dubbo_version>2.7.0</dubbo_version>
<curator_version>4.0.1</curator_version>
<zookeeper_version>3.4.13</zookeeper_version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo_version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator_version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper_version}</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
其中关于spring boot的配置是可以不需要的,只者笔者当时建项目的时候使用了spring boot而已。
第三步:dubbo-demo-api配置
定义二个接口DemoService和UserService。
package com.joyxj.dubbo.demo.api;
/**
* 测试接口类
*
* @author xiaoj
* @version 1.0
* @date 2019-04-11
*/
public interface DemoService {
/**
* 测试方法
* @param name 名称
* @return String
*/
String sayHello(String name);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.joyxj.dubbo.demo.api;
/**
* 用户信息接口
*
* @author xiaoj
* @version 1.0
* @date 2019-04-11
*/
public interface UserService {
/**
* 获得年龄
* @param name
* @return
*/
String getAge(String name);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
这里定义了二个接口,主要是为了更好的看后面如何配置dubbo而已,定义一个接口也是可以的。其中一个接口就相当于dubbo的一个服务。
第四步:dubbo-demo-provider配置
- pom.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.joyxj</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.joyxj</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zoookeeper的操作工具 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
</dependencies>
</project>
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
35
36
37
38
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
35
36
37
38
- 实现dubbo-demo-api中的接口。
package com.joyxj.dubbo.demo.provider;
import com.joyxj.dubbo.demo.api.DemoService;
/**
* 【功能描述】
*
* @author xiaoj
* @version 1.0
* @date 2019-04-11
*/
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.joyxj.dubbo.demo.provider;
import com.joyxj.dubbo.demo.api.UserService;
import java.util.Random;
/**
* 【功能描述】
*
* @author xiaoj
* @version 1.0
* @date 2019-04-11
*/
public class UserServiceImpl implements UserService {
@Override
public String getAge(String name) {
int age = new Random().nextInt(100) + 1;
return name + " : " + age;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 服务的启动类
这里我们通过java代码配置dubbo的相关信息。实际生产中用xml文件或properties文件配置。
package com.joyxj.dubbo.demo.provider;
import com.joyxj.dubbo.demo.api.DemoService;
import com.joyxj.dubbo.demo.api.UserService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
/**
* 启动类
*
* @author xiaoj
* @version 1.0
* @date 2019-04-11
*/
public class ProviderApplication {
public static void main(String[] args) throws Exception{
// 应用配置
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-demo-provider");
// qos是dubbo的运维命令
applicationConfig.setQosPort(22222);
// 采用zookeeper作为注册中心
RegistryConfig registryConfig = new RegistryConfig("zookeeper://localhost:2181");
// DemoService的配置
ServiceConfig<DemoServiceImpl> serviceConfig = new ServiceConfig<>();
serviceConfig.setApplication(applicationConfig);
serviceConfig.setRegistry(registryConfig);
serviceConfig.setInterface(DemoService.class);
serviceConfig.setRef(new DemoServiceImpl());
serviceConfig.export();
// UserService的配置
ServiceConfig<UserService> userServiceServiceConfig = new ServiceConfig<>();
userServiceServiceConfig.setApplication(applicationConfig);
userServiceServiceConfig.setRegistry(registryConfig);
userServiceServiceConfig.setInterface(UserService.class);
userServiceServiceConfig.setRef(new UserServiceImpl());
userServiceServiceConfig.export();
System.in.read();
}
}
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
35
36
37
38
39
40
41
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
35
36
37
38
39
40
41
- 执行ProviderApplication的main方法。
第五步:dubbo-demo-consumer配置
- pom文件配置,与dubbo-demo-provider配置基本一样。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.joyxj</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.joyxj</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
</dependencies>
</project>
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
35
36
37
38
39
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
35
36
37
38
39
- 消费者启动类
package com.joyxj.dubbo.demo.consumer;
import com.joyxj.dubbo.demo.api.DemoService;
import com.joyxj.dubbo.demo.api.UserService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
/**
* 消费者
*
* @author xiaoj
* @version 1.0
* @date 2019-04-11
*/
public class ConsumerApplication {
public static void main(String[] args) {
// 应用配置
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-demo-consumer");
// qos是dubbo的运维命令,默认是22222,为了防止与生产者冲突,设置成33333
applicationConfig.setQosPort(33333);
// 注册中心配置
RegistryConfig registryConfig = new RegistryConfig("zookeeper://localhost:2181");
// DemoService的配置
ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(DemoService.class);
DemoService demoService = referenceConfig.get();
String msg = demoService.sayHello("xiaojun");
System.out.println(msg);
// UserService的配置
ReferenceConfig<UserService> userServiceReferenceConfig = new ReferenceConfig<>();
userServiceReferenceConfig.setApplication(applicationConfig);
userServiceReferenceConfig.setRegistry(registryConfig);
userServiceReferenceConfig.setInterface(UserService.class);
UserService userService = userServiceReferenceConfig.get();
System.out.println(userService.getAge("xiaojun"));
}
}
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
35
36
37
38
39
40
41
42
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
35
36
37
38
39
40
41
42
- 执行ConsumerApplication的main方法,正确输出。