1. 程式人生 > >Dubbo+Zookeeper+Spring MVC+JDK8整合應用

Dubbo+Zookeeper+Spring MVC+JDK8整合應用

軟體版本說明:dubbo-admin-2.5.4.war,zookeeper-3.4.9.tar.gz,dubbo-2.8.4,jdk8,apache-tomcat-7.0.68

一. zookeeper安裝

  下載zookeeper註冊中心,下載地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下載後解壓即可,解壓zookeeper-3.4.9.tar.gz到指定目錄,進入zookeeper-3.4.9\conf目錄並複製zoo_sample.cfg檔案改名為zoo.cfg,因為zookeeper啟動時預設找zoo.cfg這個檔案,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 an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
•tickTime:這個時間是作為Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
•dataDir:顧名思義就是 Zookeeper儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
•dataLogDir:顧名思義就是Zookeeper 儲存日誌檔案的目錄

•clientPort:這個埠就是客戶端連線Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求

接著啟動zookeeper註冊中心服務。
linux下執行命令:bin/zkServer.sh start ;windows下直接雙擊zkServer.cmd就可以啟動服務了。

zookeeper客戶端工具推薦ZooInspector下載地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip,解壓縮後雙擊ZooInspector\build\zookeeper-dev-ZooInspector.jar,連線zookeeper服務,就可以看到已經向zookeeper註冊的服務

二. 介面服務

     新建一個單獨的服務介面工程,定義一個服務介面類DemoService,服務提供方和消費方公用。

package com.dubbo.service;

public interface  DemoService {

 public	String sayHello(String name);
	  
}

三. 提供者服務

    新建一個服務提供工程,定義一個DemoService介面的實現類DemoServiceImpl,即遠端請求的服務類。

package com.dubbo.api.service;

import com.dubbo.service.DemoService;


public class DemoServiceImpl implements DemoService {  
	 
    public String sayHello(String name) {  
        return "Hello " + name;  
    }  
}

spring mvc配置暴露服務
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
	http://www.springframework.org/schema/context  
	http://www.springframework.org/schema/context/spring-context-3.1.xsd  
	http://www.springframework.org/schema/mvc  
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		
	<!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
	<context:component-scan base-package="com.dubbo" />  
		
	<!--避免IE執行AJAX時,返回JSON出現下載檔案 -->

	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	<!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />	<!-- JSON轉換器 -->
			</list>
		</property>
	</bean>
	<!-- 定義跳轉的檔案的前後綴 ,檢視模式配置 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 這裡的配置我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 -->
		<property name="prefix" value="/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
    
	
    <!-- 提供方應用名稱資訊,這個相當於起一個名字,我們dubbo管理頁面比較清晰是哪個應用暴露出來的 -->  
       <dubbo:application name="dubbo_provider"></dubbo:application> 
    <!-- 使用zookeeper註冊中心暴露服務地址 -->    
       <dubbo:registry address="zookeeper://localhost:2181" ></dubbo:registry> 
    <!-- 用dubbo協議在20880埠暴露服務 -->
	<dubbo:protocol name="dubbo" host="localhost" port="18080" />  
    <!-- 要暴露的服務介面 -->    
       <dubbo:service interface="com.dubbo.service.DemoService" ref="demoService"/>
	<!-- 具體的實現bean -->
	<bean id="demoService" class="com.dubbo.api.service.DemoServiceImpl" />
	
	<!-- validate -->
	<mvc:annotation-driven/>  
    <context:annotation-config/> 


</beans>

四. 消費者服務

新建一個消費者服務工程,新建一個測試類SayHelloController

package com.dubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dubbo.service.DemoService;


@Controller
public class SayHelloController {
	
	@Autowired
	private DemoService demoService;
	
	@RequestMapping(value = "/test")
	@ResponseBody
	public String sayHello(String name){
		StringBuffer sb = new StringBuffer("Dubbo: ");
		sb.append(demoService.sayHello(name));
		return sb.toString();
	}
	
}
暴露的dubbo服務配置dubbo-config.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="test_consumer" />

	<!-- 使用zookeeper註冊中心暴露服務地址 -->
	<dubbo:registry address="zookeeper://localhost:2181" />

	<!-- 用dubbo協議在20880埠暴露服務 -->
	<!--dubbo:protocol name="dubbo" port="20880" /-->
	<dubbo:protocol name="dubbo" host="localhost" port="18080" />  

	<!-- 宣告需要暴露的服務介面 -->
	<dubbo:reference interface="com.dubbo.service.DemoService" id="demoService" check="false"/>


</beans>

啟動服務,在瀏覽器中輸入請求的url:http://localhost:8080/springmvc-dubbo-test/test?name=666666

測試結果:666666

五. Dubbo的管理頁

 1. dubbo-admin-2.5.4下載地址:http://download.csdn.net/detail/dfdsggdgg/9724631

     2. 解壓dubbo-admin-2.5.4.war,將dubbo-admin-2.5.4下的檔案替換到tomcat/webapps/ROOT資料夾下(即替換tomcat的啟動主頁)

     3. 編輯webapps\ROOT\WEB-INF下的dubbo.properties檔案

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=123456
dubbo.registry.address配置zookerper的連線地址,
dubbo.admin.root.password就是登陸的密碼

4. dubbo管理登入:http://127.0.0.1:28080/,這裡設定tomcat的埠號為28080

dubbo中的服務


提供者


消費者

整合程式碼及安裝包下載:http://download.csdn.net/detail/dfdsggdgg/9724676