4. Spring Boot使用Apache Curator实现分布式计数器「第四章 ZooKeeper Curator应用场景实战」「架构之路ZooKeeper理论和实战」
相关历史文章(阅读本文前,您可能需要先看下之前的系列????)国内最全的SpringBoot系列之四享元模式:共享女友-第355篇什么是ZooKeeper-第347篇ZooKeeper安装-第348篇ZooKeeper数据结构和实操-第349篇ZooKeeper的watch机制-第350篇ZooKeeper的acl权限控制-第351篇ZooKeeper内存数据和持久化-第352篇ZooKeeper

相关历史文章(阅读本文前,您可能需要先看下之前的系列👇)
ZooKeeper Java客户端的基本使用 - 第356篇
ZooKeeper客户端Curator的进阶使用 - 第359篇
ZooKeeper客户端Curator实现Watch事件监听 - 第361篇
Spring Boot 使用 Curator 操作 ZooKeeper - 第363篇
Spring Boot使用Apache Curator实现服务的注册和发现 - 第364篇
Spring Boot使用Apache Curator实现分布式锁(可重入排它锁) - 第365篇
Spring Boot使用Apache Curator实现leader选举 - 第366篇
Spring Boot使用Apache Curator实现分布式计数器 - 367篇
ZooKeeper Session 基本原理 - 第369篇
ZooKeeper分桶策略实现高性能的会话管理 - 第371篇
ZooKeeper Leader选举原理也不过如此,看完这篇你不再懵逼了 - 第374篇
Zookeeper 集群节点为什么要部署成奇数呢?- 第376篇
分布式一致性算法Paxos,ZooKeeper的ZAB协议 - 第381篇
分布式计数器在curator中怎么玩呢,这一节我们来看看如何使用。
一、基本概念
1.1 什么是计数器
计数器就是在原有值的基础上再增加一个值得到新的值。
1.2 什么是分布式计数器
在分布式环境下的计数器,就是分布式计数器。
二、分布式计数器实战
2.1 环境说明
(1)基于前面章节《Spring Boot 使用 Curator 操作 ZooKeeper》进行往下编码。
(2)ZooKeeper版本:3.6.2
(3)Curator版本:5.1.0
(4)对于在普通的java项目中,代码是一样的,只要能获取到CuratorFramework就能搞定。
2.2 使用DistributedAtomicInteger
使用DistributedAtomicInteger实现分布式计数器:
package com.kfit.springbootcuratordemo.distributedatomic;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.retry.RetryForever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* 分布式计数器
*
* @author 悟纤「公众号SpringBoot」
* @date 2021-04-22
* @slogan 大道至简 悟在天成
*/
@Service
public class DistributedAtomicService {
@Autowired
private CuratorFramework curatorFramework;
public void atomic(){
//DistributedAtomicLong,DistributedAtomicInteger
//RetryNTimes(retryCount,sleepMsBetweenRetries):new RetryNTimes(3,100)
//new RetryForever(100)
DistributedAtomicInteger counter = new DistributedAtomicInteger(curatorFramework, "/atomic",new RetryForever(100));
try {
AtomicValue<Integer> value = counter.increment();
String str = "";
str += "===="+Thread.currentThread().getName()+"====";
str += "原值为:"+value.preValue();//就是上一次修改成功之后的值
str += ",更改后的值为:"+value.postValue();//如果成功就是最新的值,失败的话就是0
str += ",状态:"+value.succeeded();//成功为true,失败为false.
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.3 编写测试代码
在conroller使用多线程的方式进行调用:
@Autowired
private DistributedAtomicService distributedAtomicService;
@RequestMapping("/atomic")
public String atomic(){
for(int i=0;i<20;i++){
new Thread(){
@Override
public void run() {
distributedAtomicService.atomic();
}
}.start();
}
return "ok";
}
访问测试下:
http://127.0.0.1:8080/shop/atomic
控制台打印信息:

三、分布式计数器源码
我们跟进代码counter.increment(),可以找到这么一段代码:

(1)如果节点不存在的话,那么就会走创建节点的逻辑,那么这个通过ZK只能创建一次来判断当前请求是否创建成功,节点被创建的话,那么执行这个创建执行就会失败。
(2)如果节点存在的话,那么就会走修改节点的逻辑,这里通过指定版本version,也就是使用了CAS来保证并发修改问题。
我就是我,是颜色不一样的烟火。
我就是我,是与众不同的小苹果。
à悟空学院:悟空学院
学院中有Spring Boot相关的课程!!
SpringBoot视频:从零开始学Spring Boot Plus - 网易云课堂
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringSecurity5.0视频:权限管理spring security - 网易云课堂
ShardingJDBC分库分表:分库分表Sharding-JDBC实战 - 网易云课堂
分布式事务解决方案:分布式事务解决方案「手写代码」 - 网易云课堂
JVM内存模型调优实战:深入理解JVM内存模型/调优实战 - 网易云课堂
Spring入门到精通:Spring零基础从入门到精通 - 网易云课堂
大话设计模式之爱你:大话设计模式之爱你一万年 - 网易云课堂
更多推荐



所有评论(0)