配置

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. JavaScript 笔记(7) -- 在HTML中嵌入 js (外部引用)

    本节主要说明,在HTML中嵌入自定义 JavaScript.通过HTML的script标签加载JavaScript文件 为防止网页加载缓慢,也可以把非关键的JavaScript放到网页底部,例如下面的 ...

  2. Use ASP.NET and DotNetZip to Create and Extract ZIP Files

    原文发布时间为:2011-02-16 -- 来源于本人的百度文章 [由搬家工具导入] Published: 11 Feb 2011By: Scott MitchellDownload Sample C ...

  3. 【Vue起步-Windows】N01:环境安装

    本文基于“vue.js安装过程(npm安装)”文章内容及个人出现的问题整合而成. 1.安装npm环境 在Node官网中下载最新的windows版msi安装包,并默认所有安装选择. 2.查看npm安装版 ...

  4. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---26

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  5. linux内核情景分析之内核中的互斥操作

    信号量机制: struct sempahore是其结构,定义如下 struct semaphore { atomic_t count;//资源数目 int sleepers;//等待进程数目 wait ...

  6. jmeter登录测试

    测试步骤: 1.测试计划--右键添加--Threads--线程组 2. 线程组--右键--http信息头管理器 输入Content-Type=application/json,表示接口请求的默认设置: ...

  7. 转:C#制作ORM映射学习笔记一 自定义Attribute类

    之前在做unity项目时发现只能用odbc连接数据库,感觉非常的麻烦,因为之前做web开发的时候用惯了ORM映射,所以我想在unity中也用一下ORM(虽然我知道出于性能的考虑这样做事不好的,不过自己 ...

  8. 第十二届北航程序设计竞赛决赛网络同步赛 J题 两点之间

    题目链接  Problem J 这道题思路还是很直观的,但是有两个难点: 1.题目中说$1<=NM<=10^{6}$,但没具体说明$N$和$M$的值,也就是可能出现: $N = 1, M ...

  9. 从客户端(ExportContent="...ontinuous <br />Pass Count":13...")中检测到有潜在危险的 Request.Form 值。

    mvc中,用chrome浏览器导出excel提示如题错误的解决办法. <system.web> <httpRuntime requestValidationMode="2. ...

  10. 模型搭建练习1_用numpy和tensor、variable实现前后向传播、实现激活函数

    用numpy实现搭建一个简单的forward和backward import numpy as np N, D_in, H, D_out = 64, 1000, 100, 10 x = np.rand ...