您的位置:首页 > 其它

dubbo 使用学习六(多协议,多注册中心配置)

2016-10-28 00:50 591 查看
一、不同服务在性能上适用不同协议进行传输,大数据使用短连接,小数据大并发使用长链接协议

1、服务提供者配置

<?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 ">
<!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
<dubbo:application name="hello-world-app-my" />
<dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 用rmi协议在20881端口暴露服务 -->
<dubbo:protocol name="rmi" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service  interface="com.test.dubboser.ServiceDemo"
ref="demoService" protocol="dubbo" />
<dubbo:service  interface="com.test.dubboser.ServiceDemo2"
ref="demoService2" protocol="rmi" />
<!--和本地bean一样实现服务 -->
<bean id="demoService" class="com.test.dubboser.ServiceImp"/>
<bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>

</beans>


从这些配置我们看到,不同服务使用不同的协议,这种配置方式和其他的配置方式是一样的哦 哈哈……

2、服务消费者配置

<?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="consumer-of-helloworld-app-my" />
<!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
<dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2181" />
<dubbo:reference id="demoServicemy"    interface="com.test.dubboser.ServiceDemo"/>
<dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2"/>
</beans>


消费端还是按照正常方式获取服务,只是传递使用的协议不同,底层tcp链接时常不同……

二、同一服务多协议配置

1、服务提供者配置

<?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 ">
<!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
<dubbo:application name="hello-world-app-my" />
<dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service  interface="com.test.dubboser.ServiceDemo"
ref="demoService" protocol="dubbo,rmi" />
<!--和本地bean一样实现服务 -->
<bean id="demoService" class="com.test.dubboser.ServiceImp"/>

</beans>


2、服务消费者配置

<?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="consumer-of-helloworld-app-my" />
<!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
<dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2181" />
<dubbo:reference id="demoServicemy3"    interface="com.test.dubboser.ServiceDemo"/>
</beans>


三、不同服务注册到不同的注册中心

1、服务提供者配置

<?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 ">
<!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
<dubbo:application name="hello-world-app-my" />
<!-- 多注册中心配置 -->
<dubbo:registry  id="bjregister" protocol="zookeeper"  address="192.168.0.101:2181"/>
<dubbo:registry  id="shregister" protocol="zookeeper"  address="192.168.0.101:2182"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<!--不同服务注册到不同的注册中心  -->
<dubbo:service  interface="com.test.dubboser.ServiceDemo"
ref="demoService"  registry="bjregister" />
<dubbo:service  interface="com.test.dubboser.ServiceDemo2"
ref="demoService2" registry="shregister"/>
<!--和本地bean一样实现服务 -->
<bean id="demoService" class="com.test.dubboser.ServiceImp"/>
<bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>

</beans>


2、服务消费者配置文件

<?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="consumer-of-helloworld-app-my" />
<!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
<!--向两个注册中心订阅  -->
<dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2181"/>
<dubbo:registry  protocol="zookeeper"  address="192.168.0.101:2182"/>
<!--获取服务  -->
<dubbo:reference id="demoServicemy"    interface="com.test.dubboser.ServiceDemo"/>
<dubbo:reference id="demoServicemy2"   interface="com.test.dubboser.ServiceDemo2"/>
</beans>
根据服务接口通过不同的注册中心获取服务的具体信息然后获取这个服务的返回值,不同服务走不同的注册中心……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐