通过之前的学习了解了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相关的代码是因为那些和之前的都一样的而把这些比较实用而且在开发中确实能遇到的配置在这里贴出来备份以后使用,当然了这些配置都测试过,都是可以使用!

dubbo开发中使用到的一些服务配置方式的更多相关文章

  1. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  2. Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

    Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...

  3. Log4j 2.0在开发中的高级使用具体解释—配置简单的控制台输出(三)

    Log4j 2.0在近期迎来了重大的版本号升级.攻克了1.x中死锁bug之外,性能也有10倍的提升. 相同的在最新版本号中的新特性中. 配置文件也不只局限于xml和java特性文件properties ...

  4. iOS开发中方法延迟执行的几种方式

    概述 项目开发中经常会用到方法的延时调用,下面列举常用的几种实现方式: 1.performSelector 2.NSTimer 3.NSThread线程的sleep 4.GCD 1.performSe ...

  5. Android开发中常用的ListView列表的优化方式ViewHolder

    在Android开发中难免会遇到大量的数据加载到ListView中进行显示, 然后其中最重要的数据传递桥梁Adapter适配器是常用的,随着市场的需 求变化ListView'条目中的内容是越来越多这就 ...

  6. spring 中常用的两种事务配置方式以及事务的传播性、隔离级别

    一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...

  7. centos中selinux功能及常用服务配置

    SELinux: Secure Enhenced Linux 常用命令 获取selinux的当前状态: # getenforce 临时启用或禁用: # setenfoce 0|1 永久性启用,需要修改 ...

  8. 浅谈iOS开发中方法延迟执行的几种方式

    Method1. performSelector方法 Method2. NSTimer定时器 Method3. NSThread线程的sleep Method4. GCD 公用延迟执行方法 - (vo ...

  9. iOS开发中数组常用的五种遍历方式

    随着iOS的不断发展,apple也不断推出性能更高的数组遍历方式,下面将对熟悉的五种遍历方式进行列举. 首先定义一个数组,并获取数组长度 NSArray *array=@[",]; NSIn ...

随机推荐

  1. zabbix_sender高效模式

    1.zabbix_sender介绍 zabbix获取key值有超时时间,如果自定义的key脚本一般需要执行很长时间,这根本没法去做监控,获取数据有超时时间,如果一些数据需要执行比较长的时间才能获取的话 ...

  2. POJ 1144 Network(无向图连通分量求割点)

    题目地址:id=1144">POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u ...

  3. SQL 关键字 'USER' 附近有语法错误怎么办

    如下图所示,我想要访问我的Database1.mdf的user这张表,提示如下错误   user在SQL Server中是系统保留字,将user修改为[user]就可以了.但是直接在VS中是无法修改的 ...

  4. Android学习(七) Android实现计算器

    前台页面代码,通过线性布局方式实现计算器页面:如图所示 color.xml,自定义颜色values: <?xml version="1.0" encoding="u ...

  5. 读jQuery源码之整体框架分析

    读一个开源框架,大家最想学到的就是设计的思想和实现的技巧.最近读jQuery源码,记下我对大师作品的理解和心得,跟大家分享,权当抛砖引玉. 先附上jQuery的代码结构. (function(){ / ...

  6. SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】

    配置web.xml(struts2的过滤器.spring监听器.解决Hibernate延迟加载问题的过滤器.解决中文乱码的过滤器) <!-- 解决中文乱码问题 --> <filter ...

  7. Android 图片查看器

    1 http://blog.csdn.net/wang8512945/article/details/8075413 2 http://blog.csdn.net/lcore/article/deta ...

  8. c#调用WinRAR软件压缩和解压文件

    using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...

  9. 关于TCP/IP,这十个问题你都知道,就入门了!

    关于TCP/IP,必知必会的十个问题 本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养.   TCP/IP十个问题 一.TCP/IP模型 TCP/IP ...

  10. Python script to create Screen from all Items/Graphs of a host

    #!/usr/bin/env python import urllib2 import json import argparse def authenticate(url, username, pas ...