dubbo 是目前比较流行的 RPC 框架,很多互联网公司都在用,今天讲讲如何整合 dubbo。
示例实现:dubbo 基本配置、异步调用、服务降级、多注册中心、服务分组、多版本、结果缓存、优雅停机。
整体结构
provider:提供接口,供消费者调用
provider-impl:提供者接口实现
consumer:消费者服务
pom 配置
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
提供者配置
1、ProviderImplApplication 类里面加入
@EnableDubboConfiguration
来开启 dubbo。
2、接口实现类中加入 com.alibaba.dubbo.config.annotation.Service 注解
@Service(interfaceClass = UserService.class, registry = {"test"})
@Component
@Slf4j
public class UserServiceImpl implements UserService{
.....
}
3、application.properties 配置
server.port = 8081
dubbo.scan.basePackages = com.cimu.provider.service
spring.dubbo.application.name = cimu-provider
spring.dubbo.application.organization = cimu
spring.dubbo.application.owner = cimu
#配置单个注册中心
#spring.dubbo.registry.address = zookeeper://ip:2181
#配置多个注册中心
spring.dubbo.registries[0].id = test
spring.dubbo.registries[0].address = zookeeper://ip:2181
spring.dubbo.registries[1].id = test1
spring.dubbo.registries[1].address = zookeeper://ip:2182
spring.dubbo.protocol.port = 20880
spring.dubbo.protocol.name = dubbo
spring.dubbo.server = true
spring.dubbo.provider.retries = 0
#多版本
spring.dubbo.provider.version = 0.0.1
#服务分组
spring.dubbo.provider.group = cimu
spring.dubbo.provider.timeout = 6000
#设置优雅停机超时时间,缺省超时时间是 10 秒,如果超时则强制关闭。
dubbo.service.shutdown.wait=15000
消费者配置
1、ConsumerApplication 类中加入
@EnableDubboConfiguration
来开启 dubbo。
2、application.properties
server.port = 8080
dubbo.scan.basePackages = com.cimu.provider.service
spring.dubbo.application.name = cimu-consumer
spring.dubbo.application.organization = cimu
spring.dubbo.application.owner = cimu
##配置单个注册中心
#spring.dubbo.registry.address = zookeeper://ip:2181
#配置多个注册中心
spring.dubbo.registries[0].id = test
spring.dubbo.registries[0].address = zookeeper://ip:2181
spring.dubbo.registries[1].id = test1
spring.dubbo.registries[1].address = zookeeper://ip:2182
spring.dubbo.protocol.port=20881
spring.dubbo.protocol.name=dubbo
spring.dubbo.consumer.version = 0.0.1
spring.dubbo.consumer.group = cimu
spring.dubbo.consumer.retries = 0
spring.dubbo.consumer.timeout = 10000
3、使用
@Reference(check=true,registry = "test")
private UserService userService;
@Reference(mock="return 123",registry = "test")
private MockService mockService;
@Reference(registry = "test")
private AsyncService asyncService;
@Reference(registry = "test1" ,cache="lru")
private OtherRegistryService otherRegistryService;
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于