配置

Xml配置

配置项说明

具体配置项,请參见:配置參考手冊 (+)
API使用说明

假设不想使用Spring配置。而希望通过API的方式进行调用,请參见:API配置 (+)
配置使用说明

想知道怎样使用配置。请參见:高速启动 (+)

演示样例:

provider.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       
http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.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:parameter
key="queue"
value="http://10.20.160.198/wiki/display/dubbo/10.20.31.22"
/>
</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       
http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
>
 
    <dubbo:protocol
name="jms"
p:queue="http://10.20.160.198/wiki/display/dubbo/10.20.31.22"
/>
 
</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.properties
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
com.alibaba.dubbo.config.annotation.Service;
 
@Service(version="1.0.0")
public
class
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
com.alibaba.dubbo.config.annotation.Reference;
import
org.springframework.stereotype.Component;
 
@Component
public
class
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及曾经版本号。需配置:

<!-- Spring2.0支持@Service注解配置。但不支持package属性自己主动载入bean的实例。需人工定义bean的实例。

-->

<dubbo:annotation
/>
<bean
id="barService"
class="com.foo.BarServiceImpl"
/>

API配置

API使用范围

API仅用于OpenAPI, ESB, Test, Mock等系统集成,普通服务提供方或消费方,请採用配置方式使用Dubbo,请參见:Xml配置 (+)
API属性含义參考

API属性与配置项一对一。各属性含义。请參见:配置參考手冊 (+),

比方:ApplicationConfig.setName("xxx") 相应 <dubbo:application name="xxx" />

(1) 服务提供者:

import
com.alibaba.dubbo.rpc.config.ApplicationConfig;
import
com.alibaba.dubbo.rpc.config.RegistryConfig;
import
com.alibaba.dubbo.rpc.config.ProviderConfig;
import
com.alibaba.dubbo.rpc.config.ServiceConfig;
import
com.xxx.XxxService;
import
com.xxx.XxxServiceImpl;
 
// 服务实现
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
com.alibaba.dubbo.rpc.config.ApplicationConfig;
import
com.alibaba.dubbo.rpc.config.RegistryConfig;
import
com.alibaba.dubbo.rpc.config.ConsumerConfig;
import
com.alibaba.dubbo.rpc.config.ReferenceConfig;
import
com.xxx.XxxService;
 
// 当前应用配置
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 配置的更多相关文章

  1. dubbo系列四、dubbo服务暴露过程源码解析

    一.代码准备 1.示例代码 参考dubbo系列二.dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台) 2.简单了解下spring自定义标签 https://w ...

  2. dubbo系列十一、dubbo transport层记录

    前言 在dubbo接口方法重载且入参未显式指定序列化id导致ClassCastException分析时候用到了dubbo的通信层和编解码,dubbo有个transport层,默认使用netty4进行网 ...

  3. dubbo系列五、dubbo核心配置

    一.配置文件 1.生产者配置provider.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...

  4. dubbo系列二:dubbo常用功能总结

    准备工作: (1)启动zookeeper作为dubbo的注册中心 (2)新建一个maven的生产者web工程dubbo-provider-web和一个maven的消费者web工程dubbo-consu ...

  5. dubbo系列一:dubbo介绍、dubbo架构、dubbo的官网入门使用demo

    一.dubbo介绍 Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成.简单地说,dubbo是一个基于Spri ...

  6. dubbo系列二、dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)

    一.zookeeper配置中心安装 1.下载安装包,zookeeper-3.4.6.tar.gz 2.解压安装包,修改配置文件 参考zookeeper-3.4.6/conf/zoo_sample.cf ...

  7. dubbo系列一、dubbo启动流程

    目录 dubbo启动流程分析记录 一.dubbo provider启动流程 1.自动装配 2.ServiceBean处理 3.服务暴露export() 3.1.检测dubbo.xxx.配置属性,配置到 ...

  8. dubbo系列一、dubbo背景介绍、微服务拆分

    一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...

  9. dubbo系列七、dubbo @Activate 注解使用和实现解析

    一.用法 Activate注解表示一个扩展是否被激活(使用),可以放在类定义和方法上,dubbo用它在spi扩展类定义上,表示这个扩展实现激活条件和时机. @Activate(group = Cons ...

  10. dubbo系列二、dubbo请求流程记录

    目录 1.dubbo请求处理流程 1.1. consumer端处理流程 1.2.provider端处理流程 1.3.dubbo请求分析记录-图 泳道图 xmind图 2.dubbo请求核心说明 1.d ...

随机推荐

  1. 【转】Nodejs学习笔记(二)--- 模块

    目录 简介及资料 自定义模块 创建一个自定义模块 调用自定义模块 exports和module.exports 区别 exports和module.exports 覆盖 其它... 简介及资料 通过N ...

  2. 【BZOJ4542】大数(莫队)

    题意:给定一个N位的由[0..9]组成的数字串和质数P,有M次不强制在线的询问,每次询问区间[l,r]中模P意义下为0的子串个数 N,M<=2e5,P<=1e10 思路:一次A,本来还以为 ...

  3. Eclipse与MyEclipse修改注释字体颜色

    修改配置路劲 Window--->Preferences--->Java--->Editor--->Syntax Coloring--->Element--->Co ...

  4. BootLoader的一些知识

    在嵌入式操作系统中,BootLoader是在操作系统内核运行之前运行.可以初始化硬件设备.建立内存空间映射图,从而将系统的软硬件环境带到一个合适状态,以便为最终调用操作系统内核准备好正确的环境.在嵌入 ...

  5. Codeforces 935D Fafa and Ancient Alphabet

    题目链接 题意 给定两个\(n\)位的\(m\)进制数\(s1,s2\),所有出现的\(0\)均可等概率地被其他数字替换,求\(s1\gt s2\)的概率. 思路 从高位到低位,根据每一位上相应的\( ...

  6. Codeforces Round #441 Div. 2 A B C D

    题目链接 A. Trip for Meal 题意 三个点之间两两有路径,分别长为\(a,b,c\),现在从第一个点出发,走\(n-1\)条边,问总路径最小值. 思路 记起始点相邻的边为\(a,b\), ...

  7. 02深入理解C指针之---指针类型和值

    该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 1.指针的类型: 可以在声明指针时,指定指针的类型,例如: (1)void *x  声 ...

  8. 知乎上的一些文章---leetcode【笔记1】

    张土汪 http://github.com/shawnfan Java{script}代码仔 42 人赞同 [1.19.2017] 更新: 2017年1月17日, 陪我征战多年的 2014 MackB ...

  9. hdu 2685(数论相关定理+欧几里德定理+快速取模)

    I won't tell you this is about number theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  10. AC日记——仙岛求药 openjude 2727

    仙岛求药 思路: bfs: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> #inc ...