For more information, please refer to the official address: http://alibaba.github.io/dubbo-doc-static/Home-zh.htm, no longer repeat the description, this article mainly records the detailed development integration steps.
First, the source code construction
This online a lot of tutorials, you can google/baidu, or directly download I have built well. I made changes that are compatible with JDK1.8.
Network disk download Http://pan.baidu.com/s/1sjFqt8T password: 60u7
Ii. Zookeeper Deployment (Registry Service Registration Center)
The Dubbo default configuration enables simple remote procedure calls between Provider and Consumer via the multicast registry broadcast, which does not require a registration schedule through the Registry registry, similar to the spring RMI Remoti Ng is called, but because it is not a cluster deployment, the official recommendation to use Zookeeper as a Registry registry server (which also supports Redis) is to implement the registration, discovery, and routing capabilities of the service as a distributed RPC framework.
Dubbo only adds Dubbo data nodes (such as) on the Zookeeper server and requires no additional configuration, so just install or use an existing Zookeeper server, and you can refer to the previous blog post for installation deployment of Zookeeper:/http my.oschina.net/ihanfeng/blog/525255
Third, service providers (Provider)
1. Create a Dubbo-provider project based on MAVEN management.
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4 _0_0.xsd "><modelVersion>4.0.0</modelVersion><groupId>com.hanfeng.dubbo</groupId> <artifactid>dubbo-provider</artifactid><packaging>war</packaging><version>0.0.1- snapshot</version><name>dubbo-provider maven webapp</name><url>http:// maven.apache.org</url><dependencies><dependency><groupid>org.springframework</ groupid><artifactid>spring-context</artifactid><version>4.0.8.release</version>< /dependency><dependency><groupid>com.alibaba</groupid><artifactid>dubbo</ Artifactid><version>2.5.3</version></dependency><dependency><groupid> org.apache.zookeeper</groupid><artifactid>zookeeper</artifactid><version>3.4.6</version><exclusions><exclusion>< groupid>com.sun.jmx</groupid><artifactid>jmxri</artifactid></exclusion>< exclusion><groupid>com.sun.jdmk</groupid><artifactid>jmxtools</artifactid></ Exclusion><exclusion><groupid>javax.jms</groupid><artifactid>jms</artifactid> </exclusion></exclusions></dependency><dependency><groupid>com.github.sgroschupf </groupid><artifactid>zkclient</artifactid><version>0.1</version></dependency ><dependency><groupid>com.netflix.curator</groupid><artifactid>curator-framework </artifactId><version>1.1.16</version></dependency></dependencies><build> <sourceDirectory>src</sourceDirectory><plugins><plugin><artifactId> Maven-compiler-plugin</artifactid>< version>3.1</version><configuration><source>1.8</source><target>1.8</ Target></configuration></plugin><plugin><artifactid>maven-war-plugin</artifactid ><version>2.4</version><configuration><warsourcedirectory>webroot</ warsourcedirectory><failonmissingwebxml>false</failonmissingwebxml></configuration></ Plugin></plugins></build></project>
2. Create a Test Service interface (HelloService)
Package Com.hanfeng.dubbo.provider;public interface HelloService {public string SayHello (string text);}
3. Create a Service Interface implementation class (HELLOSERVICEIMPL)
Package Com.hanfeng.dubbo.provider.impl;import Com.alibaba.dubbo.rpc.rpccontext;import Com.hanfeng.dubbo.provider.helloservice;public class Helloserviceimpl implements helloservice{@Overridepublic String SayHello (String text) {return "Hello" + text + "/n response Form provider:" + Rpccontext.getcontext (). getlocaladdr ESS ();}}
4. Integrate and configure the Dubbo Test service through spring and specify the zookeeper server address of the registry.
<?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" 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 "> <!--dubbo service provider app name --> <dubbo:application name= "Dubbo-provider" /> <!--dubbo Registration Center-->&NBSP;&NBSP;&NBSP;&NBSP;<DUBBO:REGISTRY&NBsp;address= "zookeeper://127.0.0.1:2181" /> <!--service provider Port--> <dubbo:protocol name= "Dubbo" port= "30001" /> <!--Dubbo service--> <dubbo:service interface= " Com.hanfeng.dubbo.provider.HelloService " ref=" HelloService " /> <!-- spring bean object--> <bean id= "HelloService" class= " Com.hanfeng.dubbo.provider.impl.HelloServiceImpl " /> </beans>
5. Write the console program to start the spring container, compile and package Provider.jar
Package Com.hanfeng.dubbo.test;import Org.springframework.context.support.classpathxmlapplicationcontext;public Class Providertest {@SuppressWarnings ("resource") public static void main (string[] args) throws Exception {Classpat Hxmlapplicationcontext context = new Classpathxmlapplicationcontext (new string[] {"Spring-dubbo-provider.xml"}); Context.start (); SYSTEM.OUT.PRINTLN ("Please press any key to exit"); System.in.read (); Press any key to exit}}
Iv. Service Consumer Development (Consumer)
1, the zookeeper address of registry is specified by spring configuration, and the call to Dubbo remote service is realized.
<?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" 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 "> <dubbo:application name= "Dubbo-consumer" /> <dubbo:registry address= "zookeeper://127.0.0.1:2181" /> <!-- Generate remoteService proxy, which can be used like a local bean helloservice --> <dubbo:reference interface= " Com.hanfeng.dubbo.provider.HelloService " id=" HelloService " /></beans>
2, write the call test client code, get the remote bean from the container and call.
package com.hanfeng.dubbo.test;import org.springframework.context.support.classpathxmlapplicationcontext;import com.hanfeng.dubbo.provider.helloservice;public class consumertest {@SuppressWarnings ("Resource" ) Public static void main (String[] args) throws Exception { ClassPathXmlApplicationContext context = new Classpathxmlapplicationcontext (new string[] {"Spring-dubbo-consumer.xml"}); context.start (); helloservice hellservice = (HelloService) Context.getbean ("HelloService"); // Get remote service Proxy string res = hellservice.sayhello ("World"); // Execute remote Method system.out.println ( res ); Show Call result &NBSP;&Nbsp; }}
Five, Deno test
Start the zookeeper server first, and then start the Dubbo server.
The test begins, we run Providertest first, and then run Consumertest
Output Result:
Log4j:warn No Appenders could is found for logger (org.springframework.core.env.StandardEnvironment). Log4j:warn Initialize the log4j system Properly.log4j:WARN see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more Info.hello world/n Response Form provider:192.168.5.13:30001
Login http://127.0.0.1:8080/, we found that the number of services, the number of applications, providers have corresponding data.
Source code: Link: http://pan.baidu.com/s/1sj8AyNb Password: hwd* (the above case also need source code, the last letter of the password to try it yourself)
Deployment integration of Distributed RPC Service framework based on open source Dubbo