spring-boot整合dubbo:Spring-boot-dubbo-starter
為什麼要寫這個小工具
如果你用過Spring-boot來提供dubbo服務,相信使用中有很多「不爽」的地方。既然使用spring boot,那麼能用註解的地方絕不用xml配置,這才是spring-boot-style。開個玩笑,真正意思是,spring-boot適合一些簡單的、獨立的服務,一個大的系統是不適合使用spring-boot來開發。相反,spring-boot適合那些簡單服務的搭建。 網上大多數的方法還是使用xml配置,通過@Import註解來引入xml配置。
怎麼使用
- 對於服務的消費者或提供者,使用dubbo內置的註解@Service或@Reference來聲明
- 在application.properties/yml 中配置dubbo的相關參數,例如下面就是一個簡單的消費者的配置
dubbo: application: name: lizo-consumer registry: address: zookeeper://localhost:12181
- 在配置類上使用@EnableDubbo(basePackages = "xxx.xxx.xxx") 來開啟dubbo的自動配置
經過以上3個步驟,就可以完成對dubbo的配置,是不是很Spring-bootstyle
dubbo-filter
dubbo有很多擴展,其中filter是使用比較多一個。但是用起來很不方便。如果能像Spring boot定義Spring MVC的filter那樣簡單聲明一個bean就好了。恩,其實就是這樣。
@Bean
ProviderFilter providerFilter {
return new ProviderFilter;
}
static class ProviderFilter extends AbstractDubboProviderFilterSupport {
public Result invoke(Invoker> invoker, Invocation invocation) {
System.out.println("ProviderFilter");
return invoker.invoke(invocation);
}
}
更定製化的需求,可以使用Dubbo的@Activate註解來定製化Filter,那麼可以這樣
@Bean
CustomFilter customFilter {
return new CustomFilter;
}
@Activate(group = Constants.PROVIDER)
static class CustomFilter extends AbstractDubboFilterSupport {
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException {
System.out.println("ProviderFilter2");
return invoker.invoke(invocation);
}
public Filter getDefaultExtension {
return this;
}
}
如果感興趣
源碼及其demo地址:
- 碼云:https://git.oschina.net/null_584_3382/spring-dubbo-parent
- github:https://github.com/Athlizo/spring-dubbo-parent
最後說明
- dubbo底層代碼是沒進行開發過的,只是在其基礎上進行的封裝。
- 不支持多註冊源
- 有bug或者有代碼優化建議歡迎反饋


TAG:科技優家 |
※druid-spring-boot-starter源碼解析
※深入 SpringBoot : 怎樣排查 expectedsinglematchingbeanbutfound 2 的異常
※深入 JVM 分析 spring-boot 應用 hibernate-validatorNoClassDefFoundError
※帶著新人學springboot的應用04(springboot+mybatis+redis 完)
※Spring Boot與Kotlin使用Spring-data-jpa簡化數據訪問層
※Mysql8.0主從搭建,shardingsphere+springboot+mybatis讀寫分離
※springboot:使用Spring Boot Actuator監控應用
※SpringBoot | 第九章:Mybatis-plus 的集成和使用
※Spring Boot 配置文件 yml與properties
※SpringBoot | 第三章:springboot 配置詳解
※flask 項目中使用 bootstrapFileInput
※SpringBoot伺服器壓測對比(jetty、tomcat、undertow)
※Springboot2.0 Theamleaf Security整合快速入門
※spring-boot 之 使員Druid 整合Mybatis 最簡配置多數據源
※Springboot2.X之切換使用Servlet容器Jetty、Tomcat、Undertow
※Just Don x Timberland 聯名 6-Inch Boot 系列即將上架
※Dropwizard與Spring Boot比較
※springboot配置druid連接池
※雷神911最新的6代機重裝系統提示no bootable device.systemstop修改bios教程
※SpringBoot 系列一 : SpringBoot 入門