Dubbo Server/Client demo

Posted by tomfra on Fri, 10 May 2019 17:53:33 +0200

               

    The project group uses distributed services and has dozens of applications online. RPC calls rely entirely on Dubbo. Usually the development has been built with other people's Dubbo environment. Recently, I have taken time to build a small Dubbo demo, a server and a client independently.

One server

Server-side maven parent project

First, we build a maven parent project, introducing dubbo and spring dependencies, dubbo can be seamlessly integrated with spring.
<properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <v.dubbo.ext>1.3.0-SNAPSHOT</v.dubbo.ext>  <v.spring>3.1.2.RELEASE</v.spring>  <v.plugin.jar>2.3.1</v.plugin.jar> </properties> <dependencies>  <dependency>   <groupId>com.jd.dubbo.ext</groupId>   <artifactId>dubbo-ext-spi</artifactId>   <version>${v.dubbo.ext}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-core</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-beans</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context-support</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-tx</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-orm</artifactId>   <version>${v.spring}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-web</artifactId>   <version>${v.spring}</version>  </dependency> </dependencies>
These are all dependency packages that must be used to develop dubbo servers.

Server Interface Submodule

This module is responsible for exposing interface definitions to third parties and implementing them in other packages. The third part application needs to introduce this package, but it cannot see the implementation of the interface.
This module is very simple and only needs to provide interface definitions.
package org.dubbo.server.api;public interface DemoService public String sayHello(String name);  }

Server Interface Implementation Submodule

Create a new maven module, which is responsible for implementing the interface defined by the module above.
The realization is as follows:
package org.dubbo.server.service;import org.dubbo.server.api.DemoService;import org.springframework.stereotype.Service;@Service("demoService")public class DemoServiceImpl implements DemoServicepublic String sayHello(String name) {           return "Hello " + name;    }  }
At the same time, the server should be deployed as a stand-alone application to handle client requests.
package org.dubbo.server.service;import org.springframework.context.support.ClassPathXmlApplicationContext;public class DubboServer public static void main(String[] args) throws Exception {          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});          context.start();          System.in.read(); // In order to keep the service open, the blocking of input stream is used to simulate.     }  }

Well, the server is running.

dubbo configuration on server side

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd          http://code.alibabatech.com/schema/dubbo          http://code.alibabatech.com/schema/dubbo/dubbo.xsd          http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd"  default-autowire="byName">   <context:component-scan base-package="org.dubbo.server.service.**" /> <!-- The consumer application name, used to calculate dependencies, is not a matching condition, not the same as the provider --> <dubbo:application name="hehe_consumer" organization="risk.im.jd.com"  owner="lvsheng" /> <!-- Use zookeeper Registry Exposure Service Address --> <dubbo:registry id="registry" address="10.28.163.15:6060" /> <dubbo:protocol id="protocol" name="dubbo" port="25000"  heartbeat="0" threadpool="cached" threads="512" /> <dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000"  retries="5" loadbalance="roundrobin" cluster="failover" registry="registry"> </dubbo:provider> <!-- Generating remote service proxies can be like using a local service proxy bean Same use demoService --> <dubbo:service interface="org.dubbo.server.api.DemoService" ref="demoService"  provider="im.riskctrl.dubbo.provider" > </dubbo:service></beans>  

2. Registry

The dubbo registry I use is the dubbo official website registry demo.

Three Clients

The client simply builds a common maven project. The pom dependency is consistent with the server side.
The client's call code is as follows:
package com.jd.lvsheng.dubbo.client.test;import org.dubbo.server.api.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Service;@Service("dubboConsumer")public class DubboConsumer public static void main(String[] args) throws Exception {  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });  context.start();  DemoService demoService = context.getBean("demoService", DemoService.class);  System.out.println(demoService.sayHello("aaa"));  System.in.read(); }}
The spring configuration of the client is as follows:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd          http://code.alibabatech.com/schema/dubbo          http://code.alibabatech.com/schema/dubbo/dubbo.xsd          http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd"  default-autowire="byName">   <!-- The consumer application name, used to calculate dependencies, is not a matching condition, not the same as the provider --> <dubbo:application name="dubbo_consumer" organization="risk.im.com"  owner="lvsheng" /> <!-- Use zookeeper Registry Exposure Service Address --> <dubbo:registry id="registry" address="10.28.163.15:6060" /> <dubbo:protocol id="protocol" name="dubbo" port="25001"  heartbeat="0" threadpool="cached" threads="512" /> <dubbo:provider id="im.riskctrl.dubbo.provider" timeout="5000"  retries="5" loadbalance="roundrobin" cluster="failover" registry="registry"> </dubbo:provider> <!-- Generating remote service proxies can be like using a local service proxy bean Same use demoService --> <dubbo:reference interface="org.dubbo.server.api.DemoService" id="demoService" registry="registry"> </dubbo:reference></beans>  
The whole project is operational.

Fourth Complete Project

Server-side code, please move https://github.com/bruce256/dubbo-server



           

Topics: Dubbo Spring Maven xml