张红尘的博客 张红尘的博客

纵你阅人何其多,始终无人恰似我

目录
后端 | SpringBoot整合Dubbo(基于Nacos2)
/    

后端 | SpringBoot整合Dubbo(基于Nacos2)

1.前言

这是一个基于SpringBoot整合Apache Dubbo+Nacos的极简教程,笔者使用到的技术及版本如下:

SpringBoot 2.4.5

Dubbo 2.7.11

Nacos 2.0.1(自行安装)

Dubbo官网:https://dubbo.apache.org/zh/

Nacos官网:https://nacos.io/zh-cn/docs/quick-start.html

2.目录结构

dubbo-client:生产者消费者共有接口

dubbo-consumer:消费者

dubbo-provider:生产者
sdn1.png

3.配置文件

pom核心依赖

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.11</version>
</dependency>
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>0.2.7</version>
    <exclusions>
       <exclusion>
           <groupId>com.alibaba.spring</groupId>
           <artifactId>spring-context-support</artifactId>
       </exclusion>
    </exclusions>
</dependency>

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-client</module>
        <module>dubbo-consumer</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> 
    </parent>
    <groupId>xyz.hcworld</groupId>
    <artifactId>rpcscaffold</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rpcscaffold</name>
    <description>SpringBoot整合Apache Dubbo+Nacos的极简教程</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.11</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.7</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.spring</groupId>
                    <artifactId>spring-context-support</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

注释:需要去除spring-context-support依赖,否则运行不起来,但是去除后消费者会报错却不影响使用。

4.接口模块

定义一个消费者和生产者共同拥有的接口子模块。

package xyz.hcworld.service;

/**
 * 注入接口
 * @ClassName: DemoService
 * @Author: 张红尘
 * @Date: 2021-05-24
 * @Version:1.0
 */
public interface DemoService {
    /**
     * 测试方法获取service参数
     * @param name
     * @return
     */
    String sayName(String name);
}

5.生产者模块

pom核心依赖

依赖加入接口模块。

<dependencies>
    <dependency>
         <groupId>xyz.hcworld</groupId>
         <artifactId>dubbo-client</artifactId>
         <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

yml配置文件

# 设置nacos的地址(配置中心)
nacos:
  discovery:
    server-addr: 192.168.2.142:8848
# 设置dubbo的参数
dubbo:
  application:
# 当前消费者的配置名
    name: dubbo-provider-demo
  registry:
    address: nacos://192.168.2.142:8848
# 协议(缺省值)
  protocol:
    name: dubbo
    port: -1

# 当前接口的名字与版本
demo:
  service:
    version: 1.0.0
    name: demoService

新版Dubbo将 @Service标注为过时,所以最新的注解应当使用 @DubboService ,如果使用 @Service容易与Spring的 @Service搞混。

package xyz.hcworld.service.impl;

import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Value;
import xyz.hcworld.service.DemoService;

/**
 * @ClassName: DemoServiceImpl
 * @Author: 张红尘
 * @Date: 2021-05-24
 * @Version:1.0
 */
@DubboService(interfaceClass = DemoService.class,interfaceName = "${demo.service.name}",version = "${demo.service.version}")
public class DemoServiceImpl  implements DemoService {

    @Value("${demo.service.name}")
    private String serviceName;

    @Override
    public String sayName(String name) {
        RpcContext rpcContext = RpcContext.getContext();
        return String.format("Service [name :%s , port : %d] %s(\"%s\") : Hello,%s",
                serviceName,
                rpcContext.getLocalPort(),
                rpcContext.getMethodName(),
                name,
                name);
    }
}

6.消费者模块

pom文件与同生产者模块一致,yml文件修改name其余也与生产者模块一致。

dubbo:
  application:
# 当前消费者的配置名
    name: dubbo-consumer-demo

生产者消费者的接口名与接口版本必须一致。使用@DubboReference注入远程接口。

package xyz.hcworld.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.hcworld.service.DemoService;

/**
 * @ClassName: DubboController
 * @Author: 张红尘
 * @Date: 2021-05-24
 * @Version:1.0
 */
@RestController
public class DubboController {

    @DubboReference(interfaceClass = DemoService.class,interfaceName = "${demo.service.name}",version = "${demo.service.version}")
    private DemoService demoService;

    @RequestMapping("/hello")
    public String sayHello(){
        return demoService.sayName("张");
    }
}

7.结果

生产者启动结果

sdn2.png

消费者启动结果

sdn3.png

注释:报错是因为去除了com.alibaba.spring下的spring-context-support,但并不影响使用,不去除将无法启动,如果有其他异常可以与作者进行讨论研究。

浏览器请求结果

sdn4.png

Nacos可视化

sdn5.png
sdn6.png
sdn7.png

8.项目下载地址

GitHub:https://github.com/z875479694h/RpcScaffold


标题:后端 | SpringBoot整合Dubbo(基于Nacos2)
作者:张红尘
地址:https://www.hcworld.xyz/articles/2021/07/08/1625679477411.html