Dubbo与Zookeeper、Spring框架的整合

本文采用Dubbo与Zookeeper、Spring框架的整合。

Dubbo是什么?

Dubbo 不单单只是高性能的 RPC 调用框架,更是 SOA 服务治理的一种方案。
核心:
1. 远程通讯(RPC): 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

Dubbo 架构流程图

这里写图片描述
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。

调用流程
0.启动服务提供者。
1.服务提供者在启动时,向注册中心注册自己提供的服务。
2.服务消费者在启动时,向注册中心订阅自己所需的服务。
3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

dubbo支持多种类型的注册中心:
Multicast注册中心
Zookeeper注册中心
Redis注册中心
Simple注册中心

ZooKeeper 服务注册中心
ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

Step1:下载最新版本且稳定的zookeeper,在http://apache.fayea.com/zookeeper/里就可以下载,也可以在我的资源管理器中下载Dubbo与Zookeeper所需的工具

step2:解压下载的文件,zookeeper不需要安装的,解压即可。 然后把解压后文件放到你要的位置,如:E:\zookeeper\zookeeper-3.3.6

Step3:进入E:\zookeeper\zookeeper-3.3.6\conf目录,将里面的zoo_sample.cfg文件,做一个备份,然后改名为zoo.cfg,因为zookeeper启动后,只认识zoo.cfg中的所有设置和配置

Step4:打开zoo.cfg,输入以下内容

# zk之间(当然是配置了zk集群模式)、zk与客户端之间每隔2秒进行一次心跳检测
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit和leader之间最长心跳时间,设置的是10那么就是tickTime的10陪,即2000毫秒*10=20000毫秒=20秒
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
#leader和follower之间发送消息, 请求和应答的最大时间长度. 此时该参数设置为5, 说明时间限制为5倍tickTime, 即2000毫秒*5=10
syncLimit=5
# the directory where the snapshot is stored.
# 数据目录. 可以是任意目录.
dataDir=E:\\zookeeper\\zookeeper-3.3.6\\data
dataLogDir=E:\\zookeeper\\zookeeper-3.3.6\\logs
# the port at which the clients will connect
# 监听client连接的端口号.
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
# 这个操作将限制连接到 ZooKeeper 的客户端的数量,限制并发连接的数量,它通过 IP 来区分不同的客户端。此配置选项可以用来阻止某些类别的 Dos 攻击。将它设置为 0 或者忽略而不进行设置将会取消对并发连接的限制。
#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
# 客户端在与zookeeper交互过程中会产生非常多的日志,而且zookeeper也会将内存中的数据作为snapshot保存下来,这些数据是不会被自动删除的,这样磁盘中这样的数据就会越来越多。不过可以通过这两个参数来设置,让zookeeper自动删除数据。autopurge.purgeInterval就是设置多少小时清理一次。而autopurge.snapRetainCount是设置保留多少个snapshot,之前的则删除。
# 不过如果你的集群是一个非常繁忙的集群,然后又碰上这个删除操作,可能会影响zookeeper集群的性能,所以一般会让这个过程在访问低谷的时候进行,但是遗憾的是zookeeper并没有设置在哪个时间点运行的设置,所以有的时候我们会禁用这个自动删除的功能,而在服务器上配置一个cron,然后在凌晨来干这件事。
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

Step6: 使用cmd,进入E:\zookeeper\zookeeper-3.3.6\bin,运行zkServer.cmd,启动服务,如下图(port:2181是zookeeper的专用监听端口)。
这里写图片描述

创建MAVEN项目

项目结构:
DubboDemo 工程,主工程,主要导入公共jar包等
dubbo-api : 工程 存放公共接口;
dubbo-consumer : 工程 调用远程服务(消费者);
dubbo-provider : 工程 提供远程服务(服务者)。
(注:每个工程都是独立的,通过maven 引入父子关系)
这里写图片描述

1、新建MAVEN项目 DubboDemo
导入所需要的jar包依赖。

<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>DubboDemo</groupId>
  <artifactId>DubboDemo</artifactId>
  <packaging>pom</packaging>  
  <version>0.0.1-SNAPSHOT</version>

    <modules>  
        <module>dubbo-api</module>  
        <module>dubbo-consumer</module>  
        <module>dubbo-provider</module>  
    </modules>  

    <name>DubboDemo Maven Webapp</name>  
    <url>http://maven.apache.org</url>  
    <properties>  
        <maven.compiler.source>1.8</maven.compiler.source>  
        <maven.compiler.target>1.8</maven.compiler.target>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  

        <!-- spring版本号 -->  
        <spring.version>4.2.5.RELEASE</spring.version>  

        <!-- mybatis版本号 -->  
        <mybatis.version>3.2.8</mybatis.version>  

        <!-- mysql驱动版本号 -->  
        <mysql-driver.version>5.1.29</mysql-driver.version>  

        <!-- log4j日志包版本号 -->  
        <slf4j.version>1.7.18</slf4j.version>  
        <log4j.version>1.2.17</log4j.version>  

    </properties>  
    <dependencies>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>3.8.1</version>  
            <scope>test</scope>  
        </dependency>  
        <!-- 添加jstl依赖 -->  
        <dependency>  
            <groupId>jstl</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
        </dependency>  

        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>7.0</version>  
        </dependency>  

        <!-- 添加junit4依赖 -->  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.11</version>  
            <!-- 指定范围,在测试时才会加载 -->  
            <scope>test</scope>  
        </dependency>  

        <!-- 添加spring核心依赖 -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-oxm</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</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-webmvc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <!-- 添加mybatis依赖 -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>${mybatis.version}</version>  
        </dependency>  

        <!-- 添加mybatis/spring整合包依赖 -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>1.2.2</version>  
        </dependency>  

        <!-- 添加mysql驱动依赖 -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>${mysql-driver.version}</version>  
        </dependency>  
        <!-- 添加数据库连接池依赖 -->  
        <dependency>  
            <groupId>commons-dbcp</groupId>  
            <artifactId>commons-dbcp</artifactId>  
            <version>1.2.2</version>  
        </dependency>  

        <!-- 添加fastjson -->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>fastjson</artifactId>  
            <version>1.2.22</version>  
        </dependency>  

        <!-- 添加日志相关jar包 -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>${log4j.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  

        <!-- log end -->  
        <!-- 映入JSON -->  
        <dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-mapper-asl</artifactId>  
            <version>1.9.13</version>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-core</artifactId>  
            <version>2.8.0</version>  
        </dependency>  
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
            <version>2.8.0</version>  
        </dependency>  

        <dependency>  
            <groupId>commons-fileupload</groupId>  
            <artifactId>commons-fileupload</artifactId>  
            <version>1.3.1</version>  
        </dependency>  

        <dependency>  
            <groupId>commons-io</groupId>  
            <artifactId>commons-io</artifactId>  
            <version>2.4</version>  
        </dependency>  

        <dependency>  
            <groupId>commons-codec</groupId>  
            <artifactId>commons-codec</artifactId>  
            <version>1.9</version>  
        </dependency>  

        <dependency>  
            <groupId>org.quartz-scheduler</groupId>  
            <artifactId>quartz</artifactId>  
            <version>2.2.1</version>  
        </dependency>  

        <dependency>  
            <groupId>org.apache.shiro</groupId>  
            <artifactId>shiro-core</artifactId>  
            <version>1.3.2</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.shiro</groupId>  
            <artifactId>shiro-web</artifactId>  
            <version>1.3.2</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.shiro</groupId>  
            <artifactId>shiro-spring</artifactId>  
            <version>1.3.2</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.shiro</groupId>  
            <artifactId>shiro-ehcache</artifactId>  
            <version>1.3.2</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.zookeeper</groupId>  
            <artifactId>zookeeper</artifactId>  
            <version>3.4.9</version>  
        </dependency>  
        <!-- dubbo -->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>dubbo</artifactId>  
            <version>2.5.3</version>  
            <exclusions>  
                <exclusion>  
                    <groupId>org.springframework</groupId>  
                    <artifactId>spring</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <dependency>  
            <groupId>com.101tec</groupId>  
            <artifactId>zkclient</artifactId>  
            <version>0.10</version>  
        </dependency>  
        <dependency>  
            <groupId>DubboDemo</groupId>  
            <artifactId>dubbo-api</artifactId>  
            <version>0.0.1-SNAPSHOT</version>  
        </dependency>
    </dependencies>  
    <build>  
        <finalName>DubboDemo</finalName>  
    </build>  
</project>

2、在此项目下新建MAVEN项目:dubbo-api
创建接口DemoService

package com.dubbo.demo;

import java.util.List;

public interface DemoService {
    List<String> getPermissions(Long id);
}

3、创建dubbo-provider的MAVEN项目
加入公共接口所在的依赖

<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">
  <parent>
    <artifactId>DubboDemo</artifactId>
    <groupId>DubboDemo</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>dubbo-provider</artifactId>

</project>

实现接口实现

package com.dubbo.demo.impl;

import java.util.ArrayList;
import java.util.List;

import com.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService{

    public List<String> getPermissions(Long id) {
        // TODO Auto-generated method stub
        List<String> demo = new ArrayList<String>();
        demo.add(String.format("Permission_%d", id - 1));
        demo.add(String.format("Permission_%d", id));
        demo.add(String.format("Permission_%d", id + 1));
        return demo;
    }

}

添加Spring配置声明文件 Provider.xml 此文件放在target/classes文件夹下

<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->  
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>  
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->  
    <dubbo:registry address="zookeeper://localhost:2181"/>  
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->  
    <dubbo:service interface="com.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />  
    <!--具体实现该接口的 bean-->  
    <bean id="demoService" class="com.dubbo.demo.impl.DemoServiceImpl"/>  
</beans>

添加启动远程服务类Provider:

package com.dubbo.demo.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws IOException {  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");  
        System.out.println(context.getDisplayName() + ": here");  
        context.start();  
        System.out.println("服务已经启动...");  
        System.in.read();  
    }  

}

4、创建dubbo-consumer的MAVEN项目
通过Spring引用服务

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

  <parent>
    <artifactId>DubboDemo</artifactId>
    <groupId>DubboDemo</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>
  <artifactId>dubbo-consumer</artifactId>
</project>

添加Spring配置声明文件 consumer.xml 此文件放在target/classes文件夹下

"><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="demotest-consumer" owner="programmer" organization="dubbox"/>  
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->  
    <dubbo:registry address="zookeeper://localhost:2181"/>  
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->  
    <dubbo:reference id="permissionService" interface="com.dubbo.demo.DemoService"/>  
</beans>  

创建Consumer类, 启动Consumer,调用远程服务

package com.dubbo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.demo.DemoService;

public class Consumer {
    public static void main(String[] args) {  
        //测试常规服务  
        ClassPathXmlApplicationContext context =  
                new ClassPathXmlApplicationContext("consumer.xml");  
        context.start();  
        System.out.println("consumer start");  
        DemoService demoService = context.getBean(DemoService.class);  
        System.out.println("consumer");  
        System.out.println(demoService.getPermissions(1L));  
    }  
}

5、运行项目,先确保provider已被运行后再启动consumer模块
顺序:zookeeper –> dubbo-provider –> dubbo-consumer
运行结果如下:
这里写图片描述

dubbo-admin 搭建

提供界面化管理dubbo 服务
下载地址 :https://github.com/apache/incubator-dubbo
下载之后的目录如下图:
这里写图片描述
找到dubbo-admin,将其进行打war包
进入cmd,dubbo-admin 根目录,执行 mvn package -Dmaven.skip.test=true
这里写图片描述
这里写图片描述
(如果打不成功,可以下载我的dubbo-admin.war包
将 dubbo-admin-2.5.4.war拷出放到tomcat的webapps里,因为zookeeper默认使用的是8080,为避免冲突,tomcat的 conf\server.xml 文件,把启动端口改成8090(不会起冲突的端口都可以)。

紧接着运行tomcat,war包将自动解压,为避免覆盖。解压完毕时,停掉tomcat,把war包删掉,此时进入WEB-INF目录,dubbo.properties文件里配置信息:

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

回到zookeeper文件夹,找到bin目录下的zkServer,双击将它启动。启动完毕,再启动刚刚配置的tomcat。浏览器输入http://localhost:8090/dubbo-admin-2.5.4/,输入账号root,密码root。进入页面如下:
这里写图片描述
可以对其进行管理了
这里写图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值