Spring Cloud Alibaba 与 Dubbo 的完美融合

Posted by 彭超 on 2020-02-13
Estimated Reading Time 13 Minutes
Words 2.4k In Total
Viewed Times

本文主要实践 Spring Cloud Alibaba 体系的服务注册与发现组件 NacosDubbo 的结合使用场景,实现对内 RPC 通信、对外 HTTP 通信的更高效率服务调用方式。

统一依赖版本管理

创建一个名为 hello-dubbo-nacos-dependencies 的依赖管理项目,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
79
80
<?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.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>0.2.1.RELEASE</spring-cloud-alibaba.version>

<dubbo.version>2.6.6</dubbo.version>
<dubbo-spring-boot.version>0.2.1.RELEASE</dubbo-spring-boot.version>
<dubbo-registry-nacos.version>0.0.1</dubbo-registry-nacos.version>
<dubbo-serialization-kryo.version>2.6.6</dubbo-serialization-kryo.version>
<alibaba-spring-context-support.version>1.0.2</alibaba-spring-context-support.version>
</properties>

<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo-registry-nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-serialization-kryo</artifactId>
<version>${dubbo-serialization-kryo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-spring-boot.version}</version>
</dependency>

<!-- Alibaba -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>${alibaba-spring-context-support.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

服务提供者

创建一个名为 hello-dubbo-nacos-provider 的服务提供者项目

POM

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"?>
<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>
<parent>
<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>hello-dubbo-nacos-provider-api</module>
<module>hello-dubbo-nacos-provider-service</module>
</modules>
</project>

该项目下有两个子模块,分别是 hello-dubbo-nacos-provider-apihello-dubbo-nacos-provider-service,前者用于定义接口,后者用于实现接口。

服务提供者接口模块

在服务提供者项目下创建一个名为 hello-dubbo-nacos-provider-api 的模块, 该项目模块只负责 定义接口

POM
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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>
<parent>
<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>hello-dubbo-nacos-provider-api</artifactId>
<packaging>jar</packaging>
</project>
定义一个接口
1
2
3
public interface EchoService {
String echo(String string);
}

服务提供者接口实现模块

创建名为 hello-dubbo-nacos-provider-service 服务提供者接口的实现模块,用于实现在接口模块中定义的接口。

POM
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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>
<parent>
<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>hello-dubbo-nacos-provider-service</artifactId>
<packaging>jar</packaging>
</project>
引入依赖

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
<!-- Nacos And Dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-kryo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

<!-- 依赖接口模块,用于实现接口 -->
<dependency>
<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-provider-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
相关配置

application.yml 中加入相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
spring:
application:
name: dubbo-nacos-provider
main:
allow-bean-definition-overriding: true
dubbo:
scan:
# 接口扫描路径
base-packages: com.antoniopeng.hello.dubbo.nacos.provider.service
protocol:
name: dubbo
# -1 代表自动分配端口
port: -1
# 配置高速序列化规则
serialization: kryo
registry:
# 服务注册地址,也就是 Nacos 的服务器地址
address: nacos://192.168.127.132:8848
provider:
# 配置负载均衡策略(轮询)
loadbalance: roundrobin

附:Duubo 负载均衡策略

  1. random:随机
  2. roundrobin:轮询
  3. leastactive:最少活跃数
  4. consistenthash:一致性 Hash
实现接口

通过 org.apache.dubbo.config.annotation 包下的 @Service 注解将接口暴露出去

1
2
3
4
5
6
7
8
9
10
11
import com.antoniopeng.hello.dubbo.nacos.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class EchoServiceImpl implements EchoService {

@Override
public String echo(String string) {
return "Echo Hello Dubbo " + string;
}
}

注意:@Service 注解要注明 version 属性值

验证是否成功

启动项目,通过浏览器访问Nacos Server 网址 http://192.168.127.132:8848/nacos ,会发现有一个服务已经注册在服务列表中。

服务消费者

创建一个名为 hello-dubbo-nacos-consumer 的服务消费者项目

POM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>
<parent>
<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-consumer</artifactId>
<packaging>jar</packaging>
</project>

引入依赖

同样在 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
<!-- Nacos And Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-kryo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

<!-- 依赖服务提供者接口模块,用于调用接口 -->
<dependency>
<groupId>com.antoniopeng</groupId>
<artifactId>hello-dubbo-nacos-provider-api</artifactId>
<version>${project.parent.version}</version>
</dependency>

相关配置

application.yml 中添加以下配置

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
spring:
application:
name: dubbo-nacos-consumer
main:
allow-bean-definition-overriding: true

dubbo:
scan:
# 配置 Controller 扫描路径
base-packages: com.antoniopeng.dubbo.nacos.consumer.controller
protocol:
name: dubbo
port: -1
registry:
address: nacos://192.168.127.132:8848

server:
port: 8080

# 服务监控检查
endpoints:
dubbo:
enabled: true
management:
health:
dubbo:
status:
defaults: memory
extras: threadpool
endpoints:
web:
exposure:
include: "*"

Controller

通过 org.apache.dubbo.config.annotation 包下的 @Reference 注解以 RPC 通信的方式调用服务,而对外提供以 HTTP 通信的方式的 Restful API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.antoniopeng.dubbo.nacos.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EchoController {

@Reference(version = "1.0.0")
private EchoService echoService;

@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return echoService.echo(string);
}
}

验证是否成功

通过浏览器访问 Nacos Server 网址 http:192.168.127.132:8848/nacos ,会发现又多了一个服务在服务列表中。

然后再访问服务消费者对外提供的 RESTful API http://localhost:8080/echo/hi,浏览器会响应以下内容:

1
Echo Hello Dubbo hi

到此,实现了 Nacos 与 Dubbo 的融合。


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !