dubbo是目前比较流行的RPC框架,很多互联网公司都在用,今天讲讲如何整合dubbo。
示例实现:dubbo基本配置、异步调用、服务降级、多注册中心、服务分组、多版本、结果缓存、优雅停机。

整体结构

imagepng
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;

源码地址

本文为博主原创文章,未经博主允许不得转载。
更多内容请访问:IT源点

注意:本文归作者所有,未经作者允许,不得转载