Dubbo and Spring Integrated demo

Source: Internet
Author: User

Zookeeper Installation and startup
    • http://mirrors.hust.edu.cn/apache/zookeeper/download, my version is 3.4.5.

    • Unzip to D:\zookeeper-3.4.5

    • Configuration

      To the directory conf create the Zoo.cfg file, the default is to load this file, the contents of the file I copy directly inside the sample

      Zoo.cfg's Content
      # Heartbeat Check Time 2 seconds
      ticktime=2000
      # Number of connections to server-side interval initialized, total time 10*2=20 seconds
      initlimit=10
      # The number of communications between ZK Leader and follower, total time 5*2=10 seconds
      Synclimit=5
      # Store the location of the database snapshot in memory, and if you do not set the parameter, the update transaction log will be stored in the default location.
      Datadir=d:\zk\tmp\zookeeper
      # where error logs are stored
      Datalogdir=d:\zk\logs\zookeeper
      # ZK Server-side listening port
      clientport=2181

    • Run

      The CD then executes the Zkserver.cmd in the bin directory and the boot succeeds. Use the JPS command-line command to verify that the zookeeper started successfully.

Dubbo Service Registration
建立WEB项目,引入相应Jar文件。

配置web.xml文件
<?xml version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://java.sun.com/xml/ns/javaee"xmlns:web="Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"Id="WEBAPP_ID"version="3.0" ><Display-name>dubbo-service</Display-name><Welcome-file-list><Welcome-file>index.html</Welcome-file><Welcome-file>index.htm</Welcome-file><welcome-file>index.jsp</Welcome-file><Welcome-file>default.html</Welcome-file><Welcome-file>default.htm</Welcome-file><welcome-file>default.jsp</Welcome-file></Welcome-file-list><Context-param><Param-name>contextconfiglocation</Param-name><Param-value> Web-inf/dubbo-provider.xml</Param-value></Context-param><Listener><Listener-class>org.springframework.web.context.contextloaderlistener</Listener-class></Listener><Filter><Filter-name>characterencodingfilter</Filter-name><Filter-class>org.springframework.web.filter.characterencodingfilter</Filter-class><Init-param><Param-name>encoding</Param-name><Param-value>utf-8</Param-value></Init-param><Init-param><Param-name>forceencoding</param-name> <param-value>true </param-value> </init-param> </filter> <filter-mapping> <filter-name>characterencodingfilter</ filter-name> <url-pattern>/* </url-pattern> </ Filter-mapping></WEB-APP>      

写服务类:接口:
public Span class= "Hljs-class" >interface idemoservice {/** * * * @Function: Com.test.dubbo.service.IDemoService.sayHello * @Description: * * @param name * @return Name String * * @version: v1.0 * @author: CJQ * @date: 2015-4-30 pm 5:45:30 * * Modification History: * Date Author Version Description *--------- --------------------------------------------------------* 2015-4-30 CJQ v1.0.0 Create */ public string sayhello (string name);         

实现:
public class DemoServiceImpl implements IDemoService { /* * (non-Javadoc) * * @see com.test.dubbo.service.IDemoService#sayHello(java.lang.String) */ public String sayHello(String name) { return "Hello Dubbo,Hello " + name; }}

注册服务:新建dubbo-provider.xml文件,文件内容为:
<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="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 "><!--provider application information for computing dependencies--<Dubbo:applicationName="Dubbo-service"/><!--use the multicast broadcast registry to expose the service address--<!--<dubbo:registry address= "multicast://224.5.6.7:1234"/>--<!--Use the Zookeeper registry to expose the service address--<Dubbo:registryaddress="Zookeeper://xxxx:2181"/><!--exposed at 20880 ports with the Dubbo protocol--< dubbo:protocol name= "Dubbo" port= "20880"/> <!--declares service interfaces that need to be exposed --<dubbo:service interface=  "Com.test.dubbo.service.IDemoService" ref=  "Demoservice"/> <!--and local beans as a service--<bean id=" Demoservice "class=" Com.test.dubbo.service.impl.DemoServiceImpl "/></beans >                 

部署启动服务:

Dubbo Service Invocation
新建WEB或者普通JAVA工程,引入Dubbo服务注册项目中的jar文件。需要导入一个服务接口(从上个程序中导入接口的jar并且导入到此项目中。)配置web.xml文件(和上工程一样),编写调用配置文件dubbo-consumer.xml文件,内容如下:
<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="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 "><!--provider application information for computing dependencies--<Dubbo:applicationname="Dubbo-service-consumer"/> <!--use multicast broadcast registry to expose service addresses- <!--<dubbo: Registry address= "multicast://224.5.6.7:1234"/>- <!--using zookeeper registry to expose service addresses- <Dubbo : Registry address="zookeeper://xxxx:2181"/> <!--declaration of service interfaces to be exposed-- <dubbo:reference id= "demoservice" interface="Com.test.dubbo.service.IDemoService"/></Beans >                 

编写测试代码:
/** * Copyright (C) 2015 * * *@className: Com.test.dubbo.consumer.ConsumerTest *@description: TODO * *@version: v1.0.0 *@author: CJQ * * Modification History: * Date author Version Description *----------------------------------------------- ------------------* 2015-4-30 CJQ v1.0.0 Create * * * *Package Com.test.dubbo.consumer;Import Org.springframework.context.support.ClassPathXmlApplicationContext;import Com.test.dubbo.service.IDemoService; public class consumertest { public static void main (string[] args) { Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ( "file:d:/javaworkspace/ Dubbo-service-consumer/webcontent/web-inf/dubbo-consumer.xml "); Context.start (); Idemoservice Demoservice = (idemoservice) context.getbean ("Demoservice"); //Get remote service proxy String hello = Demoservice.sayhello ("World"); //Execute remote method System.out.println (hello);}}             

Dubbo Service Invocation Results
调用测试代码,执行结果为:

Dubbo Introduction End
如果你在做分布式系统的话,不妨用一下这个技术。

Dubbo and Spring Integrated demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.