前言

dubbo是一个分布式的高性能Java RPC服务治理框架。
由于之前dubbo不更新没怎么用它。不过现在已经成为Apache的一员了,所以就不用担心这个问题了。

dubbo官网:http://dubbo.apache.org,还有中文版,不错。

模块划分:
这里将接口和用到的bean放在dubbo-interface,服务提供方dubbo-provider,服务消费方dubbo-consumer。

具体使用

1.项目结构

父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
<?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>

<groupId>com.donny</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-interface</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
</project>

2.dubbo-interface

这里定义一个UserService,并提供2个方法。

1
2
3
4
public interface UserService {
String sayHello(String name);
List<User> findAllUsers();
}

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
public class User implements Serializable{
private Long id;
private String name;
private int age;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}

PS:注意User要实现序列化接口。

3.dubbo-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
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
<?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>springboot-dubbo-demo</artifactId>
<groupId>com.donny</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-provider</artifactId>

<name>dubbo-provider</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>


<dependencies>
<dependency>
<groupId>com.donny</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

</project>

application.yml

1
2
3
4
5
6
7
8
9
10
server:
port: 8080

dubbo:
registry:
address: localhost:2181

logging:
level:
root: info

dubbo-provider.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
<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-provider" />

<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

<!-- 用dubbo协议在30001 -->
<dubbo:protocol name="dubbo" port="30001" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.donny.service.UserService" ref="userService"
version="1.0" registry="zookeeper"/>

<!-- 具体服务接口的实现 -->
<bean id="userService" class="com.donny.service.UserServiceImpl" />

</beans>

UserService实现

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
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello," + name;
}

@Override
public List<User> findAllUsers() {
List<User> users = new ArrayList<User>(3);
User user0 = new User();
user0.setId(1L);
user0.setName("张三");
user0.setAge(18);
users.add(user0);

User user1 = new User();
user1.setId(2L);
user1.setName("李四");
user1.setAge(28);
users.add(user1);

User user2 = new User();
user2.setId(3L);
user2.setName("王五");
user2.setAge(38);
users.add(user2);

return users;
}
}

Springboot主类

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@ImportResource("classpath:dubbo-provider.xml")
public class DubboProviderApp
{
public static void main( String[] args ) throws IOException {
SpringApplication.run(DubboProviderApp.class,args);
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}

4.dubbo-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
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
<?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>springboot-dubbo-demo</artifactId>
<groupId>com.donny</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-consumer</artifactId>

<name>dubbo-consumer</name>
<url>http://www.example.com</url>


<dependencies>
<dependency>
<groupId>com.donny</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

</project>

application.yml

1
2
3
4
5
6
server:
port: 8081

dubbo:
registry:
address: localhost:2181

dubbo-consumer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 消费方应用名 -->
<dubbo:application name="dubbo-consumer" />

<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

<!-- 引用UserService服务-->
<dubbo:reference id="userService" interface="com.donny.service.UserService"
check="false" version="1.0" url="" registry="zookeeper" protocol="dubbo" timeout="15000"/>

</beans>

Springboot主类

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@ImportResource("classpath:dubbo-consumer.xml")
public class DubboConsumerApp
{
public static void main( String[] args )
{
SpringApplication.run(DubboConsumerApp.class,args);
}
}

测试类

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
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DubboConsumerApp.class)
public class DubboConsumerTest
{
@Autowired
private UserService userService;

@Test
public void testSayHello() {
String result = userService.sayHello("jack");
System.out.println(result);
assertEquals(result,"Hello,jack");
}

@Test
public void testFindAllUers() {
List<User> users = this.userService.findAllUsers();
if (null != users && !users.isEmpty()) {
for (User user : users) {
System.out.println(user);
}
}

assertNotNull(users);
assertEquals(users.size(),3);
}
}

PS:这里注册中心使用的zookeeper。

5.使用dubbo-spring-boot-starter

springboot提供了非常多的starter,要用什么就使用spring-boot-starter-xxx即可。dubbo也提供了相应的starter。
dubbo-spring-boot-starter的github地址:https://github.com/alibaba/dubbo-spring-boot-starter。使用还是比较简单的,这里不再赘述。

dubbo-spring-boot-starter的使用也可以参考这篇:https://blog.csdn.net/romantic112/article/details/79687942

参考:https://blog.csdn.net/qq_27384769/article/details/79465781

代码地址:https://gitee.com/tommy88/springboot-dubbo-demo