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- MINGW64 ~/Desktop
$ cd ~ Administrator@MicroWin10- 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®ister.ip=192.168.0.232&side=consumer×tamp=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" />
dubbo直连提供者 & 只订阅 & 只注册的更多相关文章
- Dubbo入门到精通学习笔记(十二):Dubbo消费端直连提供者(开发调试)、Dubbo服务只订阅(开发调试)、Dubbo服务只注册
文章目录 Dubbo消费端直连提供者(开发调试) Dubbo服务只订阅(开发调试) Dubbo服务只注册 Dubbo消费端直连提供者(开发调试) Dubbo 官方文档: 用户指南 >> 示 ...
- Dubbo中只订阅与只注册
一:只订阅 1.场景 为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行. 可以让服务提供者开发方,只订阅服务(开发的服 ...
- dubbo之只订阅及只注册
只订阅 问题 如果有两个镜像环境,两个注册中心,有一个服务只在其中一个注册中心有部署,另一个注册中心还没来得及部署,而两个注册中心的其它应用都需要依赖此服务,所以需要将服务同时注册到两个注册中心,但却 ...
- Dubbo 只注册,只订阅
只注册场景: 某一个服务,被注册中心的一些服务依赖,但是该服务不提供给消费者调用,这个时候使用只注册,注册到注册中心,注册中心内部服务可以调用该服务,但是消费者不可以.(这个服务是被调用方) 只订阅场 ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 只订阅
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 只订阅 问题 为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如 ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 直连提供者
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 直连提供者 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候 ...
- dubbo之直连提供者
在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候可能需要点对点直连,点对点直联方式,将以服务接口为单位,忽略注册中心的提供者列表,A 接口配置点对点,不影响 B 接口从注册中心获 ...
- Dubbo的直接提供者
1.场景 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候可能需要点对点直连,点对点直联方式,将以服务接口为单位,忽略注册中心的提供者列表,A 接口配置点对点,不影响 B 接口从 ...
- Dubbo源码学习总结系列七---注册中心
Dubbo注册中心是框架的核心模块,提供了服务注册发现(包括服务提供者.消费者.路由策略.覆盖规则)的功能,该功能集中体现了服务治理的特性.该模块结合Cluster模块实现了集群服务.Dubbo管理控 ...
随机推荐
- 使用 ng build 构建后资源地址引用错误的问题
最近使用 Angular 做项目的时候,通过 ng build 打包后的资源地址都带有 localhost:4200,百思不得其解,以为是打包的问题. 最后在 index.html 的文件中发现有一个 ...
- 石家庄地铁系统开发(java web版)(一)
今天所完成的任务: 在Mysql数据库中创建了sjzsubstop表和sjzsubway表 sjzsubstop表用于录入所有站点名称和与之对应的stopid(作为主码) sjzsubway表用于录入 ...
- vscode 安装插件SVN 报vscode SVN not found
1.软件环境 svn客户端安装的是TortoiseSVN: vscode 安装的为SVN的插件: 2. 问题现象 vscode打开文件夹后右下角提示如下报错:SVN not found. Instal ...
- 【翻译】asp.net core2.0中的token认证
原文地址:https://developer.okta.com/blog/2018/03/23/token-authentication-aspnetcore-complete-guide token ...
- Django-5 模板层
Django 模板层 在之前的例子中,我们采用了硬编码的方式,来返回文本 def current_datetime(request): now = datetime.datetime.now() ht ...
- Django1-HTTP协议介绍
何为http协议(Hypertext Transfer Protocol,超文本传输协议)? 所谓协议,就是指双方遵循的规范.http协议,就是浏览器和服务器之间进行“沟通”的一种规范.我们在看空间, ...
- [转]html5监听任何App自带返回键javascript事件
1.前言 如今h5新特性.新标签.新规范等有很多,而且正在不断完善中,各大浏览器商对它们的支持,也是相当给力.作为前端程序员,我觉得我们还是有必要积极关注并勇敢地加以实践.接下来我将和各位分享一个特别 ...
- parcel+vue入门
一.parcel简单使用 npm install -D parcel-bundler npm init -y (-y表示yes,跳过项目初始化提问阶段,直接生成package.json 文件.) Pa ...
- Java 学习(1)----- java 学习的总体感觉
好久没有更新博客了,是因为最近在集中精力学习java, Java 的基础知识确实是比 js 多太多了. 学习java 断断续续的差不多有一年左右的时间, 这一年来,感觉懂了一点,过一段时间又忘记了,总 ...
- torch.view()详解及-1参数是什么意思
经常可以看到调用torch.view(-1,28*28)之类的调用,那么这里的-1是什么意思呢,经过查看文档view()得到了一下结果: view()返回的数据和传入的tensor一样,只是形状不同 ...