dubbo 2 Dubbo與 Zookeeper、SpringMVC 整合使用
第一步:在Linux上安裝Zookeeper
Zookeeper作為Dubbo服務的註冊中心,Dubbo原先基於資料庫的註冊中心,沒采用Zookeeper,Zookeeper一個分散式的服務框架,是樹型的目錄服務的資料儲存,能做到叢集管理資料,這裡能很好的作為Dubbo服務的註冊中心,Dubbo能與Zookeeper做到叢集部署,當提供者出現斷電等異常停機時,Zookeeper註冊中心能自動刪除提供者資訊,當提供者重啟時,能自動恢復註冊資料,以及訂閱請求。我們先在linux上安裝Zookeeper,我們安裝最簡單的單點,叢集比較麻煩。
先需要安裝JdK,從Oracle的Java網站下載,安裝很簡單,就不再詳述。
單機模式
單機安裝非常簡單,只要獲取到 Zookeeper 的壓縮包並解壓到某個目錄如:C:\zookeeper-3.4.5\下,Zookeeper 的啟動指令碼在 bin 目錄下,Windows 下的啟動指令碼是 zkServer.cmd。
在你執行啟動指令碼之前,還有幾個基本的配置項需要配置一下,Zookeeper 的配置檔案在 conf 目錄下,這個目錄下有 zoo_sample.cfg 和 log4j.properties,你需要做的就是將 zoo_sample.cfg 改名為 zoo.cfg,因為 Zookeeper 在啟動時會找這個檔案作為預設配置檔案。下面詳細介紹一下,這個配置檔案中各個配置項的意義。
<span style="font-size:18px;"># The number of milliseconds of each tick 2 tickTime=2000 3 # The number of ticks that the initial 4 # synchronization phase can take 5 initLimit=10 6 # The number of ticks that can pass between 7 # sending a request and getting an acknowledgement 8 syncLimit=5View Code9 # the directory where the snapshot is stored. 10 # do not use /tmp for storage, /tmp here is just 11 # example sakes. 12 dataDir=C:\\zookeeper-3.4.5\\data 13 dataLogDir=C:\\zookeeper-3.4.5\\log 14 # the port at which the clients will connect 15 clientPort=2181 16 # 17 # Be sure to read the maintenance section of the 18 # administrator guide before turning on autopurge. 19 # 20 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance 21 # 22 # The number of snapshots to retain in dataDir 23 #autopurge.snapRetainCount=3 24 # Purge task interval in hours 25 # Set to "0" to disable auto purge feature 26 #autopurge.purgeInterval=1</span>
tickTime:這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
dataDir:顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
dataLogDir:顧名思義就是 Zookeeper 儲存日誌檔案的目錄
clientPort:這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
當這些配置項配置好後,你現在就可以啟動 Zookeeper 了,啟動後要檢查 Zookeeper 是否已經在服務,可以通過 netstat – ano 命令檢視是否有你配置的
clientPort 埠號在監聽服務。
第二步:配置dubbo-admin的管理頁面,方便我們管理頁面
(1)下載dubbo-admin-2.4.1.war包,在windows的tomcat部署,先把dubbo-admin-2.4.1放在tomcat的webapps/ROOT下,然後進行解壓
(2)然後到webapps/ROOT/WEB-INF下,有一個dubbo.properties檔案,裡面指向Zookeeper,使用的是Zookeeper的註冊中心,如圖所示:
1 <span style="font-size:18px;">dubbo.registry.address=zookeeper://127.0.0.1:2181 2 dubbo.admin.root.password=root 3 dubbo.admin.guest.password=guest</span>
(3)然後啟動tomcat服務,使用者名稱和密碼:root,並訪問服務,顯示登陸頁面,說明dubbo-admin部署成功,如圖所示:
第三步:SpringMVC與Dubbo的整合,這邊使用的Maven的管理專案
第一:我們先開發服務註冊的
就是提供服務,專案結構如圖所示:
(1)test-maven-api專案加入了一個服務介面,程式碼如下:
public interface TestRegistryService { public String hello(String name);
public Person getPerson(String name); }
包括類似的,介面(interface)和依賴類(如上面Person)都是放到了業務支援包(support)裡
上面專案 test-maven-api新建時就選擇module ,然後被其他服務根據依賴引用
這其實也是分散式服務拆分為dubbo服務的思路
(2)test-maven-console在pom.xml加入Dubbo和Zookeeper的jar包、引用test-maven-api的jar包(業務支援包),程式碼如下:
<span style="font-size:18px;"><dependency> 2 <groupId>cn.test</groupId> 3 <artifactId>test-maven-api</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </dependency> 6 <dependency> 7 <groupId>com.alibaba</groupId> 8 <artifactId>dubbo</artifactId> 9 <version>2.5.3</version> 10 </dependency> 11 <dependency> 12 <groupId>org.apache.zookeeper</groupId> 13 <artifactId>zookeeper</artifactId> 14 <version>3.4.6</version> 15 </dependency> 16 <dependency> 17 <groupId>com.github.sgroschupf</groupId> 18 <artifactId>zkclient</artifactId> 19 <version>0.1</version> 20 </dependency></span>
(3)test-maven-console實現具體的服務,程式碼如下:
介面實現類
1 @Service("testRegistryService") 2 public class TestRegistryServiceImpl implements TestRegistryService { 3 public String hello(String name) { 4 return "hello"+name; 5 } 6 }
(4)我們服務已經實現好了,這時要暴露服務,程式碼如下:
配置dubbo.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" <span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span> xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-lazy-init="false" > <!-- 提供方應用名稱資訊,這個相當於起一個名字,我們dubbo管理頁面比較清晰是哪個應用暴露出來的 --> <dubbo:application name="dubbo_provider"></dubbo:application> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry> <!-- 協議 --> <dubbo:protocol id = "di" name = "dubbo" port = 20880> <!-- 一些全域性配置提前配好,其他服務提供者就不用配了,服務呼叫者也可以複用 --> <dubbo:provider timeout = "3000"> <!-- 要暴露的服務介面 --> <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" /> <!-- spring掃描包的路徑 --> <context:component-scan base-package = "com.enjoy"> <!-- 服務類申明為bean 給spring管理 --> <bean id = "testRegistryService" class = "cn.test.dubbo.registry.service.TestRegistryServiceImpl "> </beans>
一些標籤說明:
dubbo:registry標籤一些屬性的說明:
1)register是否向此註冊中心註冊服務,如果設為false,將只訂閱,不註冊。
2)check註冊中心不存在時,是否報錯。
3)subscribe是否向此註冊中心訂閱服務,如果設為false,將只註冊,不訂閱。
4)timeout註冊中心請求超時時間(毫秒)。
5)address可以Zookeeper叢集配置,地址可以多個以逗號隔開等。
dubbo:protocol
name是個列舉值,表示協議,包括 dubbo/rmi/rest 等
dubbo:provider
提供方的預設值,當ProtocolConfig和ServiceConfig某屬性沒有配置時,採用此預設值,可選。一般用於全域性配置
dubbo:service
服務配置,用於暴露一個服務,定義服務的元資訊
1)interface服務介面的路徑
2)ref引用對應的實現類的Bean的ID
3)registry向指定註冊中心註冊,在多個註冊中心時使用,值為<dubbo:registry>的id屬性,多個註冊中心ID用逗號分隔,如果不想將該服務註冊到任何registry,可將值設為N/A
4)register預設true,該協議的服務是否註冊到註冊中心。
(5)啟動專案,
啟動類中
ClasspathxmlApplicationContext context = newClasspathxmlApplicationContext("classPath:dubbo.xml");
context .start();
然後我們在Dubbo管理頁面上顯示,已經暴露的服務,但顯示還沒有消費者,因為我們還沒實現消費者服務,如圖所示:
第二:我們再開發服務消費者來呼叫服務,
我們再新建一個新的消費者專案:
引入依賴
1 <span style="font-size:18px;"><dependency> 2 <groupId>cn.test</groupId> 3 <artifactId>test-maven-api</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 </dependency> 6 <dependency> 7 <groupId>com.alibaba</groupId> 8 <artifactId>dubbo</artifactId> 9 <version>2.5.3</version> 10 </dependency> 11 <dependency> 12 <groupId>org.apache.zookeeper</groupId> 13 <artifactId>zookeeper</artifactId> 14 <version>3.4.6</version> 15 </dependency> 16 <dependency> 17 <groupId>com.github.sgroschupf</groupId> 18 <artifactId>zkclient</artifactId> 19 <version>0.1</version> 20 </dependency></span>
(2)test-maven-consumer-console專案的具體實現,程式碼如下:
1 @Controller 2 public class IndexController { 3 4 @Autowired 5 private TestRegistryService testRegistryService; 6 7 @RequestMapping("/hello") 8 public String index(Model model){ 9 String name=testRegistryService.hello("zz"); 10 System.out.println("xx=="+name); 11 return ""; 12 } 13 14 }
dubbo.xml 配置檔案
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span> 7 xmlns:context="http://www.springframework.org/schema/context" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 10 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 11 <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span> 12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" 13 default-lazy-init="false" > 14 15 <dubbo:application name="dubbo_consumer"></dubbo:application> 16 <!-- 使用zookeeper註冊中心暴露服務地址 --> 17 <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry> 18 <!-- 要引用的服務 --> 19 <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference> 20 </beans>
dubbo:reference的一些屬性的說明:
1)interface呼叫的服務介面
2)check啟動時檢查提供者是否存在,true報錯,false忽略
3)registry從指定註冊中心註冊獲取服務列表,在多個註冊中心時使用,值為<dubbo:registry>的id屬性,多個註冊中心ID用逗號分隔
4)loadbalance負載均衡策略,可選值:random(按服務提供者配置的權重weight),roundrobin(輪詢),leastactive(最少活躍數,慢的機器收到的請求少)
(4)專案啟動,Dubbo管理頁面,能看到消費者,如圖所示:
(5)然後訪問消費者專案,Controller層能像呼叫本地一樣呼叫服務的具體實現(見上面IndexController類 ),呼叫結果如圖所示:
總結:
經過一系列的操作之後,我們感覺,的確很簡單,dubbo給我們封裝了很多操作,讓我們不需要過多考慮具體的實現細節,配置化生成咱們的應用,這樣的思想,現在在IT行業正在盛行!