SpringBoot给开发减少了很多的XML配置,现在用在Dubbo上.

SpringBoot版本: 2.2.2.RELEASE
DUBBO版本:2.6.2
DUBBO starter: 0.2.0

依赖

  1. <dependency>
  2. <groupId>com.alibaba.boot</groupId>
  3. <artifactId>dubbo-spring-boot-starter</artifactId>
  4. <version>0.2.0</version>
  5. </dependency>

配置

properties配置

  1. dubbo.scan.basePackages=com.example.demo.dubbopublish
  2. dubbo.application.id=com-example-admin
  3. dubbo.application.name=com-example-admin-x
  4. dubbo.protocol.id=dubbo
  5. dubbo.protocol.name=dubbo
  6. dubbo.registry.id=zookeeper
  7. dubbo.registry.address=zookeeper://domain:30002|zookeeper://domain:30002|zookeeper://domain:30002

服务暴露

  1. import com.alibaba.dubbo.config.annotation.Service;
  2. /**
  3. * version:dubbo版本
  4. * timeout:超时,会被Spring Context引用,不需要再写@Component或者Spring#@Service
  5. */
  6. @Slf4j
  7. @Service(version = "0.0.1", timeout = 2000)
  8. public class QueryServiceImpl implements QueryService {
  9. //xxxxxx 实现方法
  10. }

服务引用

一处引用

  1. import com.alibaba.dubbo.config.annotation.Reference;
  2. ....
  3. @Reference(version = "1.0.0",timeout = 3000,init = true,lazy = true)
  4. private TestService testService;

多处引用

设置服务引用

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target({ElementType.FIELD})
  4. @Reference(version = "${dubbo.application.version}", timeout = 10_000, check = false)
  5. public @interface DubboV1 {
  6. }

用法

  1. @Service
  2. @Slf4j
  3. public class MessageConsumerService implements EnvironmentAware, InitializingBean {
  4. private final static String durationStart = "durationStart";
  5. private final static String durationEnd = "durationEnd";
  6. @DubboV1
  7. private MessageService messageService;
  8. }

总结

代码即注释的最佳使用方式,从根本上丢掉了XML,但是却变的更加易读和维护了.

======2021-08-06更新
今天在本地启动了一个dubbo服务用来看一下dubbo线程模型的源代码,然后发现死活不能注册dubbo,最后发现是我的Dubbo配置从application.properties挪到了另外一个依赖的resource目录去了,结果导致Dubbo识别不到配置了,不知道这个算不算一个Bug :)