SpringBoot整合Zookeeper

引入Jar包

复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.13</version>
</dependency>

<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>4.1.0</version>
	<exclusions>
		<exclusion>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</exclusion>
	</exclusions>
</dependency>

Zookeeper配置

Zookeeper配置连接信息

复制代码
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZookeeperConfig {

	@Bean("zookeeperClient")
	public CuratorFramework create() {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
		client.start();
		return client;
	}
}

Zookeeper 的使用

节点监控

复制代码
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.plf.zookeeper.service.NodeService;

@Component
@Order(1)
public class ZookeeperInit implements CommandLineRunner {

	private static final String ROOT_PATH = "/server";

	public TreeCache treeCache;
	
	@Autowired
	private CuratorFramework zookeeperClient;

	@Autowired
	private NodeService nodeService;

	@Override
	public void run(String... args) throws Exception {
		if (!nodeService.isExitNode(ROOT_PATH)) {
			nodeService.createNode(ROOT_PATH);
		}

		// 监视某个节点的数据变化
		treeCache = new TreeCache(zookeeperClient, ROOT_PATH);
		
		treeCache.getListenable().addListener(new TreeCacheListener() {
			@Override
			public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
				ChildData eventData = event.getData();
				switch (event.getType()) {
				case NODE_ADDED:
					System.out.println(eventData.getPath() + "节点添加" + eventData.getPath() + "\t添加数据为:"
							+ new String(eventData.getData()));
					break;
				case NODE_UPDATED:
					System.out.println(eventData.getPath() + "节点数据更新\t更新数据为:" + new String(eventData.getData())
							+ "\t版本为:" + eventData.getStat().getVersion());
					break;
				case NODE_REMOVED:
					System.out.println(eventData.getPath() + "节点被删除");
					break;
				default:
					break;
				}
			}
		});
		
		// 启动监视器对象
		treeCache.start();
	}
}

节点新增

复制代码
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NodeService {

	@Autowired
	private CuratorFramework zookeeperClient;
	
	public void createNode(String nodePath) {
		try {
			zookeeperClient.create().creatingParentsIfNeeded()
				.withMode(CreateMode.PERSISTENT)
				.forPath(nodePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean isExitNode(String path) {
		try {
			Stat stat = zookeeperClient.checkExists().forPath(path);
			if(stat == null) {
				return false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}

还有很多其他的方法可以自行扩展。

相关推荐
申阳2 分钟前
Day 22:SpringBoot4 + Tauri 2.0(VUE) 登录功能前后端联调
前端·后端·程序员
腾讯云云开发22 分钟前
【CloudBase MCP 升级福利】你的AI开发搭子已进化!晒出AI Coding项目领取周边礼品
前端·后端·小程序·云开发
haiyu柠檬26 分钟前
Ruby On Rails 笔记6——常用回调上
笔记·后端·ruby on rails
by__csdn30 分钟前
第一章 (ASP.NET Core入门)第三节( 认识.NET Standard)
后端·c#·asp.net·.net·.netcore·f#·vb.net
数据小馒头34 分钟前
生成测试数据(一):分钟级构建百万级数据,测试数据库性能
后端
helloworld工程师37 分钟前
Dubbo应用开发之基于Dubbo协议的springboot规范性开发
spring boot·后端·dubbo
码途进化论41 分钟前
前端Docker多平台构建自动化实践
前端·javascript·后端
悟空码字42 分钟前
SpringBoot 整合 RabbitMQ:和这只“兔子”交朋友
java·后端·rabbitmq
BingoGo1 小时前
万物皆字符串 PHP 中的原始类型偏执
后端·php
Carve_the_Code1 小时前
订单ID容量升级:从40位到64位的架构演进
后端