Dubbo使用xml方式配置provider与consumer总结

zookeeper安装配置及dubbo-admin安装配置自行百度;

springboot项目(使用注解方式可以自行百度);

  • 添加如下maven依赖

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>
    
  • 启动类添加如下注解并指定配置文件位置

    @ImportResource(locations = "classpath:spring/*.xml")
    
  • 在resources资源目录创建上一步指定的目录及provider.xml与consumer.xml

    resources(spring(provider.xml、consumer.xml))

  • 可以在xml中使用下面的标签引入yml配置文件,并使用${XX.XX}进行取值,方便进行配置管理

    <context:property-placeholder location="classpath:application.yml"/>
    
  • 当一个服务在提供服务的同时会去消费其他服务,这时provider.xml与consumer.xml中只需要配置一次zookeeper的地址及服务名称;

  • consumer.xml中配置的消费对象,在Java代码中需要有消费(使用@Autowired注入)才会被注册到zookeeper上;

  • consumer.xml中可以添加如下标签取消对消费对象的校验,否则在服务提供者未注册的情况下,消费者服务会启动失败;

    <dubbo:consumer check="false"/>
    
  • provider.xml配置如下

<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"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:application.yml"/>


    <!--应用名与注册中心地址写在consumer.xml中-->
    <!-- 1.提供方应用名-->
    <!--<dubbo:application name="demo-service-provider" />-->
    <!-- 2.指定注册中心地址 -->
    <!--<dubbo:registry address="zookeeper://49.232.230.89:2181"/>-->

    <!--3.提供方端口-->
    <dubbo:protocol name="dubbo" port="40880" payload="100000000"/>

    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
    <dubbo:service interface="com.wxm.demo.service.IHospitalService" ref="hospitalService" protocol="dubbo" />
    <!--具体实现该接口的 bean-->
    <bean id="hospitalService" class="com.wxm.demo.service.impl.HospitalServiceImpl"/>
</beans>

  • consumer.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:application.yml"/>

    <!-- 1.消费方应用名-->
    <dubbo:application name="demo-service" />

    <!-- 2.指定注册中心地址 -->
    <dubbo:registry address="zookeeper://49.232.230.89:2181"/>
    <!--3.不校验是否存在提供者,否则服务无法启动-->
    <dubbo:consumer check="false"/>

    <!--代码里须有@Autowired注入使用,消费者才会被注册到zookeeper-->
    <!-- 生成远程服务代理dubbo:reference:声明需要调用的远程服务的接口 -->
    <!-- 配置要消费的服务,超时时间等-->
    <dubbo:reference id="hospitalService" interface="com.wxm.demo.service.IHospitalService" timeout="30000"/>

</beans>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐