配置

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. PAT 天梯赛 L1-043 阅览室

    L1-043. 阅览室 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者 ...

  2. POJ3983 快算24

    很正常的题目,迷一样的答案. 测试数据只有一组,对没错只有一组. #include<cstdio> int main() { printf("5*(5-(1/5))\n" ...

  3. git ssh 生成步骤

    Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置. github的SSH配置如下: 一 . 设置Git的user name和email: $ git ...

  4. APP漏洞扫描用地址空间随机化【转】

    转自:http://www.cnblogs.com/alisecurity/p/6141575.html 前言 我们在前文<APP漏洞扫描器之本地拒绝服务检测详解>了解到阿里聚安全漏洞扫描 ...

  5. Codeforces 546D Soldier and Number Game(数论)

    类似筛素数的方法……求出前缀和.然后直接O(1)回答即可. #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) ...

  6. 转:Java多线程学习(总结很详细!!!)

    Java多线程学习(总结很详细!!!) 此文只能说是java多线程的一个入门,其实Java里头线程完全可以写一本书了,但是如果最基本的你都学掌握好,又怎么能更上一个台阶呢? 本文主要讲java中多线程 ...

  7. 向现有mvc程序中加入devexpress report

    Open your ASP.NET MVC project. In the main menu of Visual Studio, click the DEVEXPRESS submenu and s ...

  8. Maven出现错误No plugin found for prefix 'jetty' in the current project and in the plugin groups的问题解决

    只需在maven的setting.xml文件上加入如下节点: <pluginGroups> <pluginGroup>org.mortbay.jetty</pluginG ...

  9. js 拦截全局 ajax 请求

    你是否有过下面的需求:需要给所有ajax请求添加统一签名.需要统计某个接口被请求的次数.需要限制http请求的方法必须为get或post.需要分析别人网络协议等等,那么如何做?想想,如果能够拦截所有a ...

  10. [深入浅出iOS库]之数据库 sqlite

    一,sqlite 简介 前面写了一篇博文讲如何在 C# 中使用 ADO 访问各种数据库,在移动开发和嵌入式领域也有一个轻量级的开源关系型数据库-sqlite.它的特点是零配置(无需服务器),单磁盘文件 ...