场景
yitter-idgenerator 是基于雪花算法进行改造的分布式ID自增算法,集成时需要为每个服务设置唯一的机器号,才能保证生成的Id不会重复
实现方案
基于服务启动时指定唯一机器号
在程序服务启动时通过分布式锁 Redisson(基于Redis实现),对每台机器通过IP 对应一个 唯一的机器号(自增)映射,并保存在Redis中。缓存一次后,下次启动直接读取缓存即可
基于注册中心指定唯一机器号
从注册中心读取服务,增加多一个机器号服务来统一分配
基于第一种实现方案
Maven依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.6</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.7.19</version>
</dependency>
<dependency>
<groupId>com.github.yitter</groupId>
<artifactId>yitter-idgenerator</artifactId>
<version>1.0.6</version>
</dependency>
关键部分代码
@Component
public class RedissonUtil {
@Autowired
private RedissonClient redissonClient;
public RLock lock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock();
return lock;
}
public RLock lock(String lockKey, long timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, TimeUnit.SECONDS);
return lock;
}
public RLock lock(String lockKey, TimeUnit unit ,long timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
return lock;
}
public boolean tryLock(String lockKey,long waitTime, TimeUnit unit) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, unit);
} catch (InterruptedException e) {
return false;
}
}
public boolean tryLock(String lockKey, long waitTime, long leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);
} catch (InterruptedException e) {
return false;
}
}
public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, unit);
} catch (InterruptedException e) {
return false;
}
}
public void unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.unlock();
}
public void unlockIgnore(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
if ( !lock.isLocked() ) {
return ;
}
lock.unlock();
}
public void unlock(RLock lock) {
lock.unlock();
}
}
启动配置代码如下
@Slf4j
@Component
@Order(0)
public class SystemInitConfig implements CommandLineRunner {
@Autowired
private RedissonUtil redissonUtil;
@Autowired
private RedisTemplate redisTemplate;
private static final String CACHE_ID_GENERATOR = "LOCK_ID_GENERATOR";
private static final String CACHE_WORKERID_MAXID= "CACHE_WORKERID_MAXID";
private static final String CACHE_ID_IP = "CACHE_ID_IP";
@Override
public void run(String... args) throws Exception {
//获取mac地址
String macAddress = NetUtil.getLocalhost().getHostAddress();
log.info("{} 配置分布式Id Work缓存========开始",macAddress);
boolean existWorkerId = redisTemplate.opsForHash().hasKey(CACHE_ID_IP, macAddress);
//若已缓存在缓存中,直接跳过不设置
if (existWorkerId) {
log.info("{} 已配置分布式Id Work...",macAddress);
return ;
}
try {
//分布式锁等待120秒,执行时长最大120秒
boolean locked = redissonUtil.tryLock(CACHE_ID_GENERATOR, 120, 120);
if (!locked) {
throw new RuntimeException(macAddress+"设置分布式Id机器号失败");
}
ValueOperations <String,Integer> stringOperation = redisTemplate.opsForValue();
boolean initWorkerId = stringOperation.setIfAbsent(CACHE_WORKERID_MAXID, 1);
if( !initWorkerId ) {
//若已存在key,对最大的机器号自增1
stringOperation.increment(CACHE_WORKERID_MAXID);
}
Integer workerId = stringOperation.get(CACHE_WORKERID_MAXID);
IdGeneratorOptions options = new IdGeneratorOptions( workerId.shortValue());
YitIdHelper.setIdGenerator(options);
//设置mac地址 - workerid 到hash结构
redisTemplate.opsForHash().put(CACHE_ID_IP,macAddress,workerId);
log.info("已配置分布式Id Work,{} - {}",macAddress,workerId);
} finally {
redissonUtil.unlock(CACHE_ID_GENERATOR);
log.info("{} 配置分布式Id Work缓存========结束",macAddress);
}
}
}
直接在代码使用即可
YitIdHelper.nextId()
到此这篇关于SpringBoot集成yitter-idgenerator(雪花漂移)分布式Id自增的实现的文章就介绍到这了,更多相关SpringBoot 分布式Id自增内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!