dubbo测试项目搭建

1. dubbo简介
dubbo是阿里巴巴旗下研发的分布式系统,由于配置和使用相对灵活广受企业使用。
http://blog.csdn.net/lovesummerforever/article/details/48138393以上网址详细介绍,本人就不一一说了,上代码,亲测。
2. 项目搭建

  • 前期准备
    zookeeper安装请看

    http://blog.csdn.net/u014037599/article/details/78035134

    相关jar包
    我采用的是maven下面放上依赖关系。
    dubbo-admin
    非必须,用这个可以查看dubbo服务注册的

  • pom.xml
    pom.xml文件主要分三部分dubbo+zookeeper+zkclient

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
  • 新建源目录(source)存放dubbo 生产者消费者配置文件如图:
  • 这里写图片描述
    -新建TestService.java
package com.test.dubbo.api;

public interface TestService {

    void sayhello(String name);

}

新建TestServiceImpl.java

package com.test.dubbo.api;

public class TestServiceImpl implements TestService{

    public void sayhello(String name) {

        System.out.println(name+"hello");

    }

}

生产者

package com.test.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.test.dubbo.api.TestService;

/**
 * CallbackConsumer
 *
 * @author william.liangf
 */
public class TestConsumer {

    public static void main(String[] args) throws Exception {
        String path = "classpath:consumer.xml";
        GenericXmlApplicationContext xml = new GenericXmlApplicationContext(path);

        xml.start();
        System.out.println("------------");
        TestService test = (TestService) xml.getBean("testService");

        for(int i=0;i<100;i++){
            test.sayhello(i+"");
        }
        System.in.read();
    }

}

生产者对应xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="test-provider"/>

    <dubbo:registry address="zookeeper://xxx.xxx.xxx.xxx:2181" />

    <dubbo:protocol name="dubbo" port="20881"/>

    <bean id="testService" class="com.test.dubbo.api.TestServiceImpl"></bean>
    <dubbo:service interface="com.test.dubbo.api.TestService" 
                   ref="testService"/>


</beans>

消费者示例

package com.test.dubbo;

import java.io.IOException;

import org.springframework.context.support.GenericXmlApplicationContext;

public class TestProvider {

    public static void main(String[] args) throws IOException {

        String path = "classpath:provider.xml";
        GenericXmlApplicationContext xml = new GenericXmlApplicationContext(path);

        xml.start();
        System.out.println("------------");
        System.in.read();


    }

}

消费者xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="async-consumer"/>

    <dubbo:registry address="zookeeper://xxx.xxx.xxx.xxx:2181" />

    <dubbo:reference id="testService" interface="com.test.dubbo.api.TestService"/>

</beans>
  • 启动
    先运行TestProvider.java,如图所示启动注册成功
    这里写图片描述
    这里写图片描述

运行TestConsumer.java消费注册的服务出现图示代表成功
这里写图片描述

ps:源代码下载地址http://pan.baidu.com/s/1c2kzI84

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值