通过之前的学习了解了dubbo的常规的使用,下面我们看看特殊情况或者说真实环境下使用dubbo的一些配置实例。

一、一个接口有多个实现时可以使用group来区分

1、服务提供者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
  11. <dubbo:application name="hello-world-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露服务地址 -->
  13. <dubbo:registry  protocol="zookeeper"  address="192.168.0.107:2181"/>
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 声明需要暴露的服务接口 -->
  17. <dubbo:service  group="1" interface="com.test.dubboser.ServiceDemo"
  18. ref="demoService" />
  19. <dubbo:service group="2" interface="com.test.dubboser.ServiceDemo"
  20. ref="demoService2" />
  21. <!--和本地bean一样实现服务 -->
  22. <bean id="demoService"  class="com.test.dubboser.ServiceImp"/>
  23. <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
  24. </beans>

其他的配置都是一样,而暴露的服务接口通过group来区分两个实现类

2、服务消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  11. <dubbo:application name="consumer-of-helloworld-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
  13. <dubbo:registry  protocol="zookeeper"  address="192.168.0.107:2181" />
  14. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  15. <dubbo:reference id="demoServicemy" group="1" interface="com.test.dubboser.ServiceDemo" />
  16. <dubbo:reference id="demoServicemy2" group="2" interface="com.test.dubboser.ServiceDemo" />
  17. </beans>

这里同样使用group来区分

二、当一个接口实现出现不兼容升级时可以用版本号过渡,版本号不同的服务互相间不引用

1、服务提供者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
  11. <dubbo:application name="hello-world-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露服务地址 -->
  13. <dubbo:registry  protocol="zookeeper"  address="192.168.0.107:2181"/>
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 声明需要暴露的服务接口 -->
  17. <dubbo:service  version="1" interface="com.test.dubboser.ServiceDemo"
  18. ref="demoService" />
  19. <dubbo:service version="2" interface="com.test.dubboser.ServiceDemo"
  20. ref="demoService2" />
  21. <!--和本地bean一样实现服务 -->
  22. <bean id="demoService"  class="com.test.dubboser.ServiceImp"/>
  23. <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
  24. </beans>

添加version 来区分

2、服务消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  11. <dubbo:application name="consumer-of-helloworld-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
  13. <dubbo:registry  protocol="zookeeper"  address="192.168.0.107:2181" />
  14. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  15. <dubbo:reference id="demoServicemy" version="1" interface="com.test.dubboser.ServiceDemo" />
  16. <dubbo:reference id="demoServicemy2" version="2" interface="com.test.dubboser.ServiceDemo" />
  17. </beans>

服务消费者这边也要使用version 来区分

三、点对点直连/指定调用需求(一般在开发测试环境中使用)

点对点直连的话我们就没必要使用zookeeper来做注册中心了,直接启动服务提供者而服务消费者直接调用指定的服务消费者接口实现类方法,所以注意这里的配置方式

1、服务提供者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
  11. <dubbo:application name="hello-world-app-my" />
  12. <!--没有将服务注册到注册中心  -->
  13. <dubbo:registry address="N/A" />
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 声明需要暴露的服务接口 -->
  17. <dubbo:service   interface="com.test.dubboser.ServiceDemo"
  18. ref="demoService" />
  19. <!--和本地bean一样实现服务 -->
  20. <bean id="demoService"  class="com.test.dubboser.ServiceImp"/>
  21. </beans>

2、服务消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  11. <dubbo:application name="consumer-of-helloworld-app-my" />
  12. <!--没有注册到注册中心  -->
  13. <dubbo:registry address="N/A" ></dubbo:registry>
  14. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  15. <dubbo:reference id="demoServicemy"  interface="com.test.dubboser.ServiceDemo" url="dubbo://192.168.0.107:20880/com.test.dubboser.ServiceDemo"/>
  16. </beans>

点对点的访问url 就是服务端的ip:port/接口全路径,也就是上面配置文件所示!

四、只订阅,共用注册中心,开发人员机器上的服务提供者被吴调用,影响其他开发人员

为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行。

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

1、服务提供者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
  11. <dubbo:application name="hello-world-app-my" />
  12. <!-- 不注册-->
  13. <dubbo:registry  protocol="zookeeper"  address="192.168.0.107:2181" register="false"/>
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 声明需要暴露的服务接口 -->
  17. <dubbo:service  interface="com.test.dubboser.ServiceDemo"
  18. ref="demoService" />
  19. <!--和本地bean一样实现服务 -->
  20. <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
  21. </beans>

注意这里在想注册中心注册的时候有register="false" 所以服务提供者没有向服务注册中心注册服务,也就是没有暴露服务接口等信息

2、服务消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  11. <dubbo:application name="consumer-of-helloworld-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
  13. <dubbo:registry  protocol="zookeeper"  address="192.168.0.107:2181" />
  14. <!-- 还是使用点对点的方式访问,因为服务提供者没有向注册中心注册 -->
  15. <dubbo:reference id="demoServicemy3"  interface="com.test.dubboser.ServiceDemo" url="dubbo://192.168.0.107:20880/com.test.dubboser.ServiceDemo" />
  16. </beans>

服务消费者还是使用了点对点的方法方式来访问

只订阅模式的图形:

ok 这篇文章到此就结束了,这篇文章中没有给出java相关的代码是因为那些和之前的都一样的而把这些比较实用而且在开发中确实能遇到的配置在这里贴出来备份以后使用,当然了这些配置都测试过,都是可以使用!

最新文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (18) -----第三章 查询之结果集扁平化和多属性分组
  2. firefox与chrome中对select下拉框中的option支持问题
  3. 在VS 2010中使用 VS2013的解决方案
  4. cocos2d-x lua table数据存储
  5. MVC中的统一验证机制
  6. POJ 2485:Highways(最小生成树&amp;amp;&amp;amp;prim)
  7. js闭包和ie内存泄露原理
  8. Es6 新增函数
  9. 什么是redis,redis能做什么,redis应用场景
  10. Java开发微服务为什么一定要选spring cloud?
  11. 如何在Mac上安全彻底的卸载软件?
  12. Java反射(一眼就看会)
  13. 7series 逻辑单元理解(更新中)
  14. freemarker学习 (servlet + freemarker -&gt; Struts2+freemarker -&gt; springMVC+freemarker)
  15. Python调用sqlAlchemy
  16. VScode中支持Python虚拟环境
  17. Pytest 生成Report
  18. 2018java面试知识汇总
  19. (线段树模板)A Simple Problem with Integers --POJ--3468
  20. 字符串处理strcpy strcat函数的用法

热门文章

  1. 前端打包利器:webpack工具
  2. 转:Eclipse常见问题,快捷键收集
  3. Android Gradle基础实践
  4. dlsym
  5. iPhone手机解锁效果&amp;&amp;自定义滚动条&amp;&amp;拖拽--Clone&amp;&amp;窗口拖拽(改变大小/最小化/最大化/还原/关闭)
  6. C#程序不包含适合于入口点的静态“Main”方法怎么办
  7. 当php懈垢windows通用上传缺陷
  8. 使用RTTI为继承体系编写&rdquo;==&rdquo;运算符
  9. 排序算法 C++代码实现
  10. sqlplus登入和plsql登入的差别