springboot使用xml配置dubbo讀取yml占位符


約定優於配置是springboot簡化配置的思路,其中它提供的自動配置、基於注解配置為我們搭建項目框架帶來了很大的便利。
使用springboot的項目跟僅使用spring的項目相比,少了很多xml配置文件,基於自動配置或者使用注解和配置類就可完成大多數配置。

springboot + dubbo搭建微服務工程:(springboot版本2.0.4.RELEASE,dubbo版本2.6.2)
dubbo在com.alibaba.dubbo.config.annotation包下也提供了@Service@Reference兩個注解來配置服務提供和消費接口,
com.alibaba.dubbo.config包下提供了應用(ApplicationConfig)、協議(RegistryConfig)、注冊RegistryConfig、提供者(ProviderConfig)、消費者(ConsumerConfig)等各種配置類。

@Service@Reference僅支持配置到類上,有時我們想細粒度對接口的某個方法進行配置,這個時候就需要用到dubbo的xml配置。
例如:

<dubbo:reference id="xxxService" interface="com.biz.XxxService"timeout="5000">
    <dubbo:method name="" retries="0" async="true" return="false" />
</dubbo:reference>

XxxService接口的yyy()方法配置為不重試、異步調用、無返回。

在項目springboot啟動類里通常是:

@Slf4j
@SpringBootApplication
@ImportResource({"classpath:/dubbo-xxx.xml"})
public class XxxApplication {
    public static void main(String[] args) {
        SpringApplication.run(XxxApplication.class, args);
        log.info("XxxApplication started!");
    }
}

通過@ImportResource({"classpath:/dubbo-xxx.xml"})引入dubbo的配置文件。
注1:import的也可能是spring的context.xml配置文件,里面再import引入dubbo的配置文件);
注2:這里通過xml配置dubbo服務,啟動類沒有配置@EnableDubbo(scanBasePackages = "com.biz.xxx")注解。

然后在dubbo-xxx.xml中進行dubbo服務的配置:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="xxxService" />

    <dubbo:registry  address="${dubbo.registryAddress}" />

    <dubbo:protocol name="dubbo" port="20001" threadpool="fixed" threads="500" />

    <dubbo:provider delay="-1" retries="0" />

    <dubbo:consumer retries="0" check="false" timeout="3000" />

    <dubbo:service id="orderService" interface="com.biz.xxx.OrderService" />

    <dubbo:reference id="proudctService" interface="com.biz.xxx.ProductService" />

    <dubbo:reference id="logService" interface="com.biz.xxx.LogService" lazy="true" timeout="5000">
        <dubbo:method name="logBiz" async="true" return="false" />
    </dubbo:reference>
</beans>

注:項目中可能區分dubbo的application、provdier、consumer由多個文件配置,這里簡化配置在1個文件中。

通常項目環境有開發、測試、生產環境,一些配置參數不同環境可能是不同的,比如dubbo注冊中心的地址。
我們可以通過springboot的不同profile的yml進行配置。
如:
開發環境用application-dev.yml:

dubbo:
  registryAddress: zookeeper://192.168.0.1:2181:

測試環境用application-test.yml:

dubbo:
  registryAddress: zookeeper://192.168.5.1:2181:

然后在application.yml指定profile,啟動項目應用該配置。

spring:
  profiles:
    active: dev

最近在搭建一個項目遇到問題是yml寫了配置,在dubbo-xxx.xml中的占位符配置沒有生效。
啟動報如下警告,能啟動成功dubbo接口正常:

|WARN|2020-09-17 14:25:21.887|restartedMain|o.s.b.f.s.DefaultListableBeanFactory:1530--Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.alibaba.dubbo.config.ConsumerConfig': Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'timeout'; nested exception is java.lang.NumberFormatException: For input string: "${dubbo.consumerTimeout}"...

|INFO|2020-09-17 14:25:22.193|restartedMain|o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker:326--Bean 'com.alibaba.dubbo.config.ConsumerConfig' of type [com.alibaba.dubbo.config.ConsumerConfig] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)..

ReferenceConfig(null) Is Not DESTROYED When FINALIZE...

經過查詢資料、用不同工程反復測試調試,發現跟項目的依賴有關。
比如:
依賴了springcloud,啟動類使用@SpringCloudApplication而非@SpringBootApplication
依賴了spring-boot-devtools

dubbo-xml里配置了<dubbo:reference>才會出現。
被這個WARN困擾,查資料本地調試了很久,在此記錄一下。猜測可能跟spring bean和dubbo服務配置的加載順序有關,待以后對dubbo進行深入研究。

參考資料:
springboot yml屬性值如何填充到xml文件里面 https://www.oschina.net/question/136863_2283870
springboot yml屬性值如何填充到xml文件里面 https://segmentfault.com/q/1010000015773877/a-1020000015774973
Springboot 項目中 xml文件讀取yml 配置文件 https://www.cnblogs.com/lykbk/p/sdfsdfsdfs435456454.html
xml配置文件獲取application.yml配置文件的內容
springboot整合dubbo 0.2.0,其中使用了占位符不生效問題 ${} https://github.com/apache/dubbo-spring-boot-project/issues/544
SpringBoot Profile使用詳解及配置源碼解析 https://zhuanlan.zhihu.com/p/98991664
springboot xml文件讀取yml文件配置信息 https://blog.csdn.net/weixin_30918415/article/details/101656991
Springboot 項目中 xml文件讀取yml 配置文件
dubbo 如何進行不同環境配置?https://cloud.tencent.com/developer/ask/213501
SpringBoot2.x基礎篇:配置文件中占位符的使用https://my.oschina.net/yuqiyu/blog/3209051
spring中xml解析屬性占位符 https://blog.csdn.net/qq_41067397/article/details/104614883
[DUBBO] ReferenceConfig(null) Is Not DESTROYED When FINALIZE分析及解決 https://jaskey.github.io/blog/2020/05/22/dubbo-refernececonfig-is-not-destroyed-when-finalize/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM