• 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/

  • 相关阅读:
    关于xcode4.0调试出现“EXE_BAD_ACCESS”错误的处理
    UITableView 使用2
    mysql 命令行导入大的sql文件
    XCode4.0 内存监控方法
    如何在iphone app中加入google跟踪代码
    JQuery Mobile表单元素样式
    H.265编码视频播放器EasyPlayerProWIN版播放HLS协议视频流显示时间与实际不符如何修复?
    Visual Studio 2017自建WebRTC中peerconnection_client程序编译报错的解决方法
    【开发记录】TSINGSEE青犀视频开发H265播放器时销毁播放器仍不断重连地址如何修复
    TSINGSEE青犀视频云边端架构视频平台HLS视频流内m3u8文件和TS切片是什么关系?
  • 原文地址:https://www.cnblogs.com/cdfive2018/p/13695718.html
Copyright © 2020-2023  润新知