Spring Boot中Dubbo的XML和YAML配置有何区别,导致启动失败?

Spring Boot中Dubbo的XML和YAML配置有何区别,导致启动失败?

spring Boot整合dubboxml与YAML配置对比及启动失败分析

本文分析spring boot项目中使用Dubbo时,XML配置与YAML配置导致不同启动结果的原因。YAML配置成功启动,而XML配置却失败,并提供解决方案。

问题:开发者分别使用YAML和XML两种方式配置Dubbo。YAML配置如下:

server:   port: 8083 dubbo:   application:     name: dubbo-provider   registry:     address: zookeeper://localhost:2181   protocol:     name: dubbo     port: -1

使用此YAML配置,Spring Boot应用正常启动。但使用以下XML配置:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">     <application name="dubbo-provider"/>     <registry address="zookeeper://127.0.0.1:2181"/>     <protocol name="dubbo" port="-1"/>     <service interface="cn.suiwei.service.TimeService" ref="timeServiceImpl"/>     <bean class="cn.suiwei.provider.service.TimeServiceImpl" id="timeServiceImpl"/> </beans>

应用启动失败,报错信息为:”no application config found or it’s not a valid config! please add to your spring config”。

原因分析:尽管XML配置文件包含,但Spring Boot并未自动加载该XML文件。

解决方案:使用@ImportResource注解手动导入XML配置文件。

在Spring Boot启动类或配置类中添加@ImportResource({“classpath:dubbo-provider.xml”})注解,即可解决问题。此注解指示spring容器加载classpath下的dubbo-provider.xml文件,从而正确读取XML中的Dubbo配置信息,成功启动Dubbo服务。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享