Dubbo项目demo搭建

项目参考:

http://dubbo.io/User+Guide-zh.htm

https://my.oschina.net/superman158/blog/466637

项目使用 maven+idea 进行开发,以zookeeper为注册中心,所以需要安装并运行zookeeper

---------服务端开发

引入相关依赖:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.9</version>
        </dependency>
    </dependencies>

声明Provider接口(Provider.java):

public interface Provider {
    String build(String name) throws Exception;
}

实现接口(DemoServiceImpl .java):

public class DemoServiceImpl implements Provider {
    public String build(String name) throws Exception {
        System.out.println(" got a argument: " + name);
        return "message from provider: " + name;
    }
}

编辑spring的配置文件,随便命名,这里命名为“applicationContext.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
        ">
    <!-- 具体的实现bean -->
    <bean id="demoService"
          class="com.dubbotest.impl.DemoServiceImpl" />
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="anyname_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.dubbotest.Provider"
                   ref="demoService" />
</beans>

写启动类(Test1.java)

public class Test1 {
    public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        context.start();

        System.out.println(" app run ");

        System.in.read(); // 按任意键退出
    }
}

启动本机的zookeeper服务,然后执行Test1.main()

Console中成功打出:app run 即成功。

 

---------客户端开发

项目结构、文件,整个照抄。

删除“DemoServiceImpl .java”。

修改applicationContext.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="consumer_app" />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:consumer timeout="5000" />

    <dubbo:reference id="demoService"
                     interface="com.dubbotest.Provider" />
</beans>

修改启动类,重命名为Test2.java(Test2.java)

public class Test2 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
        context.start();
        Provider demoService = (Provider) context.getBean("demoService"); // 获取bean
        // service
        // invocation
        // proxy
        String message = "";
        try {
            message = demoService.build("2016-10-20");
            System.out.println(" the message from server is:" + message);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}

执行Test2.main()

服务端的console打印出: got a argument: 2016-10-20

客户端的console打印出: the message from server is:message from provider: 2016-10-20

联调成功。

demo 文件连接: https://files.cnblogs.com/files/fri-yu/dubbo.zip

posted @ 2016-10-20 16:39  coding烫烫烫烫烫  阅读(12683)  评论(3编辑  收藏  举报