初识Dubbo 系列之6-Dubbo 配置
配置
Xml配置
演示样例:
<?
xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd > < dubbo:application name = "hello-world-app" /> < dubbo:registry address = "multicast://224.5.6.7:1234" /> < dubbo:protocol name = "dubbo" port = "20880" /> < dubbo:service interface = "com.alibaba.dubbo.demo.DemoService" ref = "demoServiceLocal" /> < dubbo:reference id = "demoServiceRemote" interface = "com.alibaba.dubbo.demo.DemoService" /> </ beans > |
全部标签者支持自己定义參数,用于不同扩展点实现的特殊配置。 |
如:
< dubbo:protocol name = "jms" > </ dubbo:protocol > |
或:(2.1.0開始支持)
注意声明:xmlns:p="http://www.springframework.org/schema/p" |
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd > </ beans > |
Configuration Relation:
- <dubbo:service/> 服务配置,用于暴露一个服务,定义服务的元信息,一个服务能够用多个协议暴露,一个服务也能够注冊到多个注冊中心。
- <dubbo:reference/> 引用配置。用于创建一个远程服务代理,一个引用能够指向多个注冊中心。
- <dubbo:protocol/> 协议配置,用于配置提供服务的协议信息,协议由提供方指定。消费方被动接受。
- <dubbo:application/> 应用配置,用于配置当前应用信息,无论该应用是提供者还是消费者。
- <dubbo:module/> 模块配置,用于配置当前模块信息。可选。
- <dubbo:registry/> 注冊中心配置,用于配置连接注冊中心相关信息。
- <dubbo:monitor/> 监控中心配置。用于配置连接监控中心相关信息,可选。
- <dubbo:provider/> 提供方的缺省值,当ProtocolConfig和ServiceConfig某属性没有配置时,採用此缺省值,可选。
- <dubbo:consumer/> 消费方缺省配置,当ReferenceConfig某属性没有配置时。採用此缺省值。可选。
- <dubbo:method/> 方法配置,用于ServiceConfig和ReferenceConfig指定方法级的配置信息。
- <dubbo:argument/> 用于指定方法參数配置。
Configuration Override:
- 上图中以timeout为例,显示了配置的查找顺序,其他retries, loadbalance, actives等类似。
- 方法级优先。接口级次之,全局配置再次之。
- 假设级别一样,则消费方优先,提供方次之。
- 当中。服务提供方配置,通过URL经由注冊中心传递给消费方。
- 建议由服务提供方设置超时。由于一个方法须要运行多长时间,服务提供方更清楚。假设一个消费方同一时候引用多个服务,就不须要关心每一个服务的超时设置。
- 理论上ReferenceConfig的非服务标识配置,在ConsumerConfig,ServiceConfig, ProviderConfig均能够缺省配置。
属性配置
假设公共配置非常easy,没有多注冊中心。多协议等情况。或者想多个Spring容器想共享配置,能够使用dubbo.properties作为缺省配置。 |
Dubbo将自己主动载入classpath根文件夹下的dubbo.properties,能够通过JVM启动參数:-Ddubbo.properties.file=xxx.properties 改变缺省配置位置。 |
假设classpath根文件夹下存在多个dubbo.properties,比方多个jar包中有dubbo.properties,Dubbo会随意载入。并打印Error日志,兴许可能改为抛异常。 |
映射规则:
- 将XML配置的标签名,加属性名,用点分隔,多个属性拆成多行:
- 比方:dubbo.application.name=foo等价于<dubbo:application name="foo" />
- 比方:dubbo.registry.address=10.20.153.10:9090等价于<dubbo:registry address="10.20.153.10:9090" />
- 假设XML有多行同名标签配置,可用id号区分,假设没有id号将对全部同名标签生效:
- 比方:dubbo.protocol.rmi.port=1234等价于<dubbo:protocol id="rmi" name="rmi" port="1099" /> (协议的id没配时,缺省使用协议名作为id)
- 比方:dubbo.registry.china.address=10.20.153.10:9090等价于<dubbo:registry id="china" address="10.20.153.10:9090" />
典型配置如:
dubbo.application.name=foo dubbo.application.owner=bar dubbo.registry.address= 10.20 . 153.10 : 9090 |
覆盖策略:
- JVM启动-D參数优先,这样能够使用户在部署和启动时进行參数重写,比方在启动时需改变协议的port。
- XML次之,假设在XML中有配置,则dubbo.properties中的对应配置项无效。
- Properties最后,相当于缺省值,仅仅有XML没有配置时,dubbo.properties的对应配置项才会生效,通经常使用于共享公共配置,比方应用名。
注解配置
2.2.1以上版本号支持 |
服务提供方注解:
import
@Service (version= "1.0.0" ) public FooServiceImpl implements FooService { // ...... } |
服务提供方配置:
<!-- 公共信息。也能够用dubbo.properties配置 --> < dubbo:application name = "annotation-provider" /> < dubbo:registry address = "127.0.0.1:4548" /> <!-- 扫描注解包路径。多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中全部的类 --> < dubbo:annotation package = "com.foo.bar.service" /> |
服务消费方注解:
import
import
@Component public BarAction { @Reference (version= "1.0.0" ) private FooService fooService; } |
服务消费方配置:
<!-- 公共信息,也能够用dubbo.properties配置 --> < dubbo:application name = "annotation-consumer" /> < dubbo:registry address = "127.0.0.1:4548" /> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中全部的类 --> < dubbo:annotation package = "com.foo.bar.action" /> |
也能够使用:(等价于前面的:<dubbo:annotation package="com.foo.bar.service" />)
< dubbo:annotation /> < context:component-scan base-package = "com.foo.bar.service" > < context:include-filter type = "annotation" expression = "com.alibaba.dubbo.config.annotation.Service" /> </ context:component-scan > |
Spring2.5及以后版本号支持component-scan。假设用的是Spring2.0及曾经版本号。需配置:
|
API配置
API属性含义參考 API属性与配置项一对一。各属性含义。请參见:配置參考手冊 (+), 比方:ApplicationConfig.setName("xxx") 相应 <dubbo:application name="xxx" /> |
(1) 服务提供者:
import
import
import
import
import
import
// 服务实现 XxxService xxxService = new XxxServiceImpl(); // 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName( "xxx" ); // 连接注冊中心配置 RegistryConfig registry = new RegistryConfig(); registry.setAddress( "10.20.130.230:9090" ); registry.setUsername( "aaa" ); registry.setPassword( "bbb" ); // 服务提供者协议配置 ProtocolConfig protocol = new ProtocolConfig(); protocol.setName( "dubbo" ); protocol.setPort( 12345 ); protocol.setThreads( 200 ); // 注意:ServiceConfig为重对象,内部封装了与注冊中心的连接,以及开启服务port // 服务提供者暴露服务配置 ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此实例非常重,封装了与注冊中心的连接,请自行缓存,否则可能造成内存和连接泄漏 service.setApplication(application); service.setRegistry(registry); // 多个注冊中心能够用setRegistries() service.setProtocol(protocol); // 多个协议能够用setProtocols() service.setInterface(XxxService. class ); service.setRef(xxxService); service.setVersion( "1.0.0" ); // 暴露及注冊服务 service.export(); |
(2) 服务消费者:
import
import
import
import
import
// 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName( "yyy" ); // 连接注冊中心配置 RegistryConfig registry = new RegistryConfig(); registry.setAddress( "10.20.130.230:9090" ); registry.setUsername( "aaa" ); registry.setPassword( "bbb" ); // 注意:ReferenceConfig为重对象。内部封装了与注冊中心的连接,以及与服务提供方的连接 // 引用远程服务 ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例非常重,封装了与注冊中心的连接以及与提供者的连接,请自行缓存。否则可能造成内存和连接泄漏 reference.setApplication(application); reference.setRegistry(registry); // 多个注冊中心能够用setRegistries() reference.setInterface(XxxService. class ); reference.setVersion( "1.0.0" ); // 和本地bean一样使用xxxService XxxService xxxService = reference.get(); // 注意:此代理对象内部封装了全部通讯细节,对象较重,请缓存复用 |
(3) 特殊场景
注:以下仅仅列出不同的地方,其他參见上面的写法
(3.1) 方法级设置:
... // 方法级配置 List<MethodConfig> methods = new ArrayList<MethodConfig>(); MethodConfig method = new MethodConfig(); method.setName( "createXxx" ); method.setTimeout( 10000 ); method.setRetries( 0 ); methods.add(method); // 引用远程服务 ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例非常重,封装了与注冊中心的连接以及与提供者的连接,请自行缓存。否则可能造成内存和连接泄漏 ... reference.setMethods(methods); // 设置方法级配置 ... |
(3.2) 点对点直连:
... ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例非常重,封装了与注冊中心的连接以及与提供者的连接。请自行缓存,否则可能造成内存和连接泄漏 // 假设点对点直连,能够用reference.setUrl()指定目标地址,设置url后将绕过注冊中心, // 当中,协议相应provider.setProtocol()的值。port相应provider.setPort()的值, // 路径相应service.setPath()的值,假设未设置path。缺省path为接口名 reference.setUrl( "dubbo://10.20.130.230:20880/com.xxx.XxxService" ); ... |
初识Dubbo 系列之6-Dubbo 配置的更多相关文章
- dubbo系列四、dubbo服务暴露过程源码解析
一.代码准备 1.示例代码 参考dubbo系列二.dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台) 2.简单了解下spring自定义标签 https://w ...
- dubbo系列十一、dubbo transport层记录
前言 在dubbo接口方法重载且入参未显式指定序列化id导致ClassCastException分析时候用到了dubbo的通信层和编解码,dubbo有个transport层,默认使用netty4进行网 ...
- dubbo系列五、dubbo核心配置
一.配置文件 1.生产者配置provider.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...
- dubbo系列二:dubbo常用功能总结
准备工作: (1)启动zookeeper作为dubbo的注册中心 (2)新建一个maven的生产者web工程dubbo-provider-web和一个maven的消费者web工程dubbo-consu ...
- dubbo系列一:dubbo介绍、dubbo架构、dubbo的官网入门使用demo
一.dubbo介绍 Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成.简单地说,dubbo是一个基于Spri ...
- dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)
一.zookeeper配置中心安装 1.下载安装包,zookeeper-3.4.6.tar.gz 2.解压安装包,修改配置文件 参考zookeeper-3.4.6/conf/zoo_sample.cf ...
- dubbo系列一、dubbo启动流程
目录 dubbo启动流程分析记录 一.dubbo provider启动流程 1.自动装配 2.ServiceBean处理 3.服务暴露export() 3.1.检测dubbo.xxx.配置属性,配置到 ...
- dubbo系列一、dubbo背景介绍、微服务拆分
一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...
- dubbo系列七、dubbo @Activate 注解使用和实现解析
一.用法 Activate注解表示一个扩展是否被激活(使用),可以放在类定义和方法上,dubbo用它在spi扩展类定义上,表示这个扩展实现激活条件和时机. @Activate(group = Cons ...
- dubbo系列二、dubbo请求流程记录
目录 1.dubbo请求处理流程 1.1. consumer端处理流程 1.2.provider端处理流程 1.3.dubbo请求分析记录-图 泳道图 xmind图 2.dubbo请求核心说明 1.d ...
随机推荐
- css create 多边形 polygon
案例: 代码: element.style { width: 0; height: 0; /* border-left: 50px solid transparent; */ border-rig ...
- sql partition by
--不分班按学生成绩排名 select *,ROW_NUMBER() over(order by Score desc) as Sequence from Student id Gr ...
- powershell常用
对于powershell,比较强大的shell,可以直接调用.net进行下载等等 get-command|where-object{$_.name -like 'write*'} get-wmiobj ...
- Codeforces Round #453 Div. 2 A B C D (暂时)
// 从大作业和实验报告中爬出来水一发 // 补题...还是得排在写完实验报告之后... A. Visiting a Friend 题意 给定若干段行车区间,问能否从起点到终点 思路 扫描一遍,维护最 ...
- C语言中的基础知识变量探讨
C语言中的变量是编程的基础,主要有四个要素:存储类型.存储大小.存储名称和存储地址. 一.变量的要素: 1.存储类型:主要表明名变量存储的特征,主要有auto.extern.static和regist ...
- formal parameter
formal parameter : [3.16] object declared as part of a function declaration or definition that acqui ...
- javascript 表格隔行换色
用到的知识点: 获取表格元素 tbody 和 rows都是有索引的 这里我们只有一组tbody所以 索引是0 偶数行 索引取余2为0 奇数行 索引取余2不为0 通过 遍历行索引设置相应的颜色. < ...
- 基于VUE开发项目
前言 最近由于公司需要,需要写一个相对来说比较大型的后台管理系统.为了保证管理系统操作体验较为舒适并且项目后期益于维护,最后决定基于VUE全家桶来开发一个高度组件化的单页SPA应用. 技术选型 vue ...
- Scala之Future
一.简介 Future提供了一套高效便捷的非阻塞并行操作管理方案.其基本思想很简单,所谓Future,指的是一类占位符对象,用于指代某些尚未完成的计算的结果.一般来说,由Future指代的计算都是并行 ...
- Spring中使用集成MongoDB Client启动时报错:rc: 48
一定是所在的服务器也装了MongoDB导致端口冲突,解决方法:kill掉全部MongoDB的进程. ps aux | grep mongod PID 参考: http://blog.csdn.net/ ...