Spring+Dubbo+Zookeeper简单框架与使用

例子参考地址:http://www.cnblogs.com/Javame/p/3632473.htmlhtml

1、实例搭建

一、搭建框架前先下载Zookeeper(http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gzjava

二、解压Zookeeper到指定文件目录,在bin目录下双击zkServer.cmd(Windows),启动Zookeeper服务,正常应该是以下图所示,错误则看第三步git

三、若启动失败,则在conf目录下,新建zoo.cfg配置文件github

配置以下,主要修改路径地址(参考:http://blog.csdn.net/morning99/article/details/40426133)spring

# The number of milliseconds of each tick  心跳间隔 毫秒每次apache

tickTime=2000api

# The number of ticks that the initialtomcat

# synchronization phase can take服务器

initLimit=10app

# The number of ticks that can pass between

# sending a request and getting anacknowledgement

syncLimit=5

# the directory where the snapshot isstored.  //镜像数据位置

dataDir=F:\Work\Zookeeper\data

#日志位置

dataLogDir=F:\Work\Zookeeper\logs

# the port at which the clients willconnect  客户端链接的端口

clientPort=2181

参数详解:

1.tickTime:CS通讯心跳数

Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每一个 tickTime 时间就会发送一个心跳。tickTime以毫秒为单位。

2.initLimit:LF初始通讯时限

集群中的follower服务器(F)与leader服务器(L)之间初始链接时能容忍的最多心跳数(tickTime的数量)。

3.syncLimit:LF同步通讯时限
集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。

4.dataDir:数据文件目录
Zookeeper保存数据的目录,默认状况下,Zookeeper将写数据的日志文件也保存在这个目录里。

5.dataLogDir:日志文件目录
Zookeeper保存日志文件的目录。

6.clientPort:客户端链接端口
客户端链接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

7.服务器名称与地址:集群信息(服务器编号,服务器地址,LF通讯端口,选举端口)
这个配置项的书写格式比较特殊,规则以下:

server.N=YYY:A:B  

eg:

server.0=233.34.9.144:2008:6008  

server.1=233.34.9.145:2008:6008  

Zookeeper配置参数详解:http://blog.csdn.net/poechant/article/details/6650249

三、配置pom.xml(Provider与Consumer配置一致)

<dependencies>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>servlet-api</artifactId>
            <version>6.0.45</version>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.2-alpha</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

    </dependencies>

Provider方:

结构以下图(和Consumer方相似)

四、具体类的编写(和Consumer方一致)

在model下新建一个User类,可是因为使用Dubbo,因此必定要实现序列化Serializable类

public class User implements Serializable{
	private static final long serialVersionUID = -1009733312893309388L;
	
	private String name;
	private String sex;
	private Integer 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 Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

而后在service下新建一个DemoService接口(和Consumer方一致),impl下新建DemoServiceImpl实现接口

public interface DemoService {
	String sayHello(String name);  
    public List<User> getUsers();
}

public class DemoServiceImpl implements DemoService {

	public String sayHello(String name) {
		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;
	}
}

而后provider下新建一个Provider类,实如今Zookeeper中注册

public class Provider {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application.xml"});  
        context.start();  
        try {
			System.in.read();// 为保证服务一直开着,利用输入流的阻塞来模拟   
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}

五、application.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.zd.dubbo.service.impl.DemoServiceImpl" />  
      
    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="xixi_provider"  />  
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />   
    
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  
   
    <!-- 声明须要暴露的服务接口 -->  
    <dubbo:service interface="com.zd.dubbo.service.DemoService" ref="demoService" />  

      
</beans>  

Consumer方:

目录结构

我理解的是Provider方在Zookeeper注册,暴露服务地址以及DemoService接口,而后Consumer方就能够调用其暴露出来的接口,具体实现由Provider完成,Consumer方只须要拥有与Provider方一致的接口,调用接口方法就实现远程调用。

主要贴出与Provider不一样的代码,其余与其相似或一致的就不贴了。

一、consumer下新建Consumer类

public class Consumer {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] { "application.xml" });  
        context.start();  
  
        DemoService demoService = (DemoService) context.getBean("demoService"); //  
        String hello = demoService.sayHello("tom"); //调用sayHello方法
        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));  
            }  
        }  
        try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
}

二、application.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="hehe_consumer" />  
  
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  
    <!-- 生成远程服务代理,能够像使用本地bean同样使用demoService -->  
    <dubbo:reference id="demoService" interface="com.zd.dubbo.service.DemoService" />
 
</beans>

而后先启动Provider再启动Consumer,结果以下图:

 

2、常见问题

一、Dubbo采用Spring配置方式,加入Schema便可,以下

可是可能报错:

Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'.
- schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', 
 because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not 
 <xsd:schema>.

解决方案:

在maven下载的dubbo.jar(路径:C:\Users\Administrator\.m2\repository\com\alibaba\dubbo\2.5.3)解压文件中能够找到dubbo.xsd(搜索查找便可)

而后Window-->Preferences-->XML-->XML Catalog-->Add-->Catalog Entry

因为Uri Location的路径中不能包含 .,因此我将其从新拷贝到另外一个地方了,必定要修改Key,配置以下:

而后右键项目,选择Validate!

相关文章
相关标签/搜索