The simplest example of Dubbo

Source: Internet
Author: User

Use the simplest form of multicast to write Dubbo small examples

Create two new Maven WebApp projects, one on the server side and one for the client.

Server side The structure of this project is as follows:


Add a dependency jar package to the POM first

Pom.xml content is as follows:

<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>dubbo_server</groupId> <artifactid>dubbo_server</ artifactid> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name> Dubbo_server Maven webapp</name> <url>http://maven.apache.org</url> <dependencies> <d ependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <versio
          n>3.8.1</version> <scope>test</scope> </dependency> <dependency>
          <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactid>dubbo&
          lt;/artifactid> <version>2.5.3</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <v ersion>3.18.1-ga</version> </dependency> <dependency> <groupid>log4j</
          Groupid> <artifactId>log4j</artifactId> <version>1.2.15</version>
                  <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId>
                  <artifactId>jmxtools</artifactId> </exclusion> <exclusion>
              <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <artifactId>jms</artifactId> <g roupid>javax.jms</groupid> </exclusion> <exclusion> &lt ;artifactid>mail</artifactid> <groupId>javax.mail</groupId> &LT;/EXCL usion> </exclusions> </dependency> <dependency> <groupid>org.sp Ringframework</groupid> <artifactId>spring</artifactId> &LT;VERSION&GT;2.5.6.SEC03&L
          t;/version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> &LT;/DEPENDENCY&G
      T <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-log4j12</artifa
      Ctid>    <version>1.6.1</version> </dependency> <dependency> <groupid>org.a Pache.zookeeper</groupid> <artifactId>zookeeper</artifactId> <version>3.4.5&lt ;/version> <type>pom</type> </dependency> <dependency> <grou Pid>com.101tec</groupid> <artifactId>zkclient</artifactId> <version>0.4< /version> </dependency> </dependencies> <build> <finalname>dubbo_server</fina lname> </build> <properties> <project.build.sourceencoding>utf-8</project.build.so
 Urceencoding> </properties> </project>

Write an interface and an implementation class

Package com.lhx.service;

/**
 * Created by Lhx on 14-11-19.
 *
/public interface Sayhellotoclient {public

    string SayHello (String hello);
}

Implementation class:

Package Com.lhx.service.impl;

Import com.lhx.service.SayHelloToClient;

/**
 * Created by Lhx on 14-11-19.
 *
/public class Sayhellotoclientimpl implements sayhellotoclient{public

    string SayHello (string hello) {
        System.out.println ("I received:" + hello);
        return Hello + "You are also good ... " ;
    }

}

Configuration file Applicationprovider.xml

To configure the bean inside, the contents are 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 "
       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
        ">


    <!--app name--
    <dubbo:application name=" Hello-world "/>

    <!--registered Address
    --<dubbo:registry address= "multicast://224.5.6.7:1234"/>

    < location of the!--interface--
    <dubbo:service interface= "com.lhx.service.SayHelloToClient"
                   ref= "Demoservice" executes= "/>"


    <!--implement beans, the bean for the client application is based on this ID name--
    <bean id= "Demoservice" class= " Com.lhx.service.impl.SayHelloToClientImpl "/>

</beans>

Write the main function, start the service by:

Package com.lhx.main;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Lhx on 14-11-19.
 *
/public class Mymain {public

    static void Main (string[] args) throws Exception {
        Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (
                new string[] {" Applicationprovider.xml "});

        Context.start ();
        System.out.println ("Press any key to exit");
        System.in.read ();

    }
}


Server side is finished, now to write the client program

The directory structure of the client side is as follows:

The content inside the Pom.xml is similar to the server side

<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>dubbo_client</groupId> <artifactid>dubbo_client</ artifactid> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name> Dubbo_client Maven webapp</name> <url>http://maven.apache.org</url> <properties> & Lt;project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependenci es> <dependency> <groupId>junit</groupId> <artifactId>junit< /artifactid> <version>3.8.1</version> <scope>test</scope> </ Dependency> <dependency> &Lt;groupid>commons-logging</groupid> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupid>co M.alibaba</groupid> <artifactId>dubbo</artifactId> <version>2.5.3</vers
            ion> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> < /dependency> <dependency> <groupId>log4j</groupId> <artifactId>
                Log4j</artifactid> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> &LT;ARTIFACTID&GT;JM Xtools</artifactid>
                    </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion&
                    Gt <artifactId>jms</artifactId> <groupId>javax.jms</groupId> <
                    /exclusion> <exclusion> <artifactId>mail</artifactId>
        <groupId>javax.mail</groupId> </exclusion> </exclusions>
            </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </depend ency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</artifactid> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> &LT;ARTIFACTID&GT;SLF4J-LOG4J12 </artifactId> <version>1.6.1</version> </dependency> <dependency&gt
            ; <groupId>com.github.adyliu</groupId> <artifactId>zkclient</artifactId> &lt ;version>2.0</version> </dependency> <dependency> <groupid>org.apac He.zookeeper</groupid> <artifactId>zookeeper</artifactId> &LT;VERSION&GT;3.4.5&L
            t;/version> <type>pom</type> </dependency> <dependency> <groupId>dubbo_server</groupId> <artifactId>dubbo_server</artifactId> &lt ; VERSION&Gt;1.0-snapshot</version> </dependency> </dependencies> <build> <finalname&
 Gt;dubbo_client</finalname> </build> </project>

The following is a bit different from the server-side pom.xml inside to find content

<dependency>
            <groupId>dubbo_server</groupId>
            <artifactid>dubbo_server</ artifactid>
            <version>1.0-SNAPSHOT</version>
        </dependency>

Writing a client-side calling program

Package com.lhx.client;

Import com.lhx.service.SayHelloToClient;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Lhx on 14-11-19.
 *
/public class MyClient {public
    void SayHello () {
        Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (
                new string[] {"Applicationconsumer.xml"});

        Context.start ();

        Get the server over there bean
        sayhellotoclient demoservice = (sayhellotoclient) context.getbean ("Demoservice");

        System.out.println (Demoservice.sayhello ("Lisi"));
    }
}

Writing Client main functions

Package com.lhx.test;

Import com.lhx.client.MyClient;

/**
 * Created by Lhx on 14-11-19.
 *

/public class apptest{public
    static void Main (string[] args) {
        myclient myclient = new MyClient ();
        Myclient.sayhello ();
    }
}

Last Configuration Applicationconsumer.xml

<?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
        ">

    <!--Consumer application name--
    <dubbo:application name=" Consumer-of-helloworld-app "/>

    <!--registry address, used for consumer to discover services--
    < Dubbo:registry address= "multicast://224.5.6.7:1234"/>
    <dubbo:consumer timeout= "/> <!-"
    -which service to consume? --
    <dubbo:reference id= "Demoservice" interface= "com.lhx.service.SayHelloToClient"/> </
Beans>

Start the server-side main function first

Start the main function of the client again

The server side receives the parameter and knows how to invoke the method in that class. The following are shown:

The server has finished processing the program, the client side received the server-side processing results. It's kind of like webservice.

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.