XML Configuration

Developing Dubbo applications with Spring XML

Dubbo has a custom configuration component based on Spring Schema, and the configuration options supported by XML correspond to those described in the configuration reference manual. Please refer to the examples used in this article at dubbo-samples-spring-xml.

Complete XML Example

Service Provider

Define Service Interface

DemoService.java:

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

Implement Interface in Service Provider

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. }

Declare Service Exposure with Spring Configuration

  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>

Load Spring Configuration

  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. }

Service Consumer

Reference Remote Service through Spring Configuration

  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>

Load Spring Configuration and Call Remote Service

  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. }

More Examples

Version and Group

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

Cluster Fault Tolerance

Configure failover retry count:

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

Multiple Protocols

  1. <dubbo:application name="world" />
  2. <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
  3. <!-- Multiple protocol configuration -->
  4. <dubbo:protocol name="dubbo" port="20880" />
  5. <dubbo:protocol name="rmi" port="1099" />
  6. <!-- Use dubbo protocol to expose service -->
  7. <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
  8. <!-- Use rmi protocol to expose service -->
  9. <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />

Multiple Registries

  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. <!-- Multiple registry configuration -->
  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. <!-- Register with Chinese registry -->
  11. <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
  12. <!-- Register with international registry -->
  13. <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
  14. </beans>

Global Default Values

Specify global default timeout, effective for all services:

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

Based on group default values:

  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>

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)