The most common way to use Dubbo is to run it in Spring framework. The following content will guide you to develop a Dubbo application with Spring framework's XML configuration.

Dubbo Documentation

If you don't want to rely on Spring, you can try using API configuration.

First let's create a root directory called dubbo-demo:

  1. mkdir dubbo-demo
  2. cd dubbo-demo

Next, we are going to create 3 sub-directories under root directory:

  • dubbo-demo-api: the common service api
  • dubbo-demo-provider: the demo provider codes
  • dubbo-demo-consumer: the demo consumer codes

Service provider

Defining service interfaces

DemoService.java [1]:

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

The proejct structure should look like this:

  1. .
  2. ├── dubbo-demo-api
  3. ├── pom.xml
  4. └── src
  5. └── main
  6. └── java
  7. └── org
  8. └── apache
  9. └── dubbo
  10. └── demo
  11. └── DemoService.java

Implement interface in service provider

DemoServiceImpl.java [2]:

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

Exposing service with Spring configuration

provider.xml:

  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"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  6. http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  7.  
  8. <!-- provider's application name, used for tracing dependency relationship -->
  9. <dubbo:application name="demo-provider"/>
  10. <!-- use zookeeper registry center to export service -->
  11. <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  12. <!-- use dubbo protocol to export service on port 20880 -->
  13. <dubbo:protocol name="dubbo" port="20880"/>
  14. <!-- service implementation, as same as regular local bean -->
  15. <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
  16. <!-- declare the service interface to be exported -->
  17. <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
  18. </beans>

The demo uses multicast as the registry since it is simple and does not require to extra installation. If you prefer a registiry like zookeeper, please check out examples here.

Configure the logging system

Dubbo use log4j as logging system by default, it also support slf4j, Apache Commons Logging, and JUL logging.

Following is a sample configuration:

log4j.properties

  1. ###set log levels###
  2. log4j.rootLogger=info, stdout
  3. ###output to the console###
  4. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  5. log4j.appender.stdout.Target=System.out
  6. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

Bootstrap the service provider

Provider.java

  1. package org.apache.dubbo.demo.provider;
  2.  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4.  
  5. public class Provider {
  6.  
  7. public static void main(String[] args) throws Exception {
  8. System.setProperty("java.net.preferIPv4Stack", "true");
  9. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
  10. context.start();
  11. System.out.println("Provider started.");
  12. System.in.read(); // press any key to exit
  13. }
  14. }

Finally, the project structure should look like this:

  1. ├── dubbo-demo-provider
  2. ├── pom.xml
  3. └── src
  4. └── main
  5. ├── java
  6. └── org
  7. └── apache
  8. └── dubbo
  9. └── demo
  10. └── provider
  11. ├── DemoServiceImpl.java
  12. └── Provider.java
  13. └── resources
  14. ├── META-INF
  15. └── spring
  16. └── dubbo-demo-provider.xml
  17. └── log4j.properties

Service consumer

Complete installation steps, see:Consumer demo installation

Using the Spring configuration to reference a remote service

consumer.xml:

  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"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  6. http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  7.  
  8. <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
  9. don't set it same as provider -->
  10. <dubbo:application name="demo-consumer"/>
  11. <!-- use zookeeper registry center to discover service -->
  12. <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  13. <!-- generate proxy for the remote service, then demoService can be used in the same way as the
  14. local regular interface -->
  15. <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
  16. </beans>

Bootstrap the consumer

Consumer.java:

  1. package org.apache.dubbo.demo.consumer;
  2.  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.apache.dubbo.demo.DemoService;
  5. public class Consumer {
  6. public static void main(String[] args) throws Exception {
  7. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-consumer.xml"});
  8. context.start();
  9. // Obtaining a remote service proxy
  10. DemoService demoService = (DemoService)context.getBean("demoService");
  11. // Executing remote methods
  12. String hello = demoService.sayHello("world");
  13. // Display the call result
  14. System.out.println(hello);
  15. }
  16. }

Config the logging system

This is the same as how to config it on provider side.

Finally, the project structure should be look like this:

  1. ├── dubbo-demo-consumer
  2. ├── pom.xml
  3. └── src
  4. └── main
  5. ├── java
  6. └── org
  7. └── apache
  8. └── dubbo
  9. └── demo
  10. └── consumer
  11. └── Consumer.java
  12. └── resources
  13. ├── META-INF
  14. └── spring
  15. └── dubbo-demo-consumer.xml
  16. └── log4j.properties

Start the demo

Start service provider

Run the org.apache.dubbo.demo.provider.Provider class to start the provider.

Start service consumer

Run the org.apache.dubbo.demo.provider.Consumer class to start the consumer, and you should be able to see the following result:

  1. Hello world

Complete example

You can find the complete example code in the Github repository.

本文已通过「原本」原创作品认证,转载请注明文章出处及链接。

Java最后更新:2022-11-20
夏日阳光
  • 本文由 夏日阳光 发表于 2018年11月24日
  • 本文为夏日阳光原创文章,转载请务必保留本文链接:https://www.pieruo.com/26.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证
加载中...