dubbo开发中使用到的一些服务配置方式
通过之前的学习了解了dubbo的常规的使用,下面我们看看特殊情况或者说真实环境下使用dubbo的一些配置实例。
一、一个接口有多个实现时可以使用group来区分
1、服务提供者配置
- <?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
- ">
- <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
- <dubbo:application name="hello-world-app-my" />
- <!-- 使用zookeeper广播注册中心暴露服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181"/>
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service group="1" interface="com.test.dubboser.ServiceDemo"
- ref="demoService" />
- <dubbo:service group="2" interface="com.test.dubboser.ServiceDemo"
- ref="demoService2" />
- <!--和本地bean一样实现服务 -->
- <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
- <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
- </beans>
其他的配置都是一样,而暴露的服务接口通过group来区分两个实现类
2、服务消费者配置
- <?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-my" />
- <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181" />
- <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="demoServicemy" group="1" interface="com.test.dubboser.ServiceDemo" />
- <dubbo:reference id="demoServicemy2" group="2" interface="com.test.dubboser.ServiceDemo" />
- </beans>
这里同样使用group来区分
二、当一个接口实现出现不兼容升级时可以用版本号过渡,版本号不同的服务互相间不引用
1、服务提供者配置
- <?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
- ">
- <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
- <dubbo:application name="hello-world-app-my" />
- <!-- 使用zookeeper广播注册中心暴露服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181"/>
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service version="1" interface="com.test.dubboser.ServiceDemo"
- ref="demoService" />
- <dubbo:service version="2" interface="com.test.dubboser.ServiceDemo"
- ref="demoService2" />
- <!--和本地bean一样实现服务 -->
- <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
- <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
- </beans>
添加version 来区分
2、服务消费者配置
- <?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-my" />
- <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181" />
- <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="demoServicemy" version="1" interface="com.test.dubboser.ServiceDemo" />
- <dubbo:reference id="demoServicemy2" version="2" interface="com.test.dubboser.ServiceDemo" />
- </beans>
服务消费者这边也要使用version 来区分
三、点对点直连/指定调用需求(一般在开发测试环境中使用)
点对点直连的话我们就没必要使用zookeeper来做注册中心了,直接启动服务提供者而服务消费者直接调用指定的服务消费者接口实现类方法,所以注意这里的配置方式
1、服务提供者配置
- <?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
- ">
- <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
- <dubbo:application name="hello-world-app-my" />
- <!--没有将服务注册到注册中心 -->
- <dubbo:registry address="N/A" />
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.test.dubboser.ServiceDemo"
- ref="demoService" />
- <!--和本地bean一样实现服务 -->
- <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
- </beans>
2、服务消费者配置
- <?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-my" />
- <!--没有注册到注册中心 -->
- <dubbo:registry address="N/A" ></dubbo:registry>
- <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="demoServicemy" interface="com.test.dubboser.ServiceDemo" url="dubbo://192.168.0.107:20880/com.test.dubboser.ServiceDemo"/>
- </beans>
点对点的访问url 就是服务端的ip:port/接口全路径,也就是上面配置文件所示!
四、只订阅,共用注册中心,开发人员机器上的服务提供者被吴调用,影响其他开发人员
为方便开发测试,经常会在线下共用一个所有服务可用的注册中心,这时,如果一个正在开发中的服务提供者注册,可能会影响消费者不能正常运行。
以让服务提供者开发方,只订阅服务(开发的服务可能依赖其它服务),而不注册正在开发的服务,通过直连测试正在开发的服务。
1、服务提供者配置
- <?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
- ">
- <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
- <dubbo:application name="hello-world-app-my" />
- <!-- 不注册-->
- <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181" register="false"/>
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.test.dubboser.ServiceDemo"
- ref="demoService" />
- <!--和本地bean一样实现服务 -->
- <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
- </beans>
注意这里在想注册中心注册的时候有register="false" 所以服务提供者没有向服务注册中心注册服务,也就是没有暴露服务接口等信息
2、服务消费者配置
- <?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-my" />
- <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181" />
- <!-- 还是使用点对点的方式访问,因为服务提供者没有向注册中心注册 -->
- <dubbo:reference id="demoServicemy3" interface="com.test.dubboser.ServiceDemo" url="dubbo://192.168.0.107:20880/com.test.dubboser.ServiceDemo" />
- </beans>
服务消费者还是使用了点对点的方法方式来访问
只订阅模式的图形:
ok 这篇文章到此就结束了,这篇文章中没有给出java相关的代码是因为那些和之前的都一样的而把这些比较实用而且在开发中确实能遇到的配置在这里贴出来备份以后使用,当然了这些配置都测试过,都是可以使用!
dubbo开发中使用到的一些服务配置方式的更多相关文章
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...
- Log4j 2.0在开发中的高级使用具体解释—配置简单的控制台输出(三)
Log4j 2.0在近期迎来了重大的版本号升级.攻克了1.x中死锁bug之外,性能也有10倍的提升. 相同的在最新版本号中的新特性中. 配置文件也不只局限于xml和java特性文件properties ...
- iOS开发中方法延迟执行的几种方式
概述 项目开发中经常会用到方法的延时调用,下面列举常用的几种实现方式: 1.performSelector 2.NSTimer 3.NSThread线程的sleep 4.GCD 1.performSe ...
- Android开发中常用的ListView列表的优化方式ViewHolder
在Android开发中难免会遇到大量的数据加载到ListView中进行显示, 然后其中最重要的数据传递桥梁Adapter适配器是常用的,随着市场的需 求变化ListView'条目中的内容是越来越多这就 ...
- spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...
- centos中selinux功能及常用服务配置
SELinux: Secure Enhenced Linux 常用命令 获取selinux的当前状态: # getenforce 临时启用或禁用: # setenfoce 0|1 永久性启用,需要修改 ...
- 浅谈iOS开发中方法延迟执行的几种方式
Method1. performSelector方法 Method2. NSTimer定时器 Method3. NSThread线程的sleep Method4. GCD 公用延迟执行方法 - (vo ...
- iOS开发中数组常用的五种遍历方式
随着iOS的不断发展,apple也不断推出性能更高的数组遍历方式,下面将对熟悉的五种遍历方式进行列举. 首先定义一个数组,并获取数组长度 NSArray *array=@[",]; NSIn ...
随机推荐
- pclint在VS2013中的配置
1.安装pclint a. 从http://download.csdn.net/detail/finewind/8426979下载破解版的pclint9i版: b. 点击pclint9setuo.ex ...
- P6 EPPM 16 R1安装和配置文档
白桃花心木P6企业项目组合管理文档库 描述 链接 下载 零件号 16 R1用户和集成文档 查看库 下载 E68199-01 16 R1安装和配置文档 查看库 下载 E68198-01 描述 链接 ...
- Codeforces #282 div 1 C Helping People 题解
CF 282 C Helping People 题解 [原题] time limit per test 2 seconds memory limit per test 512 megabytes in ...
- Node.js 网页瘸腿爬虫初体验
延续上一篇,想把自己博客的文档标题利用Node.js的request全提取出来,于是有了下面的初哥爬虫,水平有限,这只爬虫目前还有点瘸腿,请看官你指正了. // 内置http模块,提供了http服务器 ...
- 安卓使用Socket发送中文,C语言服务端接收乱码问题解决方式
今天用安卓通过Socket发送数据到电脑上使用C语言写的服务端,发送英文没有问题,可当把数据改变成中文时,服务端接收到的数据确是乱码. 突然想到.VS的预处理使用的是ANSI编码.而安卓网络数据都是U ...
- Shell脚本之:退出循环
Shell也使用 break 和 continue 来跳出循环. break命令 下面的例子中,脚本进入死循环直至用户输入数字大于5,使用break跳出这个循环. #!/bin/bash while ...
- 谈一谈AGP
typedef enum D3DPOOL { D3DPOOL_DEFAULT = 0, D3DPOOL_MANAGED = 1, D3DPOOL_SYSTEMMEM = 2, D3DPOOL_SCRA ...
- Spring获取HttpServletRequest
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
- Socket协议通讯
Socket协议通讯 服务器端代码: using System; using System.Collections.Generic; using System.ComponentModel; us ...
- go学习资料
go书单 1.代码规范 https://github.com/golang/go/wiki/CodeReviewComments 2.基础知识 先看: https://github.com/mikel ...