XML 配置

以 Spring XML 开发 Dubbo 应用

Dubbo 有基于 Spring Schema 扩展的自定义配置组件,使用 XML 能达到的配置能力总体与 配置参考手册 对等。

以下内容的完整示例请参考 dubbo-samples

服务提供者

定义服务接口

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 DemoServiceImpl implements DemoService {
  2. @Override
  3. public String sayHello(String name) {
  4. System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name +
  5. ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
  6. return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
  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/>
  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 BasicConsumer {
  2. public static void main(String[] args) {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-demo-consumer.xml");
  4. context.start();
  5. DemoService demoService = (DemoService) context.getBean("demoService");
  6. String hello = demoService.sayHello("world");
  7. System.out.println(hello);
  8. }
  9. }

最后修改 December 16, 2022: Fix check (#1736) (97972c1)