dubbo直連提供者 & 只訂閱 & 只注冊


1.    dubbo直連提供者

  在開發及測試環境下,經常需要繞過注冊中心,只測試指定服務提供者,這時候可能需要點對點直連,點對點直連方式,將以服務接口為單位,忽略注冊中心的提供者列表,A 接口配置點對點,不影響 B 接口從注冊中心獲取列表為了避免復雜化線上環境,不要在線上使用這個功能,只應在測試階段使用。

其配置方式有如下三種:

1.     通過xml配置

點對點,可在 <dubbo:reference> 中配置 url 指向提供者,將繞過注冊中心,多個地址用分號隔開,配置如下

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService"
        url="dubbo://localhost:20880" />

2.通過 -D 參數指定

在 JVM 啟動參數中加入-D參數映射服務地址,如:

java -Ddubbo.DubboService=dubbo://localhost:20880

3. 通過文件映射

如果服務比較多,也可以用文件映射,用 -Ddubbo.resolve.file 指定映射文件路徑,此配置優先級高於 <dubbo:reference> 中的配置,如:

java -Ddubbo.resolve.file=xxx.properties

然后在映射文件 xxx.properties 中加入配置,其中 key 為服務名,value 為服務提供者 URL:

dubbo.DubboService=dubbo://localhost:20880

 

注意:

 1.0.15 及以上版本支持,2.0 以上版本自動加載 ${user.home}/dubbo-resolve.properties文件,不需要配置 

${user.home}指的是當前操作系統用戶目錄,如 Windows系統 Administrator的用戶目錄就是 C:\Users\Administrator,當然可以用git工具查看

Administrator@MicroWin10-1535 MINGW64 ~/Desktop
$ cd ~

Administrator@MicroWin10-1535 MINGW64 ~
$ pwd
/c/Users/Administrator

 

測試方法如下:

dubbo-resolve.properties內容:

dubbo.DubboService=dubbo://localhost:20880

 

 consumer.xml內容如下:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
    <dubbo:application name="consumer-of-helloworld-app">
        <!-- 關閉QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService" />
</beans>

 

 消費者代碼:

package dubbo;

import java.util.concurrent.CountDownLatch;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "consumer.xml" });
        context.start();
        for (int i = 0; i < 2; i++) {
            DubboService service = (DubboService) context.getBean("dubboService");
            System.out.println(service.sayHello(" china "));
        }

        CountDownLatch countDownLatch = new CountDownLatch(1);
        countDownLatch.await();
    }
}

 2. dubbo 只訂閱(也就是只獲取服務不發布服務)

  為方便開發測試,經常會在線下共用一個所有服務可用的注冊中心,這時,如果一個正在開發中的服務提供者注冊,可能會影響消費者不能正常運行。

  可以讓服務提供者開發方,只訂閱服務(開發的服務可能依賴其它服務),而不注冊正在開發的服務,通過直連測試正在開發的服務

     

禁用注冊配置:

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

或者:

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

 

例如:

provider.xml(只訂閱,不注冊服務)

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方應用信息,用於計算依賴關系 -->
    <dubbo:application name="hello-world-app">
        <!-- 關閉QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="true" />
        <dubbo:parameter key="qos.port" value="2222" />
    </dubbo:application>

    <dubbo:registry address="zookeeper://127.0.0.1:2181" register="false"/>

    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 聲明需要暴露的服務接口 -->
    <dubbo:service interface="dubbo.DubboService" ref="demoService"
        timeout="50000" />

    <!-- 和本地bean一樣實現服務 -->
    <bean id="demoService" class="dubbo.DubboServiceImpl"  />
</beans>

 

 consumer.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
    <dubbo:application name="consumer-of-helloworld-app">
        <!-- 關閉QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>

    <!-- 使用multicast廣播注冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService" />
</beans>

 

消費者代碼如下:

package dubbo;

import java.util.concurrent.CountDownLatch;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "consumer.xml" });
        context.start();
        for (int i = 0; i < 2; i++) {
            DubboService service = (DubboService) context.getBean("dubboService");
            System.out.println(service.sayHello(" china "));
        }

        CountDownLatch countDownLatch = new CountDownLatch(1);
        countDownLatch.await();
    }
}

直接運行會報錯如下:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dubboService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service dubbo.DubboService. No provider available for the service dubbo.DubboService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=consumer-of-helloworld-app&dubbo=2.0.2&interface=dubbo.DubboService&methods=sayHello&pid=13640&qos.enable=false&register.ip=192.168.0.232&side=consumer&timestamp=1554363548873 to the consumer 192.168.0.232 use dubbo version 2.6.6
通過管理界面查看也沒有服務:

 

 修改上面consumer.xml通過直連提供者即可:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
    <dubbo:application name="consumer-of-helloworld-app">
        <!-- 關閉QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>

    <!-- 使用multicast廣播注冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService"
        url="dubbo://localhost:20880" />
</beans>

 

3.只注冊(也就是只發布服務不獲取服務)

  如果有兩個鏡像環境,兩個注冊中心,有一個服務只在其中一個注冊中心有部署,另一個注冊中心還沒來得及部署,而兩個注冊中心的其它應用都需要依賴此服務。這個時候,可以讓服務提供者方只注冊服務到另一注冊中心,而不從另一注冊中心訂閱服務。

禁用訂閱配置:

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

 

或者:

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

 

 

測試方法如下:

provider.xml還是上面的配置,發布以及注冊服務均可以

修改consumer.xml,只注冊不訂閱,如下:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
    <dubbo:application name="consumer-of-helloworld-app">
        <!-- 關閉QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>

    <!-- 使用multicast廣播注冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"
        subscribe="false" />

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService" />
</beans>

 

消費者運行報錯如下:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dubboService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No such any registry to reference dubbo.DubboService on the consumer 192.168.0.232 use dubbo version 2.6.6, please config <dubbo:registry address="..." /> to your spring config.
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:177)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)

 

去掉上面  consumer.xml  的  subscribe="false"  的限制即可

    <!-- 使用multicast廣播注冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

 

或者:直連提供者:

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService"
        url="dubbo://localhost:20880" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM