Dubbo和springboot2.x的相遇

我觉得dubbo在小型化微服务中非常方便, 今天就集成一个精简的demo, 帮助更好的理解和使使用dubbo

简要概念

Dubbo: 是一款高性能的Java RPC框架
zookeeper: 是一个分布式的,开放源码的分布式应用程序协调服务

第一步zookeeper安装

  • 单独写一篇zookeeper安装, 并启动起来

第二步整合dubbo

运行环境:JDK8,Maven 3.0+
技术栈:SpringBoot 2.0+、Dubbo 2.5+、ZooKeeper 3.3+

将服务生产方和调用发分为了两个项目, 分别是dubbo-springboot-provide和dubbo-springboot-consumer, 两个项目的api模块和项目的POM.XML是一样的

项目文件目录

项目的POM.XML文件

这里两个项目用的pom.xml是一样的, 引入两个东西, zookeeper和dubbo

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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.dubbo</groupId>
<artifactId>dubbo-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
</modules>

<name>dubbo-springboot</name>
<description>Dubbo springboot for Spring Boot</description>

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

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<curator-framework.version>4.0.1</curator-framework.version>
<zookeeper.version>3.4.13</zookeeper.version>
<dubbo.starter.version>0.2.0</dubbo.starter.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.starter.version}</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

api模块

这里只有两处需要编写, 一处是对外开放的接口HelloService, 另一处就是pom.xml了

HelloService内容如下

项目文件目录

api模块的POM.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"?>
<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.example.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>dubbo-api</name>
<description>Dubbo api for Spring Boot</description>

<parent>
<groupId>com.example.dubbo</groupId>
<artifactId>dubbo-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

</project>

provide服务生产者项目

  • 项目的端口是9091
  • provide的文件结构是这个样子
    生产者文件结构

1.HelloServiceImpl

需要为这个提供的服务提供版本(hello.service.version), 提供的服务是sayHello

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.api.service.HelloService;

/**
* 服务提供类
*/
@Service(version = "${hello.service.version}")
public class HelloServiceImpl implements HelloService {

@Override
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
}

2.生产者的启动类

使用@EnableDubbo来开启dubbo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.provider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* 服务提供启动类
*/
@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {

public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}

3. 模块的配置文件application.properties

内容包括: 设置项目名称、启动端口、dubbo上的名称、对外服务的版本、dubbo协议、注册中心地址、服务超时时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring.application.name = dubbo-provider
server.port = 9090

dubbo.application.name = dubbo-provider
# 对外服务的版本
hello.service.version = 1.0.0

dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

# zookeeper注册中心地址
dubbo.registry.address = zookeeper://localhost:2181

# 服务超时时间
dubbo.provider.timeout = 1000

4.服务生产者模块的POM.XML

里面值引入了api模块

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
<?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.xiaoze.provider</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>dubbo-provider</name>
<description>Dubbo provider for Spring Boot</description>

<parent>
<groupId>com.xiaoze.dubbo</groupId>
<artifactId>dubbo-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>com.xiaoze.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

consumer服务消费者项目

项目的端口是9091

api模块

和生产者项目的api模块一模一样, 上面已经做了说明, 所以这里不做说明了

concumer模块

项目结构是这个样子的
消费者模块文件结构

1. 消费者控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.api.service.HelloService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 消费者控制层
*/
@RestController
public class DemoConsumerController {

@Reference(version = "${hello.service.version}")
private HelloService helloService;

@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name) {
return helloService.sayHello(name);
}

}

2. 消费者启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* 消费者启动类
*/
@EnableDubbo
@SpringBootApplication
public class DubboConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}

3. 消费者application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 项目名称
spring.application.name = dubbo-consumer
# 项目端口
server.port = 9091

# dubbo项目名称
dubbo.application.name = dubbo-consumer
hello.service.version = 1.0.0

dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

#zookeeper地址
dubbo.registry.address = zookeeper://localhost:2181

dubbo.consumer.timeout = 5000

4. 消费者的POM.XML

这里只引入了api模块

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
<?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.xiaoze.consumer</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>dubbo-consumer</name>
<description>Dubbo consumer for Spring Boot</description>

<parent>
<groupId>com.example.dubbo</groupId>
<artifactId>dubbo-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>com.example.api</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

项目测试

  1. 启动zookeeper
  2. 启动生产者项目, 端口是9090
  3. 启动消费者项目, 端口是9091
  4. 访问消费者的接口 http://localhost:9091/sayHello/HelloWorld

项目的地址在这哦: https://gitee.com/minili/dubbo-demo.git

最后

拜拜啦, 希望大家能更快的入手dubbo!