Dubbo:服务直连


#Dubbo


服务直连的意思是,不再经过 zookeeper 进行服务发现找到服务端的地址,而是指定服务端地址,直接向服务端发起调用。

这种用法适用于服务测试,不建议在线上使用。

示例

服务端依旧将服务注册到 zookeeper,dubbo-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="hello-world-app"></dubbo:application>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:protocol name="dubbo" port="20881"/>

    <dubbo:service interface="demo.contract.DemoService" class="demo.provider.DemoServiceImpl"/>

</beans>

dubbo 服务的端口是 20881 。

消费端(调用方)使用 zookeeper 进行服务发现,dubbo-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="consumer-of-helloworld-app"/>
    <dubbo:consumer timeout="5000" />
    <!-- 配置zookeeper -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>

    <dubbo:reference id="demoService" interface="demo.contract.DemoService" check="false"/>

</beans>

消费端不使用 zookeeper,而是直接连接服务端,在dubbo:reference中指定url属性即可:

<?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"/>
    <dubbo:consumer timeout="5000" />
    
    <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>-->

    <dubbo:reference id="demoService"
                     interface="demo.contract.DemoService"
                     url="dubbo://127.0.0.1:20881"
                     check="false"/>

</beans>


( 本文完 )