本文为 Idea+maven+spring-cloud项目搭建系列,maven项目的创建可以参考:
https://blog.csdn.net/l123lgx/article/details/121467823
本文使用了nacos 作为微服务的注册与发现,nacos 阿里云服务器的安装可以参考:https://blog.csdn.net/l123lgx/article/details/121421431
nacos 服务端的配置和使用可以参考:
https://blog.csdn.net/l123lgx/article/details/121491529
本文使用默认已经在服务端安装了zookeeper服务,阿里云轻量服务器–Docker–Zookeeper参考:
https://blog.csdn.net/l123lgx/article/details/122047659
1 pom 文件引入依赖jar包:
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.7.0version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>5.3.0version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>5.3.0version>
<exclusions>
<exclusion>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
exclusion>
exclusions>
dependency>
2 zookeeper 配置文件:
WrapperZK:
@Data
@Component
@ConfigurationProperties(prefix = "curator")
public class WrapperZK {
private int retryCount;
private int elapsedTimeMs;
private String connectString;
private int sessionTimeoutMs;
private int connectionTimeoutMs;
}
CuratorConfig:
@Configuration
public class CuratorConfig {
@Autowired
WrapperZK wrapperZk;
/**
* 这里的start就是创建完对象放到容器后,需要调用他的start方法
*
* @return
*/
@Bean(initMethod = "start", destroyMethod = "close")
public CuratorFramework curatorFramework() {
return CuratorFrameworkFactory.newClient(
wrapperZk.getConnectString(),
wrapperZk.getSessionTimeoutMs(),
wrapperZk.getConnectionTimeoutMs(),
new RetryNTimes(wrapperZk.getRetryCount(), wrapperZk.getElapsedTimeMs()));
}
}
3 测试类:
@SpringBootTest
public class ZookeeperTest {
@Autowired
CuratorFramework curatorFramework;
/**
* 创建节点
*
* @throws Exception
*/
@Test
void createNode() throws Exception {
// 添加持久节点
String path = curatorFramework.create().forPath("/curator-node");
System.out.println(String.format("curator create node :%s successfully.", path));
// 添加临时序号节点,并赋值数据
String path1 = curatorFramework.create()
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath("/curator-node", "some-data".getBytes());
System.out.println(String.format("curator create node :%s successfully.", path1));
// System.in.read()目的是阻塞客户端关闭,我们可以在这期间查看zk的临时序号节点
// 当程序结束时候也就是客户端关闭的时候,临时序号节点会消失
System.in.read();
}
/**
* 获取节点
*
* @throws Exception
*/
@Test
public void testGetData() throws Exception {
// 在上面的方法执行后,创建了curator-node节点,但是我们并没有显示的去赋值
// 通过这个方法去获取节点的值会发现,当我们通过Java客户端创建节点不赋值的话默认就是存储的创建节点的ip
byte[] bytes = curatorFramework.getData().forPath("/curator-node");
System.out.println("节点数据:");
System.out.println(new String(bytes));
}
/**
* 修改节点数据
*
* @throws Exception
*/
@Test
public void testSetData() throws Exception {
curatorFramework.setData().forPath("/curator-node", "changed!".getBytes());
byte[] bytes = curatorFramework.getData().forPath("/curator-node");
System.out.println("修改节点数据:");
System.out.println(new String(bytes));
}
/**
* 创建节点同时创建⽗节点
*
* @throws Exception
*/
@Test
public void testCreateWithParent() throws Exception {
String pathWithParent = "/node-parent/sub-node-1";
String path = curatorFramework.create().creatingParentsIfNeeded().forPath(pathWithParent);
System.out.println(String.format("curator create node :%s successfully.", path));
}
/**
* 删除节点(包含子节点)
*
* @throws Exception
*/
@Test
public void testDelete() throws Exception {
String pathWithParent = "/curator-node";
curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(pathWithParent);
}
}
4 创建节点查询节点信息:
4.1 进入zookeeper 容器 :
docker exec -it 容器id /bin/bash
4.2 进入bin 目录启动客户端:
cd bin/
zkCli.sh
4.3 查看节点:
ls /
可以看到测试类中创建的持久和临时节点: