1.部署集群
前提:安装了docker及docker-compose
docker-compose.yml
version: '3.3'
services:
zoo1:
image: zookeeper:3.5.9
hostname: zoo1
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
TZ: Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime #同步时间
zoo2:
image: zookeeper:3.5.9
hostname: zoo2
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181
TZ: Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime #同步时间
zoo3:
image: zookeeper:3.5.9
hostname: zoo3
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
TZ: Asia/Shanghai
volumes:
- /etc/localtime:/etc/localtime #同步时间
cd到docker-compose.yml目录运行以下命令:
docker-compose up
该命令自动查找目录下ocker-compose的文件
下载ZooKeeper并解压(主要是为了里面的执行工具)
下载链接:https://zookeeper.apache.org/releases.html
mac docker可视化如下
进入ZooKeeper解压目录,可以尝试下各种命令
sh zkCli.sh
[zk: localhost:2181(CONNECTED) 6] create /test 0
Created /test
[zk: localhost:2181(CONNECTED) 7] get /test
0
[zk: localhost:2181(CONNECTED) 8] stat /test
cZxid = 0x200000014
ctime = Thu Mar 11 15:31:22 CST 2021
mZxid = 0x200000014
mtime = Thu Mar 11 15:31:22 CST 2021
pZxid = 0x200000014
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0
[zk: localhost:2181(CONNECTED) 9] ls /
[test, zookeeper]
查看集群状态
zoo1:follower
zoo2:follower
zoo3:leader
2.springboot 整合测试
maven添加依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
application.yml
zookeeper:
address: localhost
timeout: 4000
ZooKeeperConfig.class
@Configuration
public class ZooKeeperConfig {
@Autowired
private ZooKeeperConfigProperties zooKeeperConfigProperties;
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "zookeeper")
class ZooKeeperConfigProperties {
private String address;
private Integer timeout;
}
@Bean
public ZooKeeper zooKeeper(){
CountDownLatch countDownLatch = new CountDownLatch(1);
try {
return new ZooKeeper(zooKeeperConfigProperties.getAddress(), zooKeeperConfigProperties.getTimeout(), new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("");
}
});
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
ZookeeperUtil.class
@Component
public class ZookeeperUtil {
@Autowired
public ZooKeeper zooKeeper;
public List<String> getChildren(String path) throws KeeperException, InterruptedException {
return zooKeeper.getChildren(path, false);
}
public Stat exists(String path) throws KeeperException, InterruptedException {
return zooKeeper.exists(path, false);
}
public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException {
return zooKeeper.create(path, data, acl, createMode);
}
/**
* 永久节点(开放模式)
*
* @param path
* @param data
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public String create(String path, byte[] data) throws KeeperException, InterruptedException {
return zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public String getData(String path) throws KeeperException, InterruptedException {
byte[] data = zooKeeper.getData(path, false, null);
return new String(data);
}
public void delete(String path) throws KeeperException, InterruptedException {
zooKeeper.delete(path, Info.REVISION);
}
public Stat setData(String path, byte[] data) throws KeeperException, InterruptedException {
return zooKeeper.setData(path, data, Info.REVISION);
}
ZookeeperController.class
@RestController
@RequestMapping("zoo")
public class ZookeeperController {
@Autowired
public ZookeeperUtil zookeeperUtil;
@GetMapping("exists")
public String exists(@RequestParam("path") String path) {
try {
Stat exists = zookeeperUtil.exists(path);
return exists.toString();
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
@GetMapping("get-children")
public String getChildren(@RequestParam("path") String path) {
try {
List<String> children = zookeeperUtil.getChildren(path);
return Arrays.toString(children.toArray());
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
@PostMapping("create")
public String create(@RequestParam("path") String path,
@RequestParam("data") String data) {
try {
return zookeeperUtil.create(path, data.getBytes(StandardCharsets.UTF_8));
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
@GetMapping("get-data")
public String getData(@RequestParam("path") String path) {
try {
return zookeeperUtil.getData(path);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
3.postman调用测试
/zoo/exists:
/zoo/get-children
仅做学习了解