The project team uses distributed services with dozens of applications on the line, and RPC calls rely entirely on Dubbo. Peacetime development has always been with other people to set up a good Dubbo environment, the recent time of their own independent build Dubbo small demo, a server, a client.
Service SideService side MAVEN parent Projectfirst, build a MAVEN parent project that introduces Dubbo and spring dependencies, and Dubbo can seamlessly integrate 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 the dependent packages that must be used to develop the Dubbo server. Server Interface sub-moduleThis module is responsible for applying the definition of exposed interfaces to third parties and implementing them in other packages. The third party application needs to introduce this package, but cannot see the implementation of the interface. This module is very simple, just provide the interface definition.
Package Org.dubbo.server.api;public interface Demoservice {public string SayHello (string name); }
server-side Interface Implementation sub-modulerecreate a maven module, which is responsible for implementing the interface defined by the above module. The implementation 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 demoservice{ public string SayHello (string name) { return "Hello" + Name; } }
At the same time, the server should be deployed as a standalone 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 Co ntext = new Classpathxmlapplicationcontext (new string[] {"Applicationcontext.xml"}); Context.start (); System.in.read (); To ensure that the service is always open, use the blocking of the input stream to simulate } }
Well, the service is running up. Service-side Dubbo configuration
<?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/SC Hema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd Http://www.springframework.org/schema/cont Ext http://www.springframework.org/schema/context/spring-context.xsd "default-autowire=" ByName "><context: Component-scan base-package= "org.dubbo.server.service.**"/><!--consumer app name, used to compute dependencies, not match criteria, and not as a provider-->< Dubbo:application name= "Hehe_consumer" organization= "risk.im.jd.com" owner= "Lvsheng"/><!-- Use zookeeper Registry to expose 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= "/><dubbo:provider id=" Im.riskctrl.dubbo.provider "timeout=" "retries=" 5 "loadbalance=" Roundrobin "cluster=" Failover "registry=" Registry "></dubbo:provider><!--generate a remote service proxy that can be used like a local bean Demoservice--><dubbo:service interface = "Org.dubbo.server.api.DemoService" ref= "Demoservice" provider= "Im.riskctrl.dubbo.provider" ></dubbo: Service></beans>
Registration CenterDubbo Registration Center I use the Dubbo website of the Registration center demo. ClientThe client can simply build a common MAVEN project. The pom dependency is consistent with the service side. The calling code for the client 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 ();D emoservice demoservice = Context.getbean ("Demoservice", Demoservice.class); System.out.println (Demoservice.sayhello ("AAA")); System.in.read ();}}
the client's spring configuration 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/SC Hema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd Http://www.springframework.org/schema/cont Ext http://www.springframework.org/schema/context/spring-context.xsd "default-autowire=" byname "><!--consumer app Name , which is used to calculate dependencies, not match criteria, and do not--><dubbo:application name= "Dubbo_consumer" organization= "risk.im.com" owner= "in the same way as the provider Lvsheng "/><!--use zookeeper registry to expose the 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= "/><dubbo:provider" id= "Im.riskctrl.dubbo.provider" timeout= "5" retries= "loadbalance=" Roundrobin "cluster=" Failover "registry=" registry "></dubbo:provider><!--to generate a remote service proxy, You can use Demoservice--><dubbo:reference interface= "Org.dubbo.server.api.DemoService" id= "Demoservice" as you would with a local bean "Registry=" ></dubbo:reference></beans> "Registry"
The whole project is ready to run.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dubbo Server/Client Demo