XML 配置

以 Spring XML 开发 Dubbo 应用

Dubbo 有基于 Spring Schema 扩展的自定义配置组件,XML 支持的配置项与 配置参考手册 中描述的一一对。本文使用的示例请参考 dubbo-samples-spring-xml

XML完整示例

服务提供者

定义服务接口

DemoService.java:

1. package org.apache.dubbo.demo;

3. public interface DemoService {
4. String sayHello(String name);
5. }

在服务提供方实现接口

DemoServiceImpl.java:

1. package org.apache.dubbo.demo.provider;
2. import org.apache.dubbo.demo.DemoService;

4. public class DemoServiceImpl implements DemoService {
5. public String sayHello(String name) {
6. return "Hello " + name;
7. }
8. }

用 Spring 配置声明暴露服务

1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
4. xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6. http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
7. <context:property-placeholder/>

9. <dubbo:application name="demo-provider"/>

11. <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>

13. <dubbo:provider token="true"/>

15. <bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>

17. <dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>

19. </beans>

加载 Spring 配置

1. public class Application {
2. public static void main(String[] args) throws InterruptedException {
3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-provider.xml");
4. context.start();

6. System.out.println("dubbo service started");
7. // to hang up main thread
8. new CountDownLatch(1).await();
9. }
10. }

服务消费者

通过 Spring 配置引用远程服务

1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
4. xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6. http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
7. <context:property-placeholder/>

9. <dubbo:application name="demo-consumer"/>

11. <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>

13. <dubbo:reference id="demoService" check="true" interface="org.apache.dubbo.samples.basic.api.DemoService"/>

15. </beans>

加载 Spring 配置,并调用远程服务

1. public class Application {
2. public static void main(String[] args) throws IOException {
3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-consumer.xml");
4. context.start();
5. GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");

7. String message = greetingsService.sayHi("dubbo");
8. System.out.println("Receive result ======> " + message);
9. System.in.read();
10. System.exit(0);
11. }
12. }

更多示例

版本与分组

1. <dubbo:service interface="com.foo.BarService" version="1.0.0" />
2. <dubbo:service interface="org.apache.dubbo.example.service.DemoService" group="demo2"/>

集群容错

配置 failover 重试次数:

1. <dubbo:service retries="2" />

3. <dubbo:reference>
4. <dubbo:method name="findFoo" retries="2" />
5. </dubbo:reference>

多协议

1. <dubbo:application name="world"  />
2. <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
3. <!-- 多协议配置 -->
4. <dubbo:protocol name="dubbo" port="20880" />
5. <dubbo:protocol name="rmi" port="1099" />
6. <!-- 使用dubbo协议暴露服务 -->
7. <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
8. <!-- 使用rmi协议暴露服务 -->
9. <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />

多注册中心

1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
5. 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">
6. <dubbo:application name="world"  />
7. <!-- 多注册中心配置 -->
8. <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
9. <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
10. <!-- 向中文站注册中心注册 -->
11. <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
12. <!-- 向国际站注册中心注册 -->
13. <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
14. </beans>

全局默认值

指定全局默认超时时间,多所有服务生效:

1. <dubbo:provider timeout="5000" />
2. <dubbo:consumer timeout="5000" />

基于分组的默认值:

1. <dubbo:provider timeout="5000">
2. <dubbo:service interface="com.alibaba.hello.api.HelloService" ref="helloService"/>
3. <dubbo:service interface="com.alibaba.hello.api.HelloService2" ref="helloService2"/>
4. </dubbo:provider>

6. <dubbo:provider timeout="8000">
7. <dubbo:service interface="com.alibaba.hello.api.DemoService" ref="demoService"/>
8. <dubbo:service interface="com.alibaba.hello.api.DemoService2" ref="demoService2"/>
9. </dubbo:provider>

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)