XML 配置

以 Spring XML 开发 Dubbo 应用

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

XML完整示例

服务提供者

定义服务接口

DemoService.java:

  1. package org.apache.dubbo.demo;
  2. public interface DemoService {
  3. String sayHello(String name);
  4. }

在服务提供方实现接口

DemoServiceImpl.java:

  1. package org.apache.dubbo.demo.provider;
  2. import org.apache.dubbo.demo.DemoService;
  3. public class DemoServiceImpl implements DemoService {
  4. public String sayHello(String name) {
  5. return "Hello " + name;
  6. }
  7. }

用 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/>
  8. <dubbo:application name="demo-provider"/>
  9. <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
  10. <dubbo:provider token="true"/>
  11. <bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
  12. <dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
  13. </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();
  5. System.out.println("dubbo service started");
  6. // to hang up main thread
  7. new CountDownLatch(1).await();
  8. }
  9. }

服务消费者

通过 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/>
  8. <dubbo:application name="demo-consumer"/>
  9. <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
  10. <dubbo:reference id="demoService" check="true" interface="org.apache.dubbo.samples.basic.api.DemoService"/>
  11. </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");
  6. String message = greetingsService.sayHi("dubbo");
  7. System.out.println("Receive result ======> " + message);
  8. System.in.read();
  9. System.exit(0);
  10. }
  11. }

更多示例

版本与分组

  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" />
  2. <dubbo:reference>
  3. <dubbo:method name="findFoo" retries="2" />
  4. </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>
  5. <dubbo:provider timeout="8000">
  6. <dubbo:service interface="com.alibaba.hello.api.DemoService" ref="demoService"/>
  7. <dubbo:service interface="com.alibaba.hello.api.DemoService2" ref="demoService2"/>
  8. </dubbo:provider>

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