Nacos与Dubbo整合
在 SpringCloudAlibaba 体系中 dubbo可以作为
服务调用
&负载均衡
整合其实很简单 将dubbo注册中心换成Nacos即可
demo项目为聚合项目,只贴出部分代码完整demo请参考:https://duangouyu.coding.net/p/demo/d/SCA/git/tree/NacosDubboDemo
依赖
注意nacos与dubbo版本关系 dubbo版本太高可能会出问题
<!-- 公共依赖 -->
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 端点监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Nacos注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Nacos配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- base service -->
<dependency>
<groupId>top.mengshuo</groupId>
<artifactId>base-service</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置
服务消费者
spring:
# 启用bean覆盖
main:
allow-bean-definition-overriding: true
application:
name: consumer-service
profiles:
active: dev
bootstrap-dev.yml
spring:
cloud:
nacos:
discovery:
# 注册中心地址
server-addr: 192.168.59.102:8848
config:
# 配置中心地址
server-addr: 192.168.59.102:8848
# nacos config dataId 的后缀,也是配置内容的文件扩展名
file-extension: yaml
# 端点监控
management:
endpoints:
web:
exposure:
include: "*"
server:
port: 8200
dubbo:
application:
name: ${spring.application.name}
registry:
address: spring-cloud://192.168.59.102:8848
timeout: 10000
simplified: true
protocol:
port: -1
name: dubbo
服务提供者
bootstrap.yml
spring:
# 启用bean覆盖
main:
allow-bean-definition-overriding: true
application:
name: provider-service
profiles:
active: dev
bootstrap-dev.yml
spring:
cloud:
nacos:
discovery:
# 注册中心地址
server-addr: 192.168.59.102:8848
config:
# 配置中心地址
server-addr: 192.168.59.102:8848
# nacos config dataId 的后缀,也是配置内容的文件扩展名
file-extension: yaml
# 端点监控
management:
endpoints:
web:
exposure:
include: "*"
dubbo:
application:
name: ${spring.application.name}
registry:
address: spring-cloud://192.168.59.102:8848
timeout: 10000
protocol:
port: -1
name: dubbo
scan:
base-packages: top.mengshuo.service.impl
spring:
cloud:
nacos:
discovery:
# 注册中心地址
server-addr: 192.168.59.102:8848
config:
# 配置中心地址
server-addr: 192.168.59.102:8848
# nacos config dataId 的后缀,也是配置内容的文件扩展名
file-extension: yaml
# 端点监控
management:
endpoints:
web:
exposure:
include: "*"
dubbo:
application:
name: ${spring.application.name}
registry:
address: nacos://192.168.59.102:8848
timeout: 10000
protocol:
port: -1
name: dubbo
公共接口
package top.mengshuo.service;
/**
* @author mengshuo
* @since 2021-11-17
*/
public interface ProviderService {
/**
* hello
*
* @param name name
* @return res
*/
String sayHello(String name);
}
服务消费者
package top.mengshuo.controller;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mengshuo.service.ProviderService;
/**
* @author mengshuo
* @since 2021-11-17
*/
@RestController
@RequestMapping("consumer")
public class ConsumerController {
@DubboReference
private ProviderService providerService;
@GetMapping("/hello")
public String hello(String name){
return this.providerService.sayHello(name);
}
}
服务提供者
服务提供者添加启动参数 用于启动多个实例 ,参数自定义即可
--sys.desc=provider2 --server.port=8102
package top.mengshuo.service.impl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import top.mengshuo.service.ProviderService;
/**
* @author mengshuo
* @since 2021-11-17
*/
@DubboService
public class ProviderServiceImpl implements ProviderService {
@Value("${sys.desc}")
private String desc;
/**
* hello
*
* @param name name
* @return res
*/
@Override
public String sayHello(String name) {
return "hello:" + name + " >>> " + desc;
}
}
开启dubbo&nacos
启动类添加注解 启用 dubbo & nacos 注册中心
@EnableDubbo @EnableDiscoveryClient