---
title: "Dubbo框架应用之（三）--Zookeeper注册中心、管理控制台的安装及讲解"
author: "xiaowei2002"
date: "2015-09-17T14:30:09Z"
tags: []
categories: ["企业架构"]
slug: "dubbo-zookeeper"
draft: false
lastmod: "2015-09-17T14:30:09Z"
id: 2242494
url: "https://xiaowei2002.iteye.com/blog/2242494"
---

我是在linux下使用dubbo-2.3.3以上版本的zookeeper注册中心客户端。[Zookeeper](http://zookeeper.apache.org/)是Apache Hadoop的子项目，强度相对较好，建议生产环境使用该注册中心。Dubbo未对Zookeeper服务器端做任何侵入修改，只需安装原生的Zookeeper服务器即可，所有注册中心逻辑适配都在调用Zookeeper客户端时完成。

# []()Zooleeper安装

 

下载解压

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;wget http://www.apache.org/dist//zookeeper/zookeeper-3.3.3/zookeeper-3.3.3.tar.gz
2. tar zxvf zookeeper-3.3.3.tar.gz
3. cd zookeeper-3.3.3
4. cp conf/zoo\_sample.cfg conf/zoo.cfg&lt;/span&gt;

**配置:**

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;vi conf/zoo.cfg&lt;/span&gt;

zoo.cfg的内容如下：(其中data目录需改成你真实输出目录)

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

01. &lt;span style="font-size:18px;"&gt;# The number of milliseconds of each tick
02. tickTime=2000
03. # The number of ticks that the initial
04. # synchronization phase can take
05. #initLimit=10
06. # The number of ticks that can pass between
07. # sending a request and getting an acknowledgement
08. #syncLimit=5
09. # the directory where the snapshot is stored.
10. dataDir=/home/dubbo/zookeeper-3.3.3/data
11. # the port at which the clients will connect
12. clientPort=2181&lt;/span&gt;

如果需要集群，zoo.cfg的内容如下：(其中data目录和server地址需改成你真实部署机器的信息)

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;tickTime=2000
2. initLimit=10
3. syncLimit=5
4. dataDir=/home/dubbo/zookeeper-3.3.3/data
5. clientPort=2181
6. server.1=ip1
7. server.2=ip2&lt;/span&gt;

并在data目录下放置myid文件：(上面zoo.cfg中的dataDir)

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;mkdir data
2. vi myid&lt;/span&gt;

 

启动

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;./bin/zkServer.sh start&lt;/span&gt;

![](http://img.blog.csdn.net/20150626210015097?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlzaGVoZQ=%3D%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter)

停止

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;./bin/zkServer.sh stop&lt;/span&gt;

# []()**管理控制台安装**

     管理控制台为内部裁剪版本，开源部分主要包含：路由规则，动态配置，服务降级，访问控制，权重调整，负载均衡，等管理功能。

安装

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;wget http://apache.etoak.com/tomcat/tomcat-6/v6.0.35/bin/apache-tomcat-6.0.35.tar.gz
2. tar zxvf apache-tomcat-6.0.35.tar.gz
3. cd apache-tomcat-6.0.35
4. rm -rf webapps/ROOT
5. wget http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-admin/2.4.1/dubbo-admin-2.4.1.war
6. unzip dubbo-admin-2.4.1.war -d webapps/ROOT&lt;/span&gt;

**配置:** (或将dubbo.properties放在当前用户目录下)

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;vi webapps/ROOT/WEB-INF/dubbo.properties
2. dubbo.registry.address=zookeeper://127.0.0.1:2181
3. dubbo.admin.root.password=root
4. dubbo.admin.guest.password=guest&lt;/span&gt;

启动

 

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;./bin/startup.sh&lt;/span&gt;

![](http://img.blog.csdn.net/20150626210130250?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlzaGVoZQ=%3D%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter)

 

停止

**\[plain]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;./bin/shutdown.sh&lt;/span&gt;

访问（注意我个人修改了默认的端口）（用户:root,密码:root）

` `

**\[html]** [view plain](http://blog.csdn.net/lishehe/article/details/46653915 "view plain")[copy](http://blog.csdn.net/lishehe/article/details/46653915 "copy")[print](http://blog.csdn.net/lishehe/article/details/46653915 "print")[?](http://blog.csdn.net/lishehe/article/details/46653915 "?")

1. &lt;span style="font-size:18px;"&gt;http://127.0.0.1:8090/
2. &lt;/span&gt;

 

#### []()**注册中心抽象**

     Dubbo将注册中心进行抽象，使得它可以外接不同的存储媒介给注册中心提供服务，有ZooKeeper，Memcached，Redis等。Dubbo抽象后，用户可以进行扩展，我们通过分析ZooKeeper这个实现来了解注册中心的低层。  
     进过抽象之后，用户 只需要实现对应的Registry和RegistryFactory就可以了，ZooKeeper就是实现了ZookeeperRegistry，和ZookeeperRegistryFactory。

     ZookeeperRegistryFactory的实现很简单，就是返回一个ZookeeperRegistry实例，所以主要的东西是在ZookeeperRegistry中实现的，在ZookeeperRegistry用户需要实现注册URL，注销URL，URL订阅，URL注销订阅和URL查询，在这里面设计到Zookeeper服务端的调用，都被封装到ZookeeperClient中，ZookeeperClient服务进行Server连接，断链；资源的CRUD。

 

#### []()**ZooKeeper的价值**

      由于引入了ZooKeeper作为存储媒介，也就把ZooKeeper的特性引进来。  
      首先是负载均衡，单注册中心的承载能力是有限的，在流量达到一定程度的时候就需要分流，负载均衡就是为了分流而存在的，一个ZooKeeper群配合相应的Web应用就可以很容易达到负载均衡；  
     资源同步，单单有负载均衡还不够，节点之间的数据和资源需要同步，ZooKeeper集群就天然具备有这样的功能；  
     命名服务，将树状结构用于维护全局的服务地址列表，服务提供者在启动的时候，向ZK上的指定节点/dubbo/${serviceName}/providers目录下写入自己的URL地址，这个操作就完成了服务的发布。其他特性还有Mast选举，分布式锁等。