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;
	}
}

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

相关推荐
海绵波波1073 小时前
flask后端开发(10):问答平台项目结构搭建
后端·python·flask
网络风云5 小时前
【魅力golang】之-反射
开发语言·后端·golang
Q_19284999065 小时前
基于Spring Boot的电影售票系统
java·spring boot·后端
运维&陈同学6 小时前
【Kibana01】企业级日志分析系统ELK之Kibana的安装与介绍
运维·后端·elk·elasticsearch·云原生·自动化·kibana·日志收集
Javatutouhouduan8 小时前
如何系统全面地自学Java语言?
java·后端·程序员·编程·架构师·自学·java八股文
后端转全栈_小伵8 小时前
MySQL外键类型与应用场景总结:优缺点一目了然
数据库·后端·sql·mysql·学习方法
编码浪子9 小时前
Springboot高并发乐观锁
后端·restful
uccs9 小时前
go 第三方库源码解读---go-errorlint
后端·go
Mr.朱鹏10 小时前
操作002:HelloWorld
java·后端·spring·rabbitmq·maven·intellij-idea·java-rabbitmq
编程洪同学11 小时前
Spring Boot 中实现自定义注解记录接口日志功能
android·java·spring boot·后端