Spring Cloud Alibaba 与 Dubbo 的完美融合

服务提供者

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

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.antoniopeng</groupId>
    <artifactId>hello-dubbo-nacos-provider</artifactId>
    <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
<?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>
定义一个接口
public interface EchoService {
    String echo(String string);
}

服务提供者接口实现模块

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

引入依赖

pom.xml 中主要添加以下依赖

<!-- 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 中加入相关配置

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 注解将接口暴露出去

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.xml 中添加以下主要依赖

<!-- 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 中添加以下配置

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

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,浏览器会响应以下内容:

Echo Hello Dubbo hi

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