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!

相關文章
相關標籤/搜索