这是一个基于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
dubbo-client:生产者消费者共有接口
dubbo-consumer:消费者
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.11version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-discovery-spring-boot-starterartifactId>
<version>0.2.7version>
<exclusions>
<exclusion>
<groupId>com.alibaba.springgroupId>
<artifactId>spring-context-supportartifactId>
exclusion>
exclusions>
dependency>
<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.0modelVersion>
<packaging>pompackaging>
<modules>
<module>dubbo-providermodule>
<module>dubbo-clientmodule>
<module>dubbo-consumermodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.5version>
<relativePath/>
parent>
<groupId>xyz.hcworldgroupId>
<artifactId>rpcscaffoldartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>rpcscaffoldname>
<description>SpringBoot整合Apache Dubbo+Nacos的极简教程description>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.11version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-discovery-spring-boot-starterartifactId>
<version>0.2.7version>
<exclusions>
<exclusion>
<groupId>com.alibaba.springgroupId>
<artifactId>spring-context-supportartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
注释:需要去除spring-context-support依赖,否则运行不起来,但是去除后消费者会报错却不影响使用。
定义一个消费者和生产者共同拥有的接口子模块。
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);
}
依赖加入接口模块。
<dependencies>
<dependency>
<groupId>xyz.hcworldgroupId>
<artifactId>dubbo-clientartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
# 设置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);
}
}
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("张");
}
}
注释:报错是因为去除了com.alibaba.spring下的spring-context-support,但并不影响使用,不去除将无法启动,这是因为Dubbo不兼容太新的SpringBoot版本导致。
GitHub:https://github.com/z875479694h/RpcScaffold