Spring Boot整合Zookeeper详细教程

目录

    • 1.Curator介绍
    • 2.创建springboot项目
    • 3.引⼊Curator
    • 4.application.yml配置⽂件
    • 5.读取配置⽂件注入到容器
    • 6.注⼊CuratorFramework
    • 7.添加测试方法
    • 8.执行测试方法报错解决

注意我这里用的是官方最稳定的版本3.7.1,版本之间有个别命令是有差距的!

本篇文章的示例SpringBoot和Zookeeper客户端以及zookeeper都是最新版本!

1.Curator介绍

Curator是Netflix公司开源的⼀套zookeeper客户端框架,Curator是对Zookeeper⽀持最好的客户端框架。Curator封装了⼤部分Zookeeper的功能,⽐如Leader选举、分布式锁等,减少了技术⼈员在使⽤Zookeeper时的底层细节开发⼯作。

2.创建springboot项目

Spring Boot整合Zookeeper详细教程_第1张图片

3.引⼊Curator


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.7.0version>
		<relativePath/>
	parent>
	<groupId>com.gzl.cngroupId>
	<artifactId>spring-boot-curator-zkartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>spring-boot-curator-zkname>
	<description>Demo project for Spring Bootdescription>
	<properties>
		<java.version>1.8java.version>
	properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>

        
		<dependency>
			<groupId>org.apache.curatorgroupId>
			<artifactId>curator-frameworkartifactId>
			<version>5.2.1version>
		dependency>
		 <dependency>
			<groupId>org.apache.curatorgroupId>
			<artifactId>curator-recipesartifactId>
			<version>5.2.1version>
		dependency>
        
        <dependency>
			<groupId>org.apache.zookeepergroupId>
			<artifactId>zookeeperartifactId>
			<version>3.8.0version>
		dependency>

		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<version>1.18.22version>
			<scope>providedscope>
		dependency>

		
		<dependency>
			 <groupId>org.springframework.bootgroupId>
			 <artifactId>spring-boot-configuration-processorartifactId>
			 <optional>trueoptional>
		dependency>

		<dependency>
			<groupId>junitgroupId>
			<artifactId>junitartifactId>
			<scope>4.12scope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.pluginsgroupId>
				<artifactId>maven-resources-pluginartifactId>
				<version>3.1.0version>
			plugin>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>

4.application.yml配置⽂件

application.yml和application.properties是都可以的,只不过他们的格式不一样。

curator:
  #重试retryCount次,当会话超时出现后,curator会每间隔elapsedTimeMs毫秒时间重试一次,共重试retryCount次。
  retryCount: 5
  elapsedTimeMs: 5000
  #服务器信息
  connectString: 127.0.0.1:2181
  #会话超时时间设置
  sessionTimeoutMs: 60000
  #连接超时时间
  connectionTimeoutMs: 5000

5.读取配置⽂件注入到容器

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "curator")
public class WrapperZK {
    private int retryCount;
    private int elapsedTimeMs;
    private String connectString;
    private int sessionTimeoutMs;
    private int connectionTimeoutMs;
}

6.注⼊CuratorFramework

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CuratorConfig {
    @Autowired
    WrapperZK wrapperZk;

    /**
     * 这里的start就是创建完对象放到容器后,需要调用他的start方法
     *
     * @return
     */
    @Bean(initMethod = "start")
    public CuratorFramework curatorFramework() {
        return CuratorFrameworkFactory.newClient(
                wrapperZk.getConnectString(),
                wrapperZk.getSessionTimeoutMs(),
                wrapperZk.getConnectionTimeoutMs(),
                new RetryNTimes(wrapperZk.getRetryCount(), wrapperZk.getElapsedTimeMs()));
    }
}

7.添加测试方法

在这里添加测试方法即可!

Spring Boot整合Zookeeper详细教程_第2张图片

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootCuratorZkApplication.class)
class SpringBootCuratorZkApplicationTests {

    @Autowired
    CuratorFramework curatorFramework;

    /**
     * 创建节点
     *
     * @throws Exception
     */
    @Test
    void createNode() throws Exception {
        // 添加持久节点
        String path = curatorFramework.create().forPath("/curator-node");
        System.out.println(String.format("curator create node :%s successfully.", path));

        // 添加临时序号节点,并赋值数据
        String path1 = curatorFramework.create()
                .withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
                .forPath("/curator-node", "some-data".getBytes());
        System.out.println(String.format("curator create node :%s successfully.", path));

        // System.in.read()目的是阻塞客户端关闭,我们可以在这期间查看zk的临时序号节点
        // 当程序结束时候也就是客户端关闭的时候,临时序号节点会消失
        System.in.read();
    }

    /**
     * 获取节点
     *
     * @throws Exception
     */
    @Test
    public void testGetData() throws Exception {
        // 在上面的方法执行后,创建了curator-node节点,但是我们并没有显示的去赋值
        // 通过这个方法去获取节点的值会发现,当我们通过Java客户端创建节点不赋值的话默认就是存储的创建节点的ip
        byte[] bytes = curatorFramework.getData().forPath("/curator-node");
        System.out.println(new String(bytes));
    }

    /**
     * 修改节点数据
     *
     * @throws Exception
     */
    @Test
    public void testSetData() throws Exception {
        curatorFramework.setData().forPath("/curator-node", "changed!".getBytes());
        byte[] bytes = curatorFramework.getData().forPath("/curator-node");
        System.out.println(new String(bytes));
    }

    /**
     * 创建节点同时创建⽗节点
     *
     * @throws Exception
     */
    @Test
    public void testCreateWithParent() throws Exception {
        String pathWithParent = "/node-parent/sub-node-1";
        String path = curatorFramework.create().creatingParentsIfNeeded().forPath(pathWithParent);
        System.out.println(String.format("curator create node :%s successfully.", path));
    }

    /**
     * 删除节点(包含子节点)
     *
     * @throws Exception
     */
    @Test
    public void testDelete() throws Exception {
        String pathWithParent = "/node-parent";
        curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(pathWithParent);
    }
}

8.执行测试方法报错解决

Spring Boot整合Zookeeper详细教程_第3张图片
上面那个闪电意思是install的时候跳过maven测试阶段,之所以要跳过测试阶段是因为假如不跳过他会验证测试方法,例如我们创建节点的方法,有时候我们节点已经创建了,但是他还会验证,所以就会报错。

Spring Boot整合Zookeeper详细教程_第4张图片

源码地址:https://gitee.com/gzl_com/spring-cloud.git

你可能感兴趣的:(zookeeper,spring,boot,java-zookeeper,zookeeper)