• 微服务系列之ZooKeeper注册中心03:zookeeper介绍与安装


    一、Zookeeper 介绍

    Apache ZooKeeper 是一个开放源码的分布式应用程序协调组件,是 Hadoop 和 Hbase 的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

    在微服务项目开发中 ZooKeeper 主要的角色是当做服务注册中心存在,我们将编写好的服务注册至 ZooKeeper 即可。

     

    二、ZooKeeper 安装

     

    环境准备

     

    ZooKeeper 在 Java 中运行,版本 1.8 或更高(JDK 8 LTS,JDK 11 LTS,JDK 12 - Java 9 和 10 不支持)

     

    下载

     

    ZooKeeper 下载地址:

    • https://zookeeper.apache.org/releases.html#download
    • https://archive.apache.org/dist/zookeeper/

     

    安装

     

    将文件上传至 Linux 服务器。

     

    单机版

     

    创建目录/解压

     

    创建 zookeeper 目录。

    mkdir -p /usr/local/zookeeper

    将文件解压至该目录。

    tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz -C /usr/local/zookeeper/

    创建数据目录、日志目录。

    mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
    mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/log

     

    修改配置文件

     

    # 进入配置文件目录
    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/conf/
    # ZooKeeper 启动默认加载名为 zoo.cfg 的配置文件,复制一份命名为 zoo.cfg
    cp zoo_sample.cfg zoo.cfg
    # 修改配置文件
    vi zoo.cfg
     

    主要修改数据目录dataDir、日志目录dataLogDir两处即可,修改结果如下:

    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
    dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/log
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1
    
    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true
    
    
    启动/关闭

     

    启动。

    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
    bin/zkServer.sh start
    ---------------------------------------------------------------------------------
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
     

    关闭。

    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
    bin/zkServer.sh stop
    ---------------------------------------------------------------------------------
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED
    
    

    集群版

     

    再准备两台机器,和刚才单机的机器加一起构成一个集群环境(如果电脑跑不动就改为一台机器跑三个进程的方式)。

     

    创建目录/解压

     

    创建 zookeeper 目录。

    mkdir -p /usr/local/zookeeper

    将文件解压至该目录。

    tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz -C /usr/local/zookeeper/

    创建数据目录、日志目录。

    mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
    mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/log

     

    myid 文件

     

    在 data 目录下创建 myid 文件,文件中就只写个 1 即可,其他两个机器分别写 2 3

    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data/
    vi myid

     

    修改配置文件

     

    # 进入配置文件目录
    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/conf/
    # zookeeper 启动默认加载名为 zoo.cfg 的配置文件,所以复制一份命名为 zoo.cfg
    cp zoo_sample.cfg zoo.cfg
    # 修改配置文件
    vi zoo.cfg
     

    主要修改:

    • 数据目录dataDir
    • 日志目录dataLogDir
    • 端口clientPort(如果是一台机器的伪集群,需要修改 2181 端口,比如:2181、2182、2183)
    • 集群配置(如果是一台机器的伪集群,需要修改 2888 和 3888 的端口,比如:2888、2889、2890 和 3888、3889、3890)

     

    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
    dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/log
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1
    
    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true
    # 集群配置
    # server.1 中的 1 是 myid 文件中的内容,2888 用于集群内部通信,3888 用于选择 leader
    server.1=192.168.10.101:2888:3888
    server.2=192.168.10.102:2888:3888
    server.3=192.168.10.103:2888:3888
    
    
    启动/关闭

     

    启动。

    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
    bin/zkServer.sh start
    #################################################################################
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
     

    关闭。

    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
    bin/zkServer.sh stop
    #################################################################################
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED
    
    
    集群状态查看

     

    cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
    bin/zkServer.sh status
    ################################ 192.168.10.101 ################################
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost.
    Mode: follower
    ################################ 192.168.10.102 ################################
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost.
    Mode: leader
    ################################ 192.168.10.103 ################################
    ZooKeeper JMX enabled by default
    Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
    Client port found: 2181. Client address: localhost.
    Mode: follower
     

    看到以上信息说明 ZooKeeper 集群环境已搭建成功,接下来就可以通过 RPC 框架对接 ZooKeeper,将 ZooKeeper 作为我们的注册中心来使用。点击获取 java微服务架构spring全家桶视频教程。

  • 相关阅读:
    命令模式
    连接mysql数据库,创建用户模型
    管理信息系统的开发与管理
    加载静态文件,父模板的继承和扩展
    夜间模式的开启与关闭,父模板的制作
    完成登录与注册页面的前端
    JavaScript 基础,登录前端验证
    CSS实例:图片导航块
    导航,头部,CSS基础
    web基础,用html元素制作web页面
  • 原文地址:https://www.cnblogs.com/shsxt/p/13494880.html
Copyright © 2020-2023  润新知