spring boot中zookeeper使用
spring boot中zookeeper使用
文章目錄
一、簡介
這裡介紹zookeeper在spring boot中的使用,直接程式碼示例。
二、示例
2.1 新增maven依賴
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dragon.study</groupId>
<artifactId>spring-boot-zookeeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-zookeeper</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 新增配置檔案application.yaml
application.yaml檔案內容如下:
server:
port: 10020
spring:
application:
name: spring-boot-zookeeper
zookeeper:
connectString: localhost:2181
maxRetries: 3 # 最大重試次數
baseSleepTimeMs: 1000 #初始休眠時間
2.3 定義zookeeper客戶端
ZkClientFactoryBean.java類如下:
package com.dragon.study.springbootzookeeper.config;
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.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class ZkClientFactoryBean implements FactoryBean<CuratorFramework> {
@Value("${zookeeper.connectString:localhost:2181}")
private String connectString;
@Value("${zookeeper.maxRetries:3}")
private int maxRetries;
@Value("${zookeeper.baseSleepTimeMs:1000}")
private int baseSleepTimeMs;
private CuratorFramework curatorClient;
@PostConstruct
public void init() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
curatorClient = CuratorFrameworkFactory.builder()
.connectString(connectString)
.retryPolicy(retryPolicy)
.build();
curatorClient.start();
}
@Override
public CuratorFramework getObject() throws Exception {
return curatorClient;
}
@Override
public Class<?> getObjectType() {
return CuratorFramework.class;
}
}
2.4 定義zookeeper監聽器
package com.dragon.study.springbootzookeeper.config;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Optional;
@Component
public class ZkListener {
@Resource
private CuratorFramework curatorClient;
@PostConstruct
public void init() throws Exception {
String path = "/zk";
//監聽當前節點變化,不監聽子節點變化
NodeCache nodeCache = new NodeCache(curatorClient, path);
nodeCache.start();
nodeCache.getListenable().addListener(() -> {
String nodePath = nodeCache.getCurrentData().getPath();
String data = new String(nodeCache.getCurrentData().getData());
System.out.println("nodeChanged,nodePath:" + nodePath + " data:" + data);
});
//監聽子節點變化,不監聽當前節點變化
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorClient, path, true);
pathChildrenCache.start();
pathChildrenCache.getListenable().addListener((curatorFramework, event) -> {
String type = event.getType().name();
System.out.println("pathChildrenCache, type:" + type);
Optional.ofNullable(event.getData()).ifPresent(t->{
String nodePath = t.getPath();
String data = new String(t.getData());
System.out.println("pathChildrenCache, nodePath:" + nodePath + " data:" + data + " type:" + type);
});
});
//監聽當前節點及其所有子節點變化
TreeCache treeCache = new TreeCache(curatorClient, path);
treeCache.start();
treeCache.getListenable().addListener((curatorFramework, event) -> {
String type = event.getType().name();
System.out.println("treeCache, type:" + type);
Optional.ofNullable(event.getData()).ifPresent(t->{
String nodePath = t.getPath();
String data = new String(t.getData());
System.out.println("treeCache, nodePath:" + nodePath + " data:" + data + " type:" + type);
});
});
}
}
2.5 啟動類
package com.dragon.study.springbootzookeeper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootZookeeperApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootZookeeperApplication.class, args);
}
}
2.6 測試
先執行啟動類SpringBootZookeeperApplication,開啟前面zookeeper監聽。接下來定義測試類:
package com.dragon.study.springbootzookeeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.transaction.CuratorTransaction;
import org.apache.zookeeper.CreateMode;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
class SpringBootZookeeperApplicationTests {
@Resource
private CuratorFramework curatorClient;
@Test
void zkTest() throws Exception {
//建立結點,輸出如下:
// treeCache, type:NODE_ADDED
// treeCache, nodePath:/zk data: type:NODE_ADDED
// nodeChanged,nodePath:/zk data:
curatorClient.create().creatingParentsIfNeeded().forPath("/zk");
//建立結點,同時設定值,輸出如下:
// treeCache, type:NODE_ADDED
// treeCache, nodePath:/zk data: type:NODE_ADDED
// nodeChanged,nodePath:/zk data:
curatorClient.create().creatingParentsIfNeeded().forPath("/zk", "this is zk1".getBytes());
//單獨設定結點值
curatorClient.setData().forPath("/zk", "this is zk6".getBytes());
//建立包含父結點的結點,輸出如下:
// treeCache, type:NODE_ADDED
// treeCache, nodePath:/zk/one data:this is one type:NODE_ADDED
curatorClient.create().creatingParentsIfNeeded().forPath("/zk/one", "this is one".getBytes());
//單獨設定結點值,輸出如下:
// treeCache, type:NODE_UPDATED
// treeCache, nodePath:/zk/one data:this is zk one2 type:NODE_UPDATED
curatorClient.setData().forPath("/zk/one", "this is zk one2".getBytes());
//事務,執行多個操作
CuratorTransaction curatorTransaction = curatorClient.inTransaction();
curatorTransaction.create().withMode(CreateMode.EPHEMERAL).forPath("/zk/three", "this is three".getBytes())
.and().create().withMode(CreateMode.PERSISTENT).forPath("/zk/four", "this is four".getBytes())
.and().commit();
}
}
相關文章
- spring boot中redis使用Spring BootRedis
- Spring boot + Zookeeper + Dubbo學習筆記Springboot筆記
- 使用Spring Boot、Spring State Machine Framework和Zookeeper構建分散式狀態機 - ÖzdinçÇelikelSpring BootMacFramework分散式
- Spring Boot中@Import三種使用方式!Spring BootImport
- springboot(十一):Spring boot中mongodb的使用Spring BootMongoDB
- Spring Boot中攔截器的使用Spring Boot
- Spring Boot 2.0(四):使用 Docker 部署 Spring BootSpring BootDocker
- Spring Boot Admin 使用Spring Boot
- Spring Boot 使用1Spring Boot
- Spring Boot2中Swagger3使用Spring BootSwagger
- Spring Boot(三):Spring Boot中的事件的使用 與Spring Boot啟動流程(Event 事件 和 Listeners監聽器)Spring Boot事件
- 使用Intellij中的Spring Initializr來快速構建Spring Boot工程IntelliJSpring Boot
- Spring Boot中如何使用Ostara監控應用?Spring Boot
- 在spring boot3中使用native imageSpring Boot
- 使用Spring Boot整合ConsulSpring Boot
- Spring Boot日誌使用Spring Boot
- 使用 Docker 部署 Spring BootDockerSpring Boot
- Spring Boot中配置 AuditListenerSpring Boot
- Spring Boot乾貨系列:(十二)Spring Boot使用單元測試Spring Boot
- Intellij IDEA 中 的spring boot 專案使用了spring-boot-devtools要做的兩個設定IntelliJIdeaSpring Bootdev
- spring-boot 中使用 FastDFSSpringbootAST
- Spring Boot整合swagger使用教程Spring BootSwagger
- 使用Spring Boot建立docker imageSpring BootDocker
- Spring Boot使用MyBatis Generator、SwaggerSpring BootMyBatisSwagger
- 使用Spring Boot和Elasticsearch教程Spring BootElasticsearch
- Spring Boot中使用token:jwtSpring BootJWT
- 在 Spring Boot 中使用 RedisSpring BootRedis
- 如何使用Spring Boot的ProfilesSpring Boot
- 在spring boot專案(maven)中引入其他 spring boot專案Spring BootMaven
- Spring Boot 2.X(七):Spring Cache 使用Spring Boot
- Spring Boot 中測試 CORSSpring BootCORS
- Spring Boot中快速操作MongodbSpring BootMongoDB
- Spring Boot:Spring Boot配置SwaggerSpring BootSwagger
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- 禁用 Spring Boot 中引入安全元件 spring-boot-starter-security 的方法Spring Boot元件
- Spring Boot入門(一):使用IDEA建立Spring Boot專案並使用yaml配置檔案Spring BootIdeaYAML
- Spring Boot系列十七 Spring Boot 整合 websocket,使用RabbitMQ做為訊息代理Spring BootWebMQ
- Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 開源軟體雲收藏Spring BootDocker