hsf dubbo学习四--连接方式

来源:互联网 发布:2017 seo教程百度云 编辑:程序博客网 时间:2024/06/14 13:55

直连提供者

在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时可能需要点对点直连。

(1)如果是线上需求需要点对点,可在<dubbo:reference>中配置url指向提供者,将绕过注册中心,多个地址用分号隔开。

(2)在JVM启动参数中加入-D参数映射服务地址(key为服务名,value为服务提供者url,此配置优先级最高)

(3)发果服务比较多,也可以用文件映射(用-Ddubbo.resolve.file指定映射文件路径,此配置优先级高于<dubbo:reference>中的配置)


只订阅

可以让服务提供者开发方,只订阅服务,而不注册正在开发的服务,通过直连测试正在开发的服务。


禁用注册配置:

<dubbo:registry address="10.20.153.10:9090" register="false" />

或者

<dubbo:regisytry address="10.20.154.10:9090?register=false" />


只注册

可以让服务提供者,只注册服务到另一注册中心,而不从另一注册中心订阅服务。

禁用订阅配置:

<dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />

或者

dubbo:registry id="qdRegistry" address="10.20.141.150:9090?subscribe=false" />



静态服务

有时候希望人工管理服务提供者的上线和下线,此时需要将注册中心标识为动态管理模式。

RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));registry.register(URL.valueOf("memcached://10.20.153.11/com.foo.BarService?category=providers&dynamic=false&application=foo"));

服务提供者初次注册时为禁用状态,需要工启用,断线时,将不会被自动删除,需人工禁用。


多协议

(1)不同服务不同协议

比如:不同服务在性能上适用不同协议进行传输

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="world"  />    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />     <!-- 多协议配置 -->    <dubbo:protocol name="dubbo" port="20880" />    <dubbo:protocol name="rmi" port="1099" />     <!-- 使用dubbo协议暴露服务 -->    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />    <!-- 使用rmi协议暴露服务 -->    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" /> </beans>


(2)多协议暴露服务

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="world"  />    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />     <!-- 多协议配置 -->    <dubbo:protocol name="dubbo" port="20880" />    <dubbo:protocol name="hessian" port="8080" />     <!-- 使用多个协议暴露服务 -->    <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" /> </beans>


多注册中心

(1)多注册中注册

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="world"  />     <!-- 多注册中心配置 -->    <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />     <!-- 向多个注册中心注册 --><dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" /> </beans>


(2)不同服务使用不同注册中心


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="world"  />     <!-- 多注册中心配置 -->    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />     <!-- 向中文站注册中心注册 --><dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />     <!-- 向国际站注册中心注册 -->    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" /> </beans>

(3)多注册中心引用

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="world"  />     <!-- 多注册中心配置 -->    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />     <!-- 引用中文站服务 --><dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />     <!-- 引用国际站站服务 --><dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" /> </beans>



0 0