Redis 是一个高效的 KV 存储服务器。基于 Redis 实现的 RPC 协议。
2.3.0
以上版本支持。
缓存,限流,分布式锁等
从 Dubbo 3 开始,Redis 协议已经不再内嵌在 Dubbo 中,需要单独引入独立的模块。
org.apache.dubbo.extensions
dubbo-rpc-redis
1.0.0
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));
registry.register(URL.valueOf("redis://10.20.153.11/com.foo.BarService?category=providers&dynamic=false&application=foo&group=member&loadbalance=consistenthash"));
不需要感知 Redis 的地址
在客户端使用:
或者点对点直连:
也可以使用自定义接口:
其中 “p:xxx” 为 spring 的标准 p 标签
众所周知 dubbo推荐使用zookeeper做服务发现,但今天我们来使用另一种Redis做服务发现 这样省去了维护两种服务的经历 并且可以用作生产
POM
com.alibaba.spring.boot
dubbo-spring-boot-starter
2.0.0
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-lang3
3.7
spring:
application:
name: dubbo-producer
dubbo:
provider:
threadpool: fixed
threads: 2000
application:
id: server-producer
name: server-producer
qos-enable: true
qos-port: 22222
qos-accept-foreign-ip: false
registry:
# 使用redis 注册中心暴露服务地址
address: redis://127.0.0.1:6379
protocol:
name: dubbo
# 高效序列化 kryo, fst
#用dubbo协议在20880端口暴露服务
port: 20884
#性能监控中心地址
monitor:
protocol: dubbo
address: 127.0.0.1:7070
scan: cn.itcast.service
server:
port: 8888
public interface IDubboDemoService {
String helloDubbo();
}
import com.alibaba.dubbo.config.annotation.Service;
//@Service(version = "2.0.0")
@DubboService(version = "3.0.0", group = "dev", timeout = 5000)
public class IDubboDemoServiceImpl implements IDubboDemoService {
@Override
public String helloDubbo() {
return "hello dubbo, 我是提供者";
}
}
dubbo client服务消费者 application.yml
spring:
application:
name: dubbo-consumer
dubbo:
provider:
threadpool: fixed
threads: 2000
application:
# id: database-consumer
name: database-consumer
# qos-enable: true
# qos-port: 33333
# qos-acceptforeign-ip: false
registry:
#使用redis注册中心暴露服务地址
address: redis://127.0.0.1:6379
monitor:
# protocol: dubbo
address: 127.0.0.1:7070
server:
port: 9988
public interface IDubboDemoService {
String helloDubbo();
}
@Service
public class IDubboDemoServiceImpl implements IDubboDemoService {
@Override
//该内容随便写
public String helloDubbo() {
return "hello dubbo, I'm server!";
}
}
@Service 来自spring依赖
创建自己的接口
public interface IDemoService {
String test();
}
@Service
public class DemoServiceImpl implements IDemoService {
//调用dubbo服务
// @Reference(version = "2.0.0")
@DubboReference(version = "3.0.0")
public IDubboDemoService dubboDemoService;
@Override
public String test() {
return dubboDemoService.helloDubbo();
}
}
@SpringBootApplication
@EnableDubbo
@DubboComponentScan(basePackages = "cn.itcast.service")
public class DubboServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServiceApplication.class,args);
}
}
Spring Boot 注解默认只会扫描 main 类所在的 package,如果服务定义在其它 package 中,需要增加配置 EnableDubbo(scanBasePackages = {"org.apache.dubbo.springboot.demo.provider"})
伪代码
@Autowired(required = false)
private IDemoService demoService;
@GetMapper("/get")
demoService.test
即可返回“hello dubbo, 我是提供者”