fix:使用springboot来封装

上级 d4e41daf
...@@ -11,6 +11,17 @@ ...@@ -11,6 +11,17 @@
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
...@@ -21,18 +32,6 @@ ...@@ -21,18 +32,6 @@
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>1.2.83</version> <version>1.2.83</version>
</dependency> </dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!--curator-->
<dependency> <dependency>
<groupId>org.apache.curator</groupId> <groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId> <artifactId>curator-framework</artifactId>
...@@ -43,17 +42,6 @@ ...@@ -43,17 +42,6 @@
<artifactId>curator-recipes</artifactId> <artifactId>curator-recipes</artifactId>
<version>4.0.0</version> <version>4.0.0</version>
</dependency> </dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
package com.kwan.shuyu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZookeeperApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperApplication.class, args);
}
}
package com.kwan.shuyu.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.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* zookeeper的配置类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 12:37
*/
@Configuration
public class ZookeeperConfig {
@Value("${kwan.zookeeper.hostlist}")
private String hostlist;
@Bean(destroyMethod = "close")
public CuratorFramework connect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(hostlist)
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
return client;
}
}
\ No newline at end of file
package com.kwan.shuyu; package com.kwan.shuyu.entry;
import org.apache.curator.RetryPolicy; import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
......
server:
port: 8888
spring:
application:
name: zookeeper-service
kwan:
zookeeper:
hostlist: 120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183 #多个节点用逗号分隔
\ No newline at end of file
log4j.rootLogger=off,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%d{yyyy-MM-dd HH/:mm/:ss}]%-5p %c(line/:%L) %x-%m%n
\ No newline at end of file
package com.kwan.shuyu; package com.kwan.shuyu;
import com.kwan.shuyu.entry.Ticket12306;
/** /**
* InterProcessMutex 分布式锁测试 * InterProcessMutex 分布式锁测试
* *
......
...@@ -39,7 +39,6 @@ public class CuratorTest_01_Create { ...@@ -39,7 +39,6 @@ public class CuratorTest_01_Create {
//重试策略 //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式 //2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder() client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000) .sessionTimeoutMs(60 * 1000)
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.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;
/** /**
* 创建节点 不带数据 * 创建节点 不带数据
...@@ -15,49 +14,13 @@ import org.junit.Test; ...@@ -15,49 +14,13 @@ import org.junit.Test;
* @version : 2.2.0 * @version : 2.2.0
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
@SpringBootTest
@RunWith(SpringRunner.class)
public class CuratorTest_02_Create { public class CuratorTest_02_Create {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 创建节点:create 持久 临时 顺序 数据 * 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("") * 1. 基本创建 :create().forPath("")
...@@ -69,7 +32,7 @@ public class CuratorTest_02_Create { ...@@ -69,7 +32,7 @@ public class CuratorTest_02_Create {
public void testCreate2() throws Exception { public void testCreate2() throws Exception {
//基本创建 //基本创建
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
String path = client.create().forPath("/app1"); String path = client.create().forPath("/app5");
System.out.println(path); System.out.println(path);
} }
} }
\ No newline at end of file
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 设置节点的类型 * 设置节点的类型
...@@ -18,47 +14,9 @@ import org.junit.Test; ...@@ -18,47 +14,9 @@ import org.junit.Test;
*/ */
public class CuratorTest_03_Create { public class CuratorTest_03_Create {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 创建节点:create 持久 临时 顺序 数据 * 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("") * 1. 基本创建 :create().forPath("")
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 创建多级节点 * 创建多级节点
...@@ -17,49 +13,9 @@ import org.junit.Test; ...@@ -17,49 +13,9 @@ import org.junit.Test;
*/ */
public class CuratorTest_04_Create { public class CuratorTest_04_Create {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/**
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);
*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 创建节点:create 持久 临时 顺序 数据 * 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("") * 1. 基本创建 :create().forPath("")
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 查询数据 * 查询数据
...@@ -17,47 +13,9 @@ import org.junit.Test; ...@@ -17,47 +13,9 @@ import org.junit.Test;
*/ */
public class CuratorTest_05_Get { public class CuratorTest_05_Get {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 查询节点: * 查询节点:
* 1. 查询数据:get: getData().forPath() * 1. 查询数据:get: getData().forPath()
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List; import java.util.List;
...@@ -18,46 +14,9 @@ import java.util.List; ...@@ -18,46 +14,9 @@ import java.util.List;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_06_Get { public class CuratorTest_06_Get {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 查询节点: * 查询节点:
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 查询节点状态信息 * 查询节点状态信息
...@@ -17,46 +13,9 @@ import org.junit.Test; ...@@ -17,46 +13,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_07_Get { public class CuratorTest_07_Get {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 查询节点: * 查询节点:
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 基本修改数据 * 基本修改数据
...@@ -16,46 +12,9 @@ import org.junit.Test; ...@@ -16,46 +12,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_08_Set { public class CuratorTest_08_Set {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 修改数据 * 修改数据
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 根据版本修改 * 根据版本修改
...@@ -17,46 +13,10 @@ import org.junit.Test; ...@@ -17,46 +13,10 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_09_Set { public class CuratorTest_09_Set {
private CuratorFramework client;
/** @Autowired
* 建立连接 private CuratorFramework client;
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 修改数据 * 修改数据
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 删除单个节点 * 删除单个节点
...@@ -16,46 +12,9 @@ import org.junit.Test; ...@@ -16,46 +12,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_10_Delete { public class CuratorTest_10_Delete {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 删除节点: delete deleteall * 删除节点: delete deleteall
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 删除带有子节点的节点 * 删除带有子节点的节点
...@@ -16,46 +12,9 @@ import org.junit.Test; ...@@ -16,46 +12,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_11_Delete { public class CuratorTest_11_Delete {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 删除节点: delete deleteall * 删除节点: delete deleteall
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 必须成功的删除:为了防止网络抖动。本质就是重试。 * 必须成功的删除:为了防止网络抖动。本质就是重试。
...@@ -16,46 +12,9 @@ import org.junit.Test; ...@@ -16,46 +12,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_12_Delete { public class CuratorTest_12_Delete {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 删除节点: delete deleteall * 删除节点: delete deleteall
......
package com.kwan.shuyu.crud; package com.kwan.shuyu.crud;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback; import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent; import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* 删除回调:inBackground * 删除回调:inBackground
...@@ -18,46 +14,9 @@ import org.junit.Test; ...@@ -18,46 +14,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54 * @date : 2023/5/15 09:54
*/ */
public class CuratorTest_13_Delete { public class CuratorTest_13_Delete {
private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After @Autowired
public void close() { private CuratorFramework client;
if (client != null) {
client.close();
}
}
/** /**
* 删除节点: delete deleteall * 删除节点: delete deleteall
......
package com.kwan.shuyu.watch; package com.kwan.shuyu.watch;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.*; import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* Watcher监听 * Watcher监听
...@@ -18,47 +15,9 @@ import org.junit.Test; ...@@ -18,47 +15,9 @@ import org.junit.Test;
*/ */
public class Watcher_01_NodeCache { public class Watcher_01_NodeCache {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 演示 NodeCache:给指定一个节点注册监听器 * 演示 NodeCache:给指定一个节点注册监听器
*/ */
......
package com.kwan.shuyu.watch; package com.kwan.shuyu.watch;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.*; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* Watcher监听 * Watcher监听
...@@ -18,47 +16,9 @@ import org.junit.Test; ...@@ -18,47 +16,9 @@ import org.junit.Test;
*/ */
public class Watcher_02_PathChildrenCache { public class Watcher_02_PathChildrenCache {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 演示 PathChildrenCache:监听某个节点的所有子节点们 * 演示 PathChildrenCache:监听某个节点的所有子节点们
*/ */
......
package com.kwan.shuyu.watch; package com.kwan.shuyu.watch;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.*; import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** /**
* Watcher监听 * Watcher监听
...@@ -18,47 +16,9 @@ import org.junit.Test; ...@@ -18,47 +16,9 @@ import org.junit.Test;
*/ */
public class Watcher_03_TreeCache { public class Watcher_03_TreeCache {
@Autowired
private CuratorFramework client; private CuratorFramework client;
/**
* 建立连接
*/
@Before
public void testConnect() {
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client = CuratorFrameworkFactory.builder()
.connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
//开启连接
client.start();
}
@After
public void close() {
if (client != null) {
client.close();
}
}
/** /**
* 演示 TreeCache:监听某个节点自己和所有子节点们 * 演示 TreeCache:监听某个节点自己和所有子节点们
*/ */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册