Step1、配置JAVA环境,检验环境:java -version
Step2、下载并解压zookeeper
# cd /usr/local
# https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
# tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
# cd apache-zookeeper-3.6.2-bin.tar.gz
Step3:重命名配置文件zoo_sample.cfg
# cp conf/zoo_sample.cfg conf/zoo.cfg
要启动ZooKeeper,您需要一个配置文件。这是一个示例,在conf / zoo.cfg中创建它:
# ZooKeeper使用的基本时间单位(毫秒)。它用于做心跳,并且最小会话超时将是tickTime的两倍。
tickTime=2000
# 存储内存数据库快照的位置,除非另有说明,否则存储数据库更新的事务日志。
dataDir=/var/lib/zookeeper
# 用于侦听客户端连接的端口
clientPort=2181
# 虚拟机启动客户端端口
admin.serverPort=8888
Step4:启动zookeeper
# bin/zkServer.sh start
Step5:检测是否成功启动
# bin/zkServer.sh status
Step6:检测是否成功启动,用zookeeper客户端连接下服务端
# bin/zkCli.sh
或
# bin/zkCli.sh -server localhost:2181
启动成功, 命令: ls / 可以看到默认有一个zookeeper
创建Maven项目,pom.xml添加以下配置
<dependencies>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入自定义的api通用包 例如:实体类,工具类-->
<dependency>
<groupId>com.lisy</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml文件配置
server:
port: 8082
# zookeeper 提供者服务注册进入 zookeeper服务端
spring:
application:
name: zookeeper-client
cloud:
zookeeper:
# zookeeper地址 zookeeper默认端口为 2181
connect-string: 192.168.0.72:2181
创建Main方法启动类
package com.lisy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled;
@SpringBootApplication
//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
@EnableDiscoveryClient
public class Application8082 {
public static void main(String[] args) {
SpringApplication.run(Application8082.class, args);
}
}
创建indexController测试控制器
package com.lisy.controller;
import com.lisy.result.ResultBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
@RestController
public class IndexController {
@GetMapping("/zk/get")
public ResultBody index() {
return ResultBody.success(UUID.randomUUID().toString());
}
}
zookeeper客户端命令查询当前已注册的服务
# ls /
可以看到如图下,说明注册成功
查看输入的服务名称
# ls /services
能看到zookeeper-client说明已成功注册进服务