Zookeeper介紹(配合Dubbox使用)
官方推薦使用zookeeoer注冊中心,注冊中心負責服務地址的注冊和查找,相當於目錄服務,提供提供者和消費者只在啟動時與注冊中心交互,注冊中心不轉發請求,壓力小.
zookeeper是Apacahe Hadoop的子項目,是一個樹形的目錄服務,支持變更推送,適合作為Dubbox服務的注冊中心,工業強度較高,可用於生產環境.
Zookeeper在Linux系統的安裝
安裝步驟:
第一步:安裝Jdk
第二步:把zookeeper的壓縮包上傳到linux系統.
第三步:解壓縮壓縮包
tar -zxvf zookeeper-3.4.6.tar.gz

第四步:進入conf目錄,吧zoo sample.cfg改名為zoo.cfg

Zookeeper服務啟動
進入bin目錄,啟動服務輸入命令
./zkServer.sh start
輸出一下內容表示啟動成功

關閉服務輸入命令
./zkServer.sh stop

查看狀態:
./zkServer.sh status
如果啟動狀態,提示:

如果未啟動狀態,提示:

如果想要配置離線約束:
這個地址:http://code.alibabatech.com/schema/dubbo/dubbo.xsd
不配置也是可以的.
入門的一個小Demo
配置Maven環境
-DarchetypeCatalog=internal
服務提供者開發:
開發步驟:
(1)創建Maven工程(WAR)dubboxdemo-service, war項目 ,在pom.xml中引入依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast.dubboxdemo</groupId>
<artifactId>dubboxdemo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo相關 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>8081</port>
<!-- 請求路徑 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2)在工程的webapps下創建WEB-INF文件夾,創建web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
(3)創建業務接口
創建包cn.itcast.dubbodemo.service,用於存放業務接口,創建接口
package cn.itcast.dubbodemo.service; /** * 業務接口 * @author Administrator * */ public interface UserService { public String getName(); }
(4)創建業務實現類
創建包cn.itcast.dubbodemo.service.impl ,用於存放業務實現類。創建業務實現類:
package cn.itcast.dubbodemo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import cn.itcast.dubbodemo.service.UserService; @Service public class UserServiceImpl implements UserService { public String getName() { return "itcast"; } }
注意:Service注解與原來不同,需要引入com.alibaba包下的
(5)編寫配置文件
在src/main/resources下創建applicationContext-service.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="dubboxdemo-service"/> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> <dubbo:annotation package="cn.itcast.dubboxdemo.service" /> </beans>
注意:dubbo:annotation用於掃描@Service注解。
3.5.2服務消費者開發
開發步驟:
(1)創建Maven工程(WAR)dubboxdemo-web, war項目,在pom.xml引入依賴,同“dubboxdemo-service”工程。區別就是把tomcat插件的運行端口改為8082 。
(2)在webapps目錄下創建WEB-INF 目錄,並創建web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定加載的配置文件 ,通過參數contextConfigLocation加載--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-web.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
(3)拷貝業務接口
將“dubboxdemo-service”工程的cn.itcast.dubboxdemo.service 包以及下面的接口拷貝至此工程。
(4)編寫Controller
package cn.itcast.dubboxdemo.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 cn.itcast.dubbodemo.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Reference private UserService userService; @RequestMapping("/showName") @ResponseBody public String showName(){ return userService.getName(); } }
(5)編寫spring配置文件
在src/main/resources下創建applicationContext-web.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> <property name="features"> <array> <value>WriteMapNullValue</value> <value>WriteDateUseDateFormat</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 引用dubbo 服務 --> <dubbo:application name="dubboxdemo-web" /> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> <dubbo:annotation package="cn.itcast.dubboxdemo.controller" /> </beans>
測試:
啟動服務消費方和服務提供方,兩個tomcat,以內使用了插件,所以直接啟動就好.
tomcat7:run
在瀏覽器輸入http://localhost:8082/user/showName.do,查看瀏覽器輸出結果
| package cn.itcast.dubbodemo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import cn.itcast.dubbodemo.service.UserService; @Service publicclass UserServiceImpl implements UserService { public String getName() { return"itcast"; } } |
