Dubbox與Zookeeper簡介及入門小案例

Dubbox

一:簡介
       Dubbox 是一個分佈式服務框架,其前身是阿里巴巴開源項目Dubbo ,被國內電商及互聯網項目中使用,後期阿里巴巴停止了該項目的維護,噹噹網便在Dubbo基礎上進行優化,並繼續維護,爲了與原有的Dubbo區分,故將其命名爲Dubbox。 
       Dubbox 致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。說白了就是個遠程服務調用的分佈式框架。
 
Dubbox與Zookeeper簡介及入門小案例
 
節點角色說明:
• Provider: 暴露服務的服務提供方。
• Consumer: 調用遠程服務的服務消費方。
• Registry: 服務註冊與發現的註冊中心。
• Monitor: 統計服務的調用次調和調用時間的監控中心。
• Container: 服務運行容器。
 
調用關係說明:
• 0. 服務容器負責啓動,加載,運行服務提供者。
• 1. 服務提供者在啓動時,向註冊中心註冊自己提供的服務。
• 2. 服務消費者在啓動時,向註冊中心訂閱自己所需的服務。
• 3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連接推
送變更數據給消費者。
• 4. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,
如果調用失敗,再選另一臺調用。
• 5. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計
數據到監控中心。

Zookeeper

一:簡介
       Zookeeper 是 Apacahe Hadoop 的子項目,是一個樹型的目錄服務,適合作爲Dubbox 服務的註冊中心,註冊中心負責服務地址的註冊與查找,相當於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小。
二: Zookeeper 在Linux系統的安裝

  1. Linux中安裝 jdk
  2. 把 zookeeper 的壓縮包上傳到 linux 系統。
  3. 解壓縮壓縮包:

    tar -zxvf zookeeper-3.4.6.tar.gz

  4. 進入 zookeeper-3.4.6 目錄,創建 data 文件夾。

    mkdir data

  5. 進入conf目錄 ,把 zoo_sample.cfg 改名爲 zoo.cfg

    mv zoo_sample.cfg zoo.cfg

  6. 打開zoo.cfg , 修改 data 屬性:

    dataDir=/root/zookeeper-3.4.6/data

三:Zookeeper 服務啓動
進入bin目錄,啓動服務輸入命令

./zkServer.sh start

Demo

一:創建服務提供者的Maven工程
1:pom文件

<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>

    </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>

2: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:Spring的配置文件

<?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.25.128:2181"/>
    <!-- dubbox的包掃描 -->
    <dubbo:annotation package="com.zgz.demo.serviceImpl" />

</beans>

4:代碼及目錄結構
Dubbox與Zookeeper簡介及入門小案例
Dubbox與Zookeeper簡介及入門小案例
Dubbox與Zookeeper簡介及入門小案例
二:創建服務消費者的Maven工程
1:pom文件和上面的一致
2: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:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

3:SpringMvc配置文件

<?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="false">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />
            </bean>  
        </mvc:message-converters>   
    </mvc:annotation-driven>

    <!-- 當前的應用名稱 -->
    <dubbo:application name="dubboxdemo-web" />
    <!-- 指定的註冊中心地址 -->
    <dubbo:registry address="zookeeper://192.168.25.128:2181"/>
    <!-- dubbox的包掃描 -->
    <dubbo:annotation package="com.zgz.demo.controller" />

</beans>

4:代碼及目錄結構
Dubbox與Zookeeper簡介及入門小案例
Dubbox與Zookeeper簡介及入門小案例
Dubbox與Zookeeper簡介及入門小案例
三:管理中心的部署
       我們在開發時,需要知道註冊中心都註冊了哪些服務,以便我們開發和測試。我們可以通過部署一個管理中心來實現。其實管理中心就是一個web應用,部署到tomcat即可。
(1)編譯源碼,得到war包在命令符下進入dubbo-admin目錄 ,輸入maven命令

mvn package -Dmaven.skip.test=true

(2)進入target文件夾,你會看到一個dubbo-admin-2.8.4.war , 在linux服務器上安裝tomcat, 將此war包上傳到linux服務器的tomcat的webapps下。啓動tomcat後自動解壓。

(3)如果你部署在zookeeper同一臺主機並且端口是默認的2181,則無需修改任何配置。如果不是在一臺主機上或端口被修改,需要修改WEB-INF下的dubbo.properties ,修改如下配置,修改後重新啓動tomcat

dubbo.registry.address=zookeeper://127.0.0.1:2181

四:管理中心的使用

打開瀏覽器,輸入http://192.168.25.128:8080/dubbo-admin ,登錄用戶名和密碼均爲root 進入首頁。
Dubbox與Zookeeper簡介及入門小案例

五:啓動demo
先啓動服務提供方的maven工程,在啓動服務消費方的maven工程。(啓動方法見下圖)注意zookeeper的服務要提前啓動,直接輸入服務消費方的地址,看到結果。
Dubbox與Zookeeper簡介及入門小案例
Dubbox與Zookeeper簡介及入門小案例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章