Zookeeper 注册中心:单机部署

序言

本文给大家介绍 Zookeeper 单机部署流程、 如何与 Spring 整合使用。除此之外,还有 Zookeeper 作为注册中心与 SpringCloud 的整合流程。

一、部署流程

  1. 官网下载 Zookeeper 安装包

    image.png

  2. 解压安装包到指定目录

    image.png

  3. 进入 apache-zookeeper-3.8.4-bin/conf 目录,使用 cp zoo_sample.cfg zoo.cfg 命令拷贝 zoo_sample.cfg 到当前目录。

    image.png

  4. 进入 apache-zookeeper-3.8.4-bin 目录,使用 mkdir data 命令创建数据存放目录

    image.png

  5. 修改 apache-zookeeper-3.8.4-bin/conf/zoo.cfg 配置文件

    image.png

  6. 进入 apache-zookeeper-3.8.4-bin/bin 目录,执行 ./zkServer.sh start 命令启动 Zookeeper

    image.png

二、Zookeeper 服务管理

  1. 启动 Zookeeper 服务

    zkServer.sh start
    
  2. 停止 Zookeeper 服务

    zkServer.sh stop
    
  3. 查看 Zookeeper 服务状态

    zkServer.sh status
    
  4. 重启 Zookeeper 服务

    zkServer.sh restart
    

三、命令行客户端

  1. 连接 Zookeeper 服务

    ./zkCli.sh -server localhost:2181
    
    # 可以直接按如下方式使用,默认指向的服务端:端口是 localhost:2181
    ./zkCli.sh
    

    image.png

  2. 查看 Zookeeper 中的节点

    ls /path
    

    image.png

  3. 创建一个新的节点

    # 创建新节点可以在后面跟上数据也可以不跟
    create /path data
    

    image.png

  4. 删除节点

    delete /path
    

    image.png

  5. 设置节点的数据

    set /path data
    

    image.png

  6. 查看节点的详细信息

    get /path
    

    image.png

  7. 关闭连接

    quit
    

    image.png

四、Spring 整合 Zookeeper

  1. 引入依赖

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.7.3</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>2021.0.8</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
    <dependencies>
      <!-- 注册中心依赖 -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
      </dependency>
    </dependencies>
    
  2. application.yml 文件中编写配置

    spring:
      cloud:
        zookeeper:
          # 指定 Zookeeper 服务器地址
          connect-string: localhost:2181
    
  3. 使用案例

    @SpringBootTest
    public class AppTest {
    
        @Resource
        private CuratorFramework curatorFramework;
    
        @Test
        public void test() throws Exception {
            // 创建节点(增加数据)
            curatorFramework.create().forPath("/hello", "zookeeper".getBytes());
    
            // 获取节点数据(查询数据)
            curatorFramework.getData().forPath("/hello");
    
            // 更新节点数据(修改数据)
            curatorFramework.setData().forPath("/hello", "world".getBytes());
    
            // 删除节点(删除数据)
            curatorFramework.delete().forPath("/hello");
        }
    }
    

五、Zookeeper 注册中心

  1. 引入依赖

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.7.3</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>2021.0.8</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
    
      <!-- 注册中心依赖 -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
      </dependency>
    </dependencies>
    
  2. application.yml 文件中编写配置

    spring:
      cloud:
        zookeeper:
          # 指定 Zookeeper 服务器地址
          connect-string: localhost:2181
      application:
        # 指定当前服务名称
        name: user-service
    
  3. 启动服务,查看服务注册情况

    image.png

推荐阅读

  1. 深入理解 JavaScript 中的 Promise、async 和 await
  2. 探索生产者/消费者模式:解决并发编程中的资源竞争
  3. 深入探究 Spring Boot Starter:从概念到实践
  4. RBAC 权限设计(五)
  5. Docker Compose:简化多容器应用部署
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值