首页 > 代码库 > dubbo最简单的例子

dubbo最简单的例子

1.新建两个maven项目,一个是服务器端的,一个是客户端的

2.服务器端pom.xml添加jar

<dependency>  
    <groupId>commons-logging</groupId>  
    <artifactId>commons-logging</artifactId>  
    <version>1.1.1</version>  
</dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </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> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <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> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> <type>pom</type> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

 3.编写一个接口和一个实现类。

package com.mu.dubbo.service;  
  
/** 
 * Created by mdt on 17-6-26. 
 */  
public interface SayHelloToClient {  
  
    public String sayHello(String hello);  
}
package com.mu.dubbo.service.impl;  
  
import com.mu.dubbo.service.SayHelloToClient;  
  
/** 
 * Created by mdt on 17-6-26.
 */  
public class SayHelloToClientImpl implements SayHelloToClient {  
  
    public String sayHello(String hello){  
        System.out.println("我接收到了:" + hello);  
        return hello + "你也好啊!!!" ;  
    }  
  
}

4.创建配置文件applicationProvider.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  
        ">
  
    <!--应用名称-->  
    <dubbo:application name="hello-world" />  
  
    <!-- 注册地址 -->  
    <dubbo:registry address="multicast://224.5.6.7:1234" />  
  
    <!-- 接口的位置 -->  
    <dubbo:service interface="com.mu.dubbo.service.SayHelloToClient" ref="demoService" executes="10" />  
  
    <!-- 实现bean,客户端应用的bean就以这个id名称为主 -->  
    <bean id="demoService" class="com.mu.dubbo.service.impl.SayHelloToClientImpl" />  
  
</beans>

5.编写主函数,启动服务用。

package com.mu.dubbo.main;  
  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
/** 
 * Created by mdt on 17-6-26. 
 */  
public class MyMain {  
  
    public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });  
  
        context.start();  
        System.out.println("按任意键退出");  
        System.in.read();  
  
    }  
}

6.客户端pom.xml。

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>  
        <groupId>junit</groupId>  
        <artifactId>junit</artifactId>  
        <version>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</artifactId>  
        <version>2.5.3</version>  
    </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>  
                <artifactId>jmxtools</artifactId>  
            </exclusion>  
            <exclusion>  
                <groupId>com.sun.jmx</groupId>  
                <artifactId>jmxri</artifactId>  
            </exclusion>  
            <exclusion>  
                <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>  
    </dependency>  
    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>slf4j-api</artifactId>  
        <version>1.7.6</version>  
    </dependency>  
    <dependency>  
        <groupId>org.slf4j</groupId>  
        <artifactId>slf4j-log4j12</artifactId>  
        <version>1.6.1</version>  
    </dependency>  
    <dependency>  
        <groupId>com.github.adyliu</groupId>  
        <artifactId>zkclient</artifactId>  
        <version>2.0</version>  
    </dependency>  
    <dependency>  
        <groupId>org.apache.zookeeper</groupId>  
        <artifactId>zookeeper</artifactId>  
        <version>3.4.5</version>  
        <type>pom</type>  
    </dependency>  
    <dependency>
        <groupId>com.mu.dubbo</groupId>  
        <artifactId>mu-dubbo-server</artifactId>  
        <version>0.0.1-SNAPSHOT</version>  
    </dependency>
</dependencies>

以下的这点不太一样,从服务端pom.xml里面拷贝。

<dependency>
    <groupId>com.mu.dubbo</groupId>  
    <artifactId>mu-dubbo-server</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
</dependency>

7.客户端调用程序。

package com.mu.dubbo.client;  
  
import com.mu.dubbo.service.SayHelloToClient;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
/** 
 * Created by mdt on 17-6-26.
 */  
public class MyClient {
    
    public void sayHello(){  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });  
  
        context.start();  
  
        //获取服务器那边的bean  
        SayHelloToClient demoService = (SayHelloToClient) context.getBean("demoService");  
  
        System.out.println(demoService.sayHello("huahua"));  
    }  
}

8.客户端主函数。

package com.mu.dubbo.test;  
  
import com.mu.dubbo.client.MyClient;  
  
/** 
 * Created by mdt on 17-6-26.
 */  
  
public class AppTest{  
    public static void main(String[] args) {
        MyClient myClient = new MyClient();  
        myClient.sayHello();  
    }  
}

9.配置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="5000" />  
    <!-- which service to consume? -->  
    <dubbo:reference id="demoService" interface="com.mu.dubbo.service.SayHelloToClient"/>  
</beans>

先启动服务端主函数,再启动客户端主函数。

  

 

dubbo最简单的例子