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.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring-framework.version}version>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.4.10version>
            <exclusions>
                <exclusion>
                    <artifactId>springartifactId>
                    <groupId>org.springframeworkgroupId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.3version>
        dependency>

新建一个接口:

public interface DemoService {
    String sayHello(String name);

    public List getUsers();
}

实现接口:

public class DemoServiceImpl implements DemoService {

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

    public List getUsers() {
        List list = new ArrayList();
        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 id="demoService" class="com.unj.dubbotest.provider.DemoServiceImpl" />  

      
    <dubbo:application name="xixi_provider"  />  

      

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

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

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

      
    <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 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运行结果如下:
Dubbo+zookeeper入门示例搭建_第1张图片

远程调用失败问题:
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)就可以解决这一个异常.

你可能感兴趣的:(web)