dubbo(2.7.3) 8.XML配置

简介: dubbo(2.7.3) 8.XML配置

XML 配置的方式来配置你的 Dubbo 应用

有关 XML 的详细配置项,请参见:配置参考手册[1]。如果不想使用 Spring 配置,而希望通过 API 的方式进行调用,请参见:API 配置[2]。想知道如何使用配置,请参见:快速启动[3]

请在此查看文档描述的完整示例[4]

provider.xml 示例

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo-provider"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:protocol name="dubbo" port="20890"/>
    <bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
    <dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
</beans>

consumer.xml 示例

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demo-consumer"/>
    <dubbo:registry group="aaa" address="zookeeper://127.0.0.1:2181"/>
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
</beans>

所有标签都支持自定义参数,用于不同扩展点实现的特殊配置,如:

<dubbo:protocol name="jms">
    <dubbo:parameter key="queue" value="your_queue" />
</dubbo:protocol>

或: 1[5]

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:protocol name="jms" p:queue="your_queue" />
</beans>

配置之间的关系

image.png

标签 用途 解释
<dubbo:service/> 服务配置 用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心
<dubbo:reference/>2[6] 引用配置 用于创建一个远程服务代理,一个引用可以指向多个注册中心
<dubbo:protocol/> 协议配置 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受
<dubbo:application/> 应用配置 用于配置当前应用信息,不管该应用是提供者还是消费者
<dubbo:module/> 模块配置 用于配置当前模块信息,可选
<dubbo:registry/> 注册中心配置 用于配置连接注册中心相关信息
<dubbo:monitor/> 监控中心配置 用于配置连接监控中心相关信息,可选
<dubbo:provider/> 提供方配置 当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选
<dubbo:consumer/> 消费方配置 当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选
<dubbo:method/> 方法配置 用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息
<dubbo:argument/> 参数配置 用于指定方法参数配置

不同粒度配置的覆盖关系

以 timeout 为例,下图显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似:

  • 方法级优先,接口级次之,全局配置再次之。
  • 如果级别一样,则消费方优先,提供方次之。

其中,服务提供方配置,通过 URL 经由注册中心传递给消费方。

image.png

(建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的超时设置)。

理论上 ReferenceConfig 中除了interface这一项,其他所有配置项都可以缺省不配置,框架会自动使用 ConsumerConfig,ServiceConfig, ProviderConfig 等提供的缺省配置。

参考资料

[1]

配置参考手册: https://cn.dubbo.apache.org/zh-cn/docsv2.7/user/references/xml

[2]

API配置: https://cn.dubbo.apache.org/zh-cn/docsv2.7/user/configuration/api

[3]

快速启动: https://cn.dubbo.apache.org/zh-cn/docsv2.7/user/quick-start

[4]

完整示例: https://github.com/apache/dubbo-samples/tree/master/2-advanced/dubbo-samples-spring-xml

[5]

1: #fn:1

[6]

2: #fn:2


相关文章
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
13 1
|
4月前
|
存储 Java 测试技术
JAVA-MAVEN初学者教程(配置、pom.xml、依赖管理等)
JAVA-MAVEN初学者教程(配置、pom.xml、依赖管理等)
241 0
|
1月前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1
|
1月前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
10 0
|
1月前
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
7 0
|
1月前
|
XML Java Apache
Apache Flink自定义 logback xml配置
Apache Flink自定义 logback xml配置
152 0
|
2月前
|
Java
logback配置,命名为logback-spring.xml
logback配置,命名为logback-spring.xml
|
2月前
|
XML Java 测试技术
【SpringBoot】基于 Maven 的 pom.xml 配置详解
【SpringBoot】基于 Maven 的 pom.xml 配置详解
233 0
【SpringBoot】基于 Maven 的 pom.xml 配置详解
|
2月前
|
负载均衡 Dubbo NoSQL
Dubbo配置文件解密:从dubbo-consumer.xml到dubbo-provider.xml一网打尽【十】
Dubbo配置文件解密:从dubbo-consumer.xml到dubbo-provider.xml一网打尽【十】
43 0
|
2月前
|
XML 负载均衡 Dubbo
了解Dubbo配置:优先级、重试和容错机制的秘密【五】
了解Dubbo配置:优先级、重试和容错机制的秘密【五】
37 0