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

摘要:
referenceinterface=“com.biz.XxxService”timeout=“5000”>参数);}}通过@ImportResource({“classpath://www.w3.org/2001/XMLSchema-instance " xmlns://www.springframework.org/schema/beans/spring-beans.xsdhttp:

约定优于配置是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   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   interface="com.biz.xxx.OrderService" />

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

    <dubbo:reference   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/

免责声明:文章转载自《springboot使用xml配置dubbo读取yml占位符》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Apache与php的安装vtk点云数据的显示下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

在Linux中samba server的配置

1、查看是否安装samba服务 # rpm –qa |grep samba 2、若没安装,则安 # yum install samba 执行4次此命令 3、查看安装的samba文件 #rpm–qa |grep samba 4、改动配置文件 # vim /etc/samba/smb.conf 注意: A: 做匿名訪问sambaserver——改动例如以下:...

Linux之iptables(五、firewall命令及配置)

firewalld服务 firewalld是CentOS 7.0新推出的管理netfilter的工具 firewalld是配置和监控防火墙规则的系统守护进程。可以实现iptables,ip6tables,ebtables的功能 firewalld服务由firewalld包提供 firewalld支持划分区域zone,每个zone可以设置独立的防火墙规则...

Laravel 5.2 三、中间件、视图与 Blade 模板引擎

一、中间件 Laravel 的 HTTP 中间件提供了对路由的一层过滤和保护。下面模拟一下用中间件验证后台登录。 1. 创建中间件 cmd 窗口进入项目目录,使用 artisan 命令创建 php artisan make:middleware AdminLoginVerify 这将在 app/Http/Middleware 目录创建中间件 AdminLo...

库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)

项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 PyTorch 其他项目 自然语言处理和语音处理 该部分项目涉及语音识别、多说话人语音处理、机器翻译、共指消解、情感分类、词嵌入/表征、语音生成、文本语音转...

【Maven】CentOS7使用Nexus3搭建maven私服

一、简介   Maven是一个采用纯Java编写的开源项目管理工具, Maven采用了一种被称之为Project Object Model(POM)概念来管理项目,所有的项目配置信息都被定义在一个叫做POM.xml的文件中, 通过该文件Maven可以管理项目的整个生命周期,包括清除、编译,测试,报告、打包、部署等等。目前Apache下绝大多数项目都已经采用...

EasyDSS中如何利用FFmpeg对MP4文件进行字幕叠加?

上一篇我们讲了在EasyDSS内可以通过FFmpeg对音视频进行裁剪,那么裁剪后我们又想进行字幕叠加时应该怎么做?其实这个需求也可以通过FFmpeg来满足,我们只需要调用对应的命令就可以了。 在具体教大家怎么叠加字幕之前,先给大家普及一下字幕文件的格式,字幕文件有很多种,常见的有 .srt,.ass 文件等。不同格式的字幕文件也可以相互转换,命令如下:...