SpringBoot给开发减少了很多的XML配置,现在用在Dubbo上.
SpringBoot版本: 2.2.2.RELEASE
DUBBO版本:2.6.2
DUBBO starter: 0.2.0
依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
配置
properties配置
dubbo.scan.basePackages=com.example.demo.dubbopublish
dubbo.application.id=com-example-admin
dubbo.application.name=com-example-admin-x
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.registry.id=zookeeper
dubbo.registry.address=zookeeper://domain:30002|zookeeper://domain:30002|zookeeper://domain:30002
服务暴露
import com.alibaba.dubbo.config.annotation.Service;
/**
* version:dubbo版本
* timeout:超时,会被Spring Context引用,不需要再写@Component或者Spring#@Service
*/
@Slf4j
@Service(version = "0.0.1", timeout = 2000)
public class QueryServiceImpl implements QueryService {
//xxxxxx 实现方法
}
服务引用
一处引用
import com.alibaba.dubbo.config.annotation.Reference;
....
@Reference(version = "1.0.0",timeout = 3000,init = true,lazy = true)
private TestService testService;
多处引用
设置服务引用
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Reference(version = "${dubbo.application.version}", timeout = 10_000, check = false)
public @interface DubboV1 {
}
用法
@Service
@Slf4j
public class MessageConsumerService implements EnvironmentAware, InitializingBean {
private final static String durationStart = "durationStart";
private final static String durationEnd = "durationEnd";
@DubboV1
private MessageService messageService;
}
总结
代码即注释的最佳使用方式,从根本上丢掉了XML,但是却变的更加易读和维护了.
======2021-08-06更新
今天在本地启动了一个dubbo服务用来看一下dubbo线程模型的源代码,然后发现死活不能注册dubbo,最后发现是我的Dubbo配置从application.properties挪到了另外一个依赖的resource目录去了,结果导致Dubbo识别不到配置了,不知道这个算不算一个Bug :)