docker-compose搭建zookeeper集群
搭建zookeeper集群
创建docker-compose.yml文件
``` version: '3.1' services: zoo1: image: zookeeper restart: always container_name: zoo1 ports: - 2181:2181 volumes: - /usr/local/docker/zookeeper/zoo1/data:/data - /usr/local/docker/zookeeper/zoo1/datalog:/datalog environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 zoo2: image: zookeeper restart: always container_name: zoo2 ports: - 2182:2181 volumes: - /usr/local/docker/zookeeper/zoo2/data:/data - /usr/local/docker/zookeeper/zoo2/datalog:/datalog environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 zoo3: image: zookeeper restart: always container_name: zoo3 ports: - 2183:2181 volumes: - /usr/local/docker/zookeeper/zoo3/data:/dada - /usr/local/docker/zookeeper/zoo3/datalog:/datalog environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 ```
- docker-compose up -d 后台运行
- docker-compose config 查看配置
- docker exec -it bash 交互式进入容器
bin/zkServer.sh status 查看zookeeper集群状态(follower/leader)
java使用zookeeper导入包和配置
导入包
``` 'org.apache.zookeeper:zookeeper:3.5.6', "org.apache.logging.log4j:log4j:2.12.1", 'org.apache.logging.log4j:log4j-1.2-api:2.12.1', 'org.apache.logging.log4j:log4j-core:2.12.1' ```
在resources目录下创建 log4j2.properties文件并配置
``` log4j.rootLogger=INFO,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n ```
代码示例
``` public static void main(String[] args) throws IOException, KeeperException, InterruptedException { String connect = "192.168.145.128:2181,192.168.145.128:2182,192.168.145.128:2183"; int timeout = 30000; ZooKeeper zooKeeper = null; ZooKeeper finalZooKeeper = zooKeeper; zooKeeper = new ZooKeeper(connect, timeout, new Watcher() { @Override public void process(WatchedEvent event) { // System.out.println("start"); // List<String> children = null; // try { // children = finalZooKeeper.getChildren("/hhh", true); // children.forEach(item -> System.out.println(item)); // } catch (KeeperException e) { // e.printStackTrace(); // } catch (InterruptedException e) { // e.printStackTrace(); // } // System.out.println("end"); // } } }); //必须连接好才能创建目录 System.out.println(zooKeeper.getState()); while (true) { if (ZooKeeper.States.CONNECTING == zooKeeper.getState()) { Thread.sleep(1000); } else { break; } } System.out.println(zooKeeper.getState()); // String path = zooKeeper.create("/hhh", "test11".getBytes() // , ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // System.out.println(path); List<String> children = zooKeeper.getChildren("/", true); children.forEach(item -> System.out.println(item)); byte[] data = zooKeeper.getData("/hhh", false, null); System.out.println(new String(data)); ```
相关推荐
Kafka 2020-09-18
iamdll 2020-07-29
deyu 2020-07-27
deyu 2020-07-21
Elmo 2020-07-19
deyu 2020-07-07
ZHANGYONGHAO0 2020-07-05
joynet00 2020-06-26
isHooky 2020-06-25
CobingLiu 2020-06-21
CobingLiu 2020-06-16
airfish000 2020-06-14
roddex00 2020-06-12
iamdll 2020-06-11
CobingLiu 2020-06-10
MrZhangAdd 2020-06-09
yixietianxia 2020-06-04