1、使用zookeeper做注册中心
本示例为zookeeper为注册中心情况下,使用依赖可能有所不同:
maven依赖:(使用的spring cloud版本为:2021.0.5,zk版本为:3.6.4)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.4</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
zk配置:
@Configuration
@Slf4j
public class ZookeeperConfig {
@Resource
private ZookeeperProperties zookeeperProperties;
@Bean
public CuratorFramework zk(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(zookeeperProperties.getConnectString())
.retryPolicy(retryPolicy)
.namespace("sirius")
.build();
client.start();
return client;
}
}
zk service
/**
* @classDesc: zk工具
* @author: cyjer
* @date: 2023/1/30 9:53
*/
@Slf4j
@Component
public class ZookeeperService {
@Resource
private CuratorFramework zk;
/**
* 查询节点数据
*
* @param nodePath 节点
* @return {@link String}
*/
public String queryData(String nodePath) {
try {
Stat stat = zk.checkExists().forPath(nodePath);
if (stat != null) {
byte[] bytes = zk.getData().forPath(nodePath);
return new String(bytes, StandardCharsets.UTF_8);
}
return null;
} catch (Exception e) {
log.error("查询节点数据失败:", e);
return null;
}
}
/**
* 创建节点
*
* @param nodePath 。
* @param nodeData 。
* @return {@link String}
*/
public String create(String nodePath, String nodeData) {
try {
Stat stat = zk.checkExists().forPath(nodePath);
if (stat == null) {
return zk.create().forPath(nodePath, nodeData.getBytes());
} else {
return null;
}
} catch (Exception e) {
log.error("创建节点失败:", e);
return null;
}
}
/**
* 更新节点数据
*
* @param nodePath 。
* @param nodeData 。
* @return {@link Stat}
*/
public boolean update(String nodePath, String nodeData) {
try {
Stat stat = zk.checkExists().forPath(nodePath);
if (stat != null) {
stat = zk.setData().forPath(nodePath, nodeData.getBytes());
}
return stat != null;
} catch (Exception e) {
log.error("更新节点失败:", e);
return false;
}
}
/**
* 删除节点
*
* @param nodePath v
* @return {@link boolean}
*/
public boolean delete(String nodePath) {
try {
Stat stat = zk.checkExists().forPath(nodePath);
if (stat != null) {
zk.delete().forPath(nodePath);
}
return true;
} catch (Exception e) {
log.error("删除节点失败:", e);
return false;
}
}
/**
* 监听子节点 只能监听子节点,子节点的子节点不支持
*
* @param
* @return {@link }
*/
public boolean addWatchChildListener(String nodePath, AbstractChildListenerProcess processer) {
CuratorCache curatorCache = CuratorCache.builder(zk, nodePath).build();
CuratorCacheListener listener = CuratorCacheListener.builder()
.forPathChildrenCache(nodePath, zk, new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) {
log.info("监听到子节点变动,变动类型:{}", pathChildrenCacheEvent.getType().toString());
processer.process(curatorFramework, pathChildrenCacheEvent);
}
}).build();
curatorCache.listenable().addListener(listener);
curatorCache.start();
return true;
}
/**
* 监听子孙节点 支持子节点的子节点监听
*
* @param nodePath .
* @param nodePath .
* @return {@link }
*/
public boolean addWatchDescendantsListener(String nodePath, AbstractDescendantsListenerProcess processer) {
CuratorCache curatorCache = CuratorCache.builder(zk, nodePath).build();
CuratorCacheListener listener = CuratorCacheListener.builder()
.forTreeCache(zk, new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) {
log.info("监听到子节点变动,变动类型:{}", treeCacheEvent.getType().toString());
processer.process(curatorFramework, treeCacheEvent);
}
}).build();
curatorCache.listenable().addListener(listener);
curatorCache.start();
return true;
}
}
事件监听处理抽象类
public abstract class AbstractChildListenerProcess {
/**
* 处理子节点变更事件
*
* @param curatorFramework zk客户端
* @param pathChildrenCacheEvent 子节点事件
* @return {@link }
*/
public abstract void process(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent);
}
初始化监听:
zookeeperService.addWatchChildListener(Constraint.ROUTE_DEFINITION, routeProcesser);