• docker-compose搭建zookeeper集群


    搭建zookeeper集群

    1. 创建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
       ```
      
    2. docker-compose up -d 后台运行

    3. docker-compose config 查看配置

    4. docker exec -it <容器id> bash 交互式进入容器

    5. bin/zkServer.sh status 查看zookeeper集群状态(follower/leader)

    java使用zookeeper导入包和配置

    1. 导入包

       ```
               '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'
       ```
      
    2. 在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
      
       ```
      
    3. 代码示例

       ```
       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));
       ```
  • 相关阅读:
    DirectX标准规定 DirectX和OpenGL的不同
    Android 抽屉效果的导航菜单实现
    Servlet基础(三) Servlet的多线程同步问题
    Java微服务之Spring Boot on Docker
    Spring Cloud 微服务架构学习笔记与示例
    从你的全世界路过—一群程序员的稻城亚丁游记
    从一个国内普通开发者的视角谈谈Sitecore
    吴军《硅谷来信》思维导图笔记
    .NET Core微服务之基于Jenkins+Docker实现持续部署(Part 1)
    2018OKR年中回顾
  • 原文地址:https://www.cnblogs.com/yloved/p/11881128.html
Copyright © 2020-2023  润新知