1. 程式人生 > >基於Dubbo+Zookeeper 實現WebService

基於Dubbo+Zookeeper 實現WebService

本文主要介紹 Dubbo+Zookeeper 實現webservice

1:windows 下安裝Zookeeper  


1.   概述

ZooKeeper是Hadoop的正式子專案,它是一個針對大型分散式系統的可靠協調系統,提供的功能包括:配置維護、名字服務、分散式同步、組服務等。ZooKeeper的目標就是封裝好複雜易出錯的關鍵服務,將簡單易用的介面和效能高效、功能穩定的系統提供給使用者。

 

 

2.   安裝&配置

在apache的官方網站提供了好多映象下載地址,然後找到對應的版本,目前最新的是3.3.6

下載地址:

http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz


Windows下安裝


把下載的zookeeper的檔案解壓到指定目錄

D:\machine\zookeeper-3.3.6>


修改conf下增加一個zoo.cfg

內容如下:

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

tickTime=2000

# The number of ticks that the initial

# synchronization phase can take

initLimit=10

# The number of ticks that can pass between

# sending a request and getting anacknowledgement

syncLimit=5

# the directory where the snapshot isstored.  //映象資料位置

dataDir=D:\\data\\zookeeper

#日誌位置

dataLogDir=D:\\logs\\zookeeper

# the port at which the clients willconnect  客戶端連線的埠

clientPort=2181

注:如果啟動有報錯提示cfg檔案有錯誤,可以用zoo_sample.cfg內內容替代也是可以的


進入到bin目錄,並且啟動zkServer.cmd,這個指令碼中會啟動一個Java程序

D:\machine\zookeeper-3.3.6>cd bin

D:\machine\zookeeper-3.3.6\bin>

D:\machine\zookeeper-3.3.6\bin >zkServer.cmd

啟動後jps可以看到QuorumPeerMain的程序

D:\machine\zookeeper-3.3.6\bin >jps


啟動客戶端執行檢視一下

D:\machine\zookeeper-3.3.6\bin>zkCli.cmd-server 127.0.0.1:2181



這個時候zookeeper已經安裝成功了,

 


2:建立Dubbo專案 一共2個專案  

一:建立者 provider


1:首先建立一個介面類


package com.dubbo;


public interface DemoService {
String sayHello(String name);


}

2:實現他的介面


package com.dubbo;


public class DemoServiceImpl implements DemoService{


public String sayHello(String name) {
return "Hello :"+name;
}


}

3建立配置檔案

applicationContext.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="hello-world-app"  />
 
    <!-- 使用multicast廣播註冊中心暴露服務地址 --> // 廣播方法有很多,這裡指採用Zookeeper 埠名 可以在Zookeeper 的zoo.cfg 裡面配置
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 用dubbo協議在20880埠暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 宣告需要暴露的服務介面 -->
    <dubbo:service interface="com.dubbo.DemoService" ref="demoService" />
 
    <!-- 建立介面實現類的bean -->
    <bean id="demoService" class="com.dubbo.DemoServiceImpl" />
    
 
</beans>

4 main 方法開啟執行緒 讀取配置 並 採用輸入流的阻塞 來維持服務, 內容如下


package provider;


import java.io.IOException;


import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml"
});
context.start();
System.in.read();
}
}//到這裡  建立者已經完成了


二:消費者  consumer


1 建立消費者介面 


 package com.dubbo;




public interface DemoService {


String sayHello(String name);


}

(消費者 這裡就不需要建立介面實現類了,因為他的實現類是從 建立者 呼叫的,如果寫了 實現類 那還叫WebService? 嘿嘿)


2  建立 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="consumer-of-helloworld-app"  />
 
    <!-- 使用Zookeeper廣播註冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 生成遠端服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="demoService" interface="com.dubbo.DemoService"/>
 
</beans>

(嚴重問題! 大家一定要記住, 消費者 和建立者 的 包名,以及介面名, 必須相同!必須相同!必須相同! 重要的事情說三遍!)


3 利用main方法建立執行緒讀取配置檔案呼叫 服務

package com.alibaba.dubbo.demo.pp;




import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.dubbo.DemoService;


public class Consumer {


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


DemoService demoService = (DemoService) context.getBean("demoService");
String arg=demoService.sayHello("tom");
System.out.println(arg);
System.in.read();
}


}




下面是demo的資源連結 http://download.csdn.net/detail/qq_22313643/9716387

最後補充一點:要下載控制檯 dubbo-admin的時候  如果JDK1.8以上的 要下專門的 jdk1.8的  dubbo-admin ,否則啟動不起來。

如果專案裡 有dubbo 服務,啟動專案前務必先啟動 zookeeper 否則專案起不來