Dubbox與zookeeper

1.Dubbox框架java

 

SOA(Service-Oriented Architecture)是一種面向服務的架構。它將應用程序的不一樣功能單元(稱爲服務)進行拆分,並經過這些服務之間定義良好的接口和契約聯繫起來.linux

Dubbox 是一個分佈式服務框架,若是沒有分佈式的需求,實際上是不須要用的,只有在分佈式的時候,纔有dubbox這樣的分佈式服務框架的需求git

 

2.註冊中心Zookeepergithub

註冊中心負責服務地址的註冊與查找,至關於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小web

3.在linux上安裝zookeeperspring

1)安裝jdkapache

2) zookeeper 的壓縮包上傳到 linux 系統瀏覽器

3)解壓spring-mvc

tar -zxvf zookeeper-3.4.6.tar.gz -C /usr/local

4)進入 zookeeper-3.4.6 目錄,建立 data 文件夾。tomcat

cd zookeeper-3.4.6
mkdir data

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

cd conf
mv zoo_sample.cfg zoo.cfg

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

dataDir=/usr/local/zookeeper-3.4.6/data

7)進入bin目錄,啓動zookeeper服務

cd bin
./zkServer.sh start

輸出以上內容表示啓動成功

關閉服務:

./zkServer.sh stop

查看狀態:

./zkServer.sh statu

若是啓動狀態,提示

若是未啓動狀態,提示

 4.入門小案例

服務消費者開發

1)建立Maven工程(WARdubboxdemo-web ,在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」工程的com.gh.dubboxdemo.service 包以及下面的接口拷貝至此工程。

4)編寫Controller 

package com.gh.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 com.gh.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="false">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />
            </bean>  
        </mvc:message-converters>    
    </mvc:annotation-driven>
    <!-- 引用dubbo 服務 -->
    <dubbo:application name="dubboxdemo-web" />
    <dubbo:registry address="zookeeper://192.168.25.132:2181"/>
     <dubbo:annotation package="com.gh.dubboxdemo.controller" />
</beans>

6)測試運行

在瀏覽器輸入http://localhost:8082/user/showName.do,查看瀏覽器輸出結果。

 

服務提供者開發

1)建立Maven工程(WARdubboxdemo-service  ,在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>com.gh.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>        
    </dependencies>
   <build>  
      <plugins>
          <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-compiler-plugin</artifactId>  
              <version>2.3.2</version>  
              <configuration>  
                  <source>1.8</source>  
                  <target>1.8</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)建立業務接口

建立包com.gh.dubbodemo.service,用於存放業務接口,建立接口

package com.gh.dubbodemo.service;

public interface UserService {    
    public String getName();    
}

4)建立業務實現類

建立包com.gh.dubbodemo.service.impl ,用於存放業務實現類。建立業務實現類:

package com.gh.dubbodemo.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.gh.dubbodemo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
    public String getName() {        
        return "success";
    }
}

注意: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.25.132:2181"/> 
<dubbo:annotation package="com.gh.dubboxdemo.service" /> 
</beans>

注意:dubbo:annotation用於掃描@Service註解。

6)測試運行

tomcat7:run,注意,先運行服務提供者,再運行服務消費者。

3.6.1管理端安裝

1)編譯源碼,獲得war

從網上下載個dubbox-master.zip 的資源包 ,這個是dubbox的源碼,咱們可使用maven命令編譯源碼獲得「管理端」的war包,將此壓縮包解壓,在命令符下進入dubbo-admin目錄 ,輸入maven命令

mvn package -Dmaven.skip.test=true

若是你看到以下信息,就說明成功了

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

3)若是你部署在zookeeper同一臺主機而且端口是默認的2181,則無需修改任何配置。若是不是在一臺主機上或端口被修改,須要修改WEB-INF下的dubbo.properties  ,修改以下配置:

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

修改後從新啓動tomcat

3.6.2管理端使用

1)打開瀏覽器,輸入http://192.168.25.132:8080/dubbo-admin/ ,登陸用戶名和密碼均爲root 進入首頁。 (192.168.25.132:)是我部署的linux主機地址。

2)啓動服務提供者工程,便可在服務治理-提供者查看到該服務。

點擊其中一條數據後能夠查看詳情。

3)啓動服務消費者工程,運行頁面,觀察「消費者」列表

相關文章
相關標籤/搜索