zookeeper 简介
ZooKeeper是一种分布式协调服务,用于管理大型主机。在分布式环境中协调和管理服务是一个复杂的过程。ZooKeeper通过其简单的架构和API解决了这个问题。 ZooKeeper允许开发人员专注于核心应用程序逻辑,而不必担心应用程序的分布式特性。
ZooKeeper框架最初是在“Yahoo!”上构建的,用于以简单而稳健的方式访问他们的应用程序。后来,Apache ZooKeeper成为Hadoop,HBase和其他分布式框架使用的有组织服务的标准。例如,Apache HBase使用ZooKeeper跟踪分布式数据的状态。本教程将介绍ZooKeeper的基础知识,如何在分布式环境中安装和部署ZooKeeper集群,最后总结了一些使用Java编程示例和应用程序示例。
zookeeper下载安装
-
zookeeper下载:前往官网下载即可,https://downloads.apache.org/zookeeper/zookeeper-3.6.0/apache-zookeeper-3.6.0-bin.tar.gz
注意:一定要下载
apache-zookeeper-3.6.0-bin.tar.gz
带有 bin 的安装包,不要下载apache-zookeeper-3.6.0.tar.gz
这个 ,下载后者会导致安装完成后无法启动zookeeper报Starting zookeeper ... FAILED TO START
错误 -
安装zookeeper之前要确保已配置好JDK环境,因为zookeeper依赖于JDK运行,建议安装
1.8
以上版本JDK -
将下载好的安装包,上传至linux系统中,我放在了
/root/soft
文件夹下了,该文件夹 我习惯性放置一些安装包的压缩包,然后解压后的文件夹我习惯性放在/usr/zookeeper
文件夹下。看个人喜好 -
使用命令
cd /root/soft
进入soft
文件夹下,使用命令tar zxvf apache-zookeeper-3.6.0-bin.tar.gz
进行解压。 -
使用命令
mv apache-zookeeper-3.6.0-bin /usr/zookeeper
将解压后文件移动至/usr/zookeeper
文件夹 -
使用
/usr/java/apache-zookeeper-3.6.0-bin/conf
进入conf
文件夹下 ,使用命令cp zoo_sample.cfg zoo.cfg
将zoo_sample.cfg
文件复制并重命名为zoo.cfg
文件.文件中有如下属性#这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime #时间就会发送一个心跳。 tickTime=2000 # 初始连接时能容忍的最多心跳数 initLimit=10 # 请求和答应最多能容忍的心跳数 syncLimit=5 # 顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。 dataDir=/tmp/zookeeper # 这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。 clientPort=2181
-
进入bin 文件夹下
/usr/java/apache-zookeeper-3.6.0-bin/bin
#启动zookeeper ./zkServer.sh start # 查看ZK服务状态: ./zkServer.sh status # 停止ZK服务: ./zkServer.sh stop #重启ZK服务: ./zkServer.sh restart
-
出现如图所示,表示zookeeper启动成功。
向zookeeper注册服务
-
使用Maven建立模块
cloud-provider-payment8004
-
修改
pom
文件<?xml version="1.0" encoding="UTF-8"?> <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>cloud2020</artifactId> <groupId>com.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8004</artifactId> <dependencies> <!--SPringCloud 依赖--> <dependency> <groupId>com.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <!-- spring-boot-starter-web 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--监控依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- zookeeper依赖 spring-cloud-starter-zookeeper-discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> <!-- SpringBoot热部署依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
-
创建
yml
文件server: port: 8004 spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 182.92.128.143:2181 #此地址问zookeeper服务器访问地址+端口号
-
创建主启动类
@SpringBootApplication @EnableDiscoveryClient//加入此注解 public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
-
建立测试
controller
类@RestController public class PaymentController { //直接获取yml中的端口号 @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/zk") public String paymentzk(){ //用于测试,直接在浏览器上打印出端口号+UUID随机数 return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString(); } }
-
启动该项目,将此服务注册进
zookeeper
服务中心 -
使用命令
./zkCli.sh
zookeeper客户端 -
使用命令
ls /
查看节点,会发现有[services, zookeeper]
两个节点,查看ls /services
节点如图
-
浏览器测试,出现如图表示成功。
可能出现的问题
-
启动
cloud-provider-payment8004
时后台报错 -
原因:由于zookeeper jar包冲突导致的问题
-
解决:排除jar包
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zk 3.4,9版本--> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>