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简介及入门小案例

猜你喜欢

转载自blog.51cto.com/13416247/2356903