dubbo的三种配置方式

一、使用zookeeper搭建注册中心

provider.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"
 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="dubbo_provider" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
 <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
 <dubbo:service interface="com.xxxx.Dubbo.service.DemoService" ref="demoService" />

    <!-- 接口实现类-->
 <bean id="demoService" class="com.xxxx.Dubbo.ServiceImpl.DemoServiceImpl"/>

</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"
 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="dubbo_consumer" />
 <!--使用zookeeper注册中心-->
 <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/>
 <!-- 生成远程服务代理,可以和本地bean一样使用demoService-->
<!-- <dubbo:reference id="demoService" interface="com.xxxx.Dubbo.service.DemoService" />-->
</beans>

二、没有注册中心,采用直连的方式

provider.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"
 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="dubbo_provider" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />-->
 <dubbo:registry protocol="zookeeper" address="N/A" file="./.dubbo-platform"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
 <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
 <dubbo:service interface="com.xxxx.Dubbo.service.xxxService" ref="xxxService" />

    <!-- 接口实现类-->
 <bean id="xxxService" class="com.xxxx.Dubbo.ServiceImpl.xxxServiceImpl"/>

</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"
 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="dubbo_consumer" />
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
 <!--使用zookeeper注册中心-->
 <!--<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />-->
 <dubbo:reference id="demoService" url="dubbo://127.0.0.1:20880" interface="com.xxxx.Dubbo.service.DemoService" check="true"/>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<!-- <dubbo:reference id="xxxService" interface="com.xxxx.Dubbo.service.xxxService" />-->
</beans>

注意端口要对应

三、通过广播的方式(没有实测)

<dubbo:registry address="multicast://xxx.5x.x.x:1234?unicast=false" />
发布了66 篇原创文章 · 获赞 230 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/lyztyycode/article/details/85241058