使用dubbo的原因
- 服务治理框架
- 服务的监控
- 服务的注册发现
- 服务的通信
- 服务的容错
- 服务的负载均衡
配置dubbo提供方信息
#dubbo 服务扫描基础包路径
dubbo.scan.base-packages=com.dubbo.provider
dubbo.protocol.id=dubbo
# Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端
口,从 20880 开始)
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
spring.cloud.nacos.discovery.server-addr=192.168.1.16:8848
Dubbo多注册中心的配置
dubbo.registries.shanghai.address=zookeeper://192.168.1.16:2181
dubbo.registries.shanghai.timeout=10000
dubbo.registries.shanghai.zone=shanghai
dubbo.registries.shanghai.weight=100
dubbo.registries.hunan.address=nacos://192.168.1.16:8848
dubbo.registries.hunan.weight=10
dubbo.registries.hunan.preferred=true
代码调用
@DubboService(registry = {"shanghai","hunan"},version = "2.0")
public class SayHelloServiceImpl2 implements ISayHelloService {
@Override
public String sayHello(String msg) {
return "[Version2.0]Hello,"+msg;
}
}
@RestController
public class TestController{
@DubboReference(registry = {"shanghai","hunan"},version = "2.0")
ISayHelloService sayHelloService;
@GetMapping("/say")
public String say(){
return sayHelloService.sayHello("World");
}
}
dubbo集成SpringCloud
# 使用dubbo来管理
dubbo.scan.base-packages=com.springcloud.dubbo.provider.services
dubbo.protocol.port=20882
dubbo.protocol.name=dubbo
spring.cloud.nacos.discovery.server-addr=192.168.1.16:8848
nacos集成springcloud
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>