Dubbo+zookeeper入门示例搭建

参考文章:http://blog.csdn.net/doegoo/article/details/49679781
http://www.cnblogs.com/ASPNET2008/p/5622005.html
http://www.cnblogs.com/Javame/p/3632473.html

-安装zookeeper
1.在官网上下载zookeeper安装文件,解压,重命名zookeeper-3.4.5\conf目录下的zoo_sample.cfg为zoo.cfg
2.在zookeeper-3.4.5\bin目录下,双击zkServer.cmd开启zookeeper服务器

-安装dubbo
1新建maven项目,在pom文件中添加如下依赖:引入如下三个主要的包就可以了,主要是spring,dubbo以及zkclient

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>

新建一个接口:

public interface DemoService {
    String sayHello(String name);

    public List<User> getUsers();
}

实现接口:

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        System.out.println("----------");
        return "Hello " + name;
    }

    public List<User> getUsers() {
        List<User> list = new ArrayList<User>();
        User u1 = new User();
        u1.setName("jack");
        u1.setAge(20);
        u1.setSex("男");

        User u2 = new User();
        u2.setName("tom");
        u2.setAge(21);
        u2.setSex("女");

        User u3 = new User();
        u3.setName("rose");
        u3.setAge(19);
        u3.setSex("女");

        list.add(u1);
        list.add(u2);
        list.add(u3);
        return list;
    }
}

配置producter的Spring配置文件:

<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.unj.dubbotest.provider.DemoServiceImpl" />  

    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="xixi_provider"  />  

    <!-- 使用multicast广播注册中心暴露服务地址   
    <dubbo:registry address="multicast://127.0.0.1:1234" />-->  

    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />   

    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  

    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" />  

</beans>

开启服务:

public class Producter {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext-producter.xml" });
        context.start();
        System.out.println("start");
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

配置consumer的spring配置文件

<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="hehe_consumer" />  

    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />  

    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
    <dubbo:reference id="demoService"  
        interface="com.unj.dubbotest.provider.DemoService" />  

</beans>  

consumer来使用服务

public class Consumer {  

    public static void main(String[] args) throws Exception {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] { "applicationContext-consumer.xml" });  
        context.start();  

        DemoService demoService = (DemoService) context.getBean("demoService"); //  
        String hello = demoService.sayHello("tom"); // ִ  
        System.out.println(hello); //   

        //   
        List<User> list = demoService.getUsers();  
        if (list != null && list.size() > 0) {  
            for (int i = 0; i < list.size(); i++) {  
                System.out.println(list.get(i));  
            }  
        }  
        // System.out.println(demoService.hehe());  
        System.in.read();  
    }  

}

使用的user类如下:

public class User implements Serializable{
    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

先启动producter,再启动consumer运行结果如下:
这里写图片描述

远程调用失败问题:
Caused by: com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method subscribe in the service com.alibab
a.dubbo.registry.RegistryService. Tried 3 times of the providers[172.168.1.167:2181] (1/1) from the registry
172.168.1.167:2181 on the consumer 169.254.249.102 using the dubbo version2.4.9. Last error is: Invoke remote
method timeout.
解决方法
这个是由于dubbo接口中的的传输对象没有被序列化而导致的,只需要要检查dubbo接口中参数中的实体类实现序列化(implements Serializable)就可以解决这一个异常.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值