18 changed files with 991 additions and 10 deletions
@ -0,0 +1,29 @@
|
||||
package net.sopod.soim.common.util; |
||||
|
||||
import com.google.common.base.Preconditions; |
||||
|
||||
/** |
||||
* ObjectUtil |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 16:54 |
||||
*/ |
||||
public class ObjectUtil { |
||||
|
||||
public static <T> T defaultValue(T value, T...defaultValues) { |
||||
if (value != null) { |
||||
return value; |
||||
} |
||||
Preconditions.checkArgument(defaultValues != null && defaultValues.length > 0, "默认值不能为空"); |
||||
T result = null; |
||||
for (T defaultValue : defaultValues) { |
||||
if (defaultValue != null) { |
||||
result = defaultValue; |
||||
break; |
||||
} |
||||
} |
||||
Preconditions.checkNotNull(result, "默认值至少一个不为空"); |
||||
return result; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,16 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<artifactId>so-im</artifactId> |
||||
<groupId>net.sopod</groupId> |
||||
<version>1.0.0</version> |
||||
<relativePath>../../pom.xml</relativePath> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<artifactId>im-segment-id-api</artifactId> |
||||
|
||||
|
||||
</project> |
||||
@ -0,0 +1,13 @@
|
||||
package net.sopod.soim.logic.segmentid.model; |
||||
|
||||
/** |
||||
* Segment |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 11:58 |
||||
*/ |
||||
public class Segment { |
||||
|
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,78 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<parent> |
||||
<artifactId>im-logic</artifactId> |
||||
<groupId>net.sopod</groupId> |
||||
<version>1.0.0</version> |
||||
</parent> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<artifactId>im-segment-id</artifactId> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>net.sopod</groupId> |
||||
<artifactId>im-segment-id-api</artifactId> |
||||
<version>${soim.version}</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>net.sopod</groupId> |
||||
<artifactId>im-common</artifactId> |
||||
<version>${soim.version}</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
<exclusions><!-- 去掉springboot默认logback日志配置 --> |
||||
<exclusion> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-logging</artifactId> |
||||
</exclusion> |
||||
</exclusions> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-log4j2</artifactId> |
||||
<exclusions> |
||||
<exclusion> |
||||
<groupId>com.fasterxml.jackson.core</groupId> |
||||
<artifactId>jackson-databind</artifactId> |
||||
</exclusion> |
||||
<exclusion> |
||||
<groupId>com.fasterxml.jackson.datatype</groupId> |
||||
<artifactId>jackson-datatype-jsr310</artifactId> |
||||
</exclusion> |
||||
</exclusions> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-data-redis</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.dubbo</groupId> |
||||
<artifactId>dubbo-spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.apache.dubbo</groupId> |
||||
<artifactId>dubbo-registry-nacos</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>mysql</groupId> |
||||
<artifactId>mysql-connector-java</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>com.zaxxer</groupId> |
||||
<artifactId>HikariCP</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>com.baomidou</groupId> |
||||
<artifactId>mybatis-plus-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-orm</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
</project> |
||||
@ -0,0 +1,21 @@
|
||||
package net.sopod.soim.logic.segmentid; |
||||
|
||||
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; |
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
||||
/** |
||||
* SegmentIdApplication |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 14:55 |
||||
*/ |
||||
@EnableDubbo(scanBasePackages = {"net.sopod.soim.logic.segmentid.service"}) |
||||
@SpringBootApplication |
||||
public class SegmentIdApplication { |
||||
|
||||
public static void main(String[] args) { |
||||
SpringApplication.run(SegmentIdApplication.class); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,28 @@
|
||||
package net.sopod.soim.logic.segmentid.config; |
||||
|
||||
import lombok.Data; |
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* SegmentConfigration |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 17:02 |
||||
*/ |
||||
@EnableConfigurationProperties |
||||
@Configuration() |
||||
@Component |
||||
@Data |
||||
public class SegmentConfig { |
||||
|
||||
public static final String KEY_PREFIX_SEGMENT_ID_INSERT = "SEGMENT_ID_INSERT_"; |
||||
|
||||
private long initId = 10000L; |
||||
|
||||
private long initStep = 1000L; |
||||
|
||||
private int dbSegmentVersionRetryTimes = 3; |
||||
|
||||
} |
||||
@ -0,0 +1,14 @@
|
||||
package net.sopod.soim.logic.segmentid.mapper; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import net.sopod.soim.logic.segmentid.model.entity.SegmentId; |
||||
|
||||
/** |
||||
* SegmentIdMapper |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 15:00 |
||||
*/ |
||||
public interface SegmentIdMapper extends BaseMapper<SegmentId> { |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
|
||||
package net.sopod.soim.logic.segmentid.model.dto; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* NextSegmentParam |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 16:51 |
||||
*/ |
||||
@Data |
||||
public class NextSegmentParam { |
||||
|
||||
/** 业务标签 */ |
||||
private String bizTag; |
||||
|
||||
/** 步长,为空则使用数据库默认 */ |
||||
private Long step; |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
|
||||
package net.sopod.soim.logic.segmentid.model.dto; |
||||
|
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
/** |
||||
* SegmentDTO |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 17:27 |
||||
*/ |
||||
@Data |
||||
@Accessors(chain = true) |
||||
public class SegmentDTO { |
||||
|
||||
private long beginId; |
||||
|
||||
private long endId; |
||||
|
||||
} |
||||
@ -0,0 +1,47 @@
|
||||
package net.sopod.soim.logic.segmentid.model.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableField; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* SegmentId |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 15:01 |
||||
*/ |
||||
@Data |
||||
@Accessors(chain = true) |
||||
@TableName("im_segment_id") |
||||
public class SegmentId { |
||||
|
||||
/** 业务标签 */ |
||||
@TableId(value = "biz_tag", type = IdType.INPUT) |
||||
private String bizTag; |
||||
|
||||
/** 当前id值 */ |
||||
@TableField(value = "current_id") |
||||
private Long currentId; |
||||
|
||||
/** 业务标签初始步长 */ |
||||
@TableField(value = "init_step") |
||||
private Long initStep; |
||||
|
||||
/** 创建时间 */ |
||||
@TableField(value = "create_time") |
||||
private Date createTime; |
||||
|
||||
/** 更新时间 */ |
||||
@TableField(value = "update_time") |
||||
private Date updateTime; |
||||
|
||||
/** 版本号 */ |
||||
@TableField(value = "version") |
||||
private Long version; |
||||
|
||||
} |
||||
@ -0,0 +1,139 @@
|
||||
package net.sopod.soim.logic.segmentid.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.google.common.base.Preconditions; |
||||
import lombok.AllArgsConstructor; |
||||
import net.sopod.soim.common.util.ImClock; |
||||
import net.sopod.soim.common.util.ObjectUtil; |
||||
import net.sopod.soim.logic.segmentid.config.SegmentConfig; |
||||
import net.sopod.soim.logic.segmentid.mapper.SegmentIdMapper; |
||||
import net.sopod.soim.logic.segmentid.model.dto.NextSegmentParam; |
||||
import net.sopod.soim.logic.segmentid.model.dto.SegmentDTO; |
||||
import net.sopod.soim.logic.segmentid.model.entity.SegmentId; |
||||
import net.sopod.soim.logic.segmentid.util.RedisLockUtil; |
||||
import org.apache.dubbo.config.annotation.DubboService; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
|
||||
import java.util.concurrent.ThreadLocalRandom; |
||||
|
||||
/** |
||||
* SegmentIdService |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-02 15:22 |
||||
*/ |
||||
@DubboService |
||||
@AllArgsConstructor |
||||
public class SegmentIdService { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SegmentIdService.class); |
||||
|
||||
private final SegmentIdMapper segmentIdMapper; |
||||
|
||||
private final RedisTemplate<String, String> redisTemplate; |
||||
|
||||
private final SegmentConfig segmentConfig; |
||||
|
||||
public SegmentDTO nextSegmentId(NextSegmentParam param) { |
||||
String bizTag = param.getBizTag(); |
||||
SegmentId segmentId = segmentIdMapper.selectById(bizTag); |
||||
if (segmentId == null) { |
||||
String key = SegmentConfig.KEY_PREFIX_SEGMENT_ID_INSERT + bizTag; |
||||
String val = String.valueOf(ThreadLocalRandom.current().nextInt()); |
||||
// 锁二十秒,创建数据
|
||||
boolean locked = RedisLockUtil.acquireLock(redisTemplate, key, val, 20000L); |
||||
if (locked) { |
||||
try { |
||||
// 插入分段id数据
|
||||
SegmentId newSegmentId = new SegmentId() |
||||
.setBizTag(bizTag) |
||||
.setCreateTime(ImClock.date()) |
||||
.setCurrentId(segmentConfig.getInitId()) |
||||
.setVersion(0L) |
||||
.setInitStep(segmentConfig.getInitStep()); |
||||
segmentIdMapper.insert(newSegmentId); |
||||
segmentId = newSegmentId; |
||||
} finally { |
||||
// 释放锁
|
||||
RedisLockUtil.releaseLock(redisTemplate, key, val); |
||||
} |
||||
} else { |
||||
// 等待锁释放
|
||||
long timeout = 2000L; |
||||
boolean released = waitReleaseRedisLock(key, timeout); |
||||
if (!released) { |
||||
logger.warn("{} 插入数据超过 {} 未释放", bizTag, timeout); |
||||
} |
||||
segmentId = segmentIdMapper.selectById(bizTag); |
||||
} |
||||
if (segmentId == null) { |
||||
throw new RuntimeException(String.format("%s分段数据不存在,新增失败", bizTag)); |
||||
} |
||||
} |
||||
int i = 0; |
||||
do { |
||||
SegmentDTO segment = getSegment(segmentId, param.getStep()); |
||||
if (segment != null) { |
||||
return segment; |
||||
} |
||||
i++; |
||||
} while (i < segmentConfig.getDbSegmentVersionRetryTimes()); |
||||
|
||||
throw new RuntimeException("id分段获取失败"); |
||||
} |
||||
|
||||
private boolean waitReleaseRedisLock(String key, long timeout) { |
||||
long begin = ImClock.millis(); |
||||
// while 阻塞等待
|
||||
while (true) { |
||||
long current = ImClock.millis(); |
||||
if (!RedisLockUtil.hasKey(redisTemplate, key)) { |
||||
return true; |
||||
} |
||||
if (current - begin > timeout) { |
||||
return false; |
||||
} |
||||
try { |
||||
Thread.sleep(100); |
||||
} catch (InterruptedException e) { |
||||
logger.error("wait release redis lock interrupted!", e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取id段 |
||||
* @param dbSegmentId 当前DB segmentId |
||||
* @param step 步长 |
||||
*/ |
||||
private SegmentDTO getSegment(SegmentId dbSegmentId, Long step) { |
||||
step = ObjectUtil.defaultValue(step, dbSegmentId.getInitStep(), null); |
||||
Preconditions.checkState(step > 0, "步长需大于0"); |
||||
// 开始id, 结束id
|
||||
Long currentId = dbSegmentId.getCurrentId(); |
||||
long endId = dbSegmentId.getCurrentId() + step; |
||||
// 版本号更新条件
|
||||
LambdaQueryWrapper<SegmentId> segmentIdUpdate = new QueryWrapper<SegmentId>().lambda() |
||||
.eq(SegmentId::getBizTag, dbSegmentId.getBizTag()) |
||||
.eq(SegmentId::getVersion, dbSegmentId.getVersion()); |
||||
SegmentId newSegment = new SegmentId() |
||||
.setBizTag(dbSegmentId.getBizTag()) |
||||
.setUpdateTime(ImClock.date()) |
||||
.setCurrentId(endId + 1) |
||||
.setVersion(dbSegmentId.getVersion() + 1); |
||||
// 根据版本号条件尝试更新
|
||||
int row = segmentIdMapper.update(newSegment, segmentIdUpdate); |
||||
if (row == 0) { |
||||
// 未更新成功,被其他连接并发更新
|
||||
return null; |
||||
} |
||||
// 更新成功
|
||||
return new SegmentDTO() |
||||
.setBeginId(currentId) |
||||
.setEndId(endId); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,120 @@
|
||||
package net.sopod.soim.logic.segmentid.util; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.data.redis.core.script.DefaultRedisScript; |
||||
import org.springframework.data.redis.core.script.RedisScript; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* Redis分布式锁工具 |
||||
* |
||||
* @author tangmingyou |
||||
* @date 2022-03-18 14:21 |
||||
*/ |
||||
public class RedisLockUtil { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RedisLockUtil.class); |
||||
|
||||
private static final RedisScript<String> SCRIPT_LOCK = new DefaultRedisScript<>("return redis.call('set',KEYS[1],ARGV[1],'NX','PX',ARGV[2])", String.class); |
||||
private static final RedisScript<String> SCRIPT_UNLOCK = new DefaultRedisScript<>("if redis.call('get',KEYS[1]) == ARGV[1] then return tostring(redis.call('del', KEYS[1])==1) else return 'false' end", String.class); |
||||
private static final String LOCK_SUCCESS = "OK"; |
||||
|
||||
/** |
||||
* 加锁,往 redis 中设置值并设置过期时间 |
||||
* @param lockKey 键 |
||||
* @param lockValue 值 |
||||
* @param acquireExpire 超期时间(毫秒) |
||||
* @return 是否获取成功 |
||||
*/ |
||||
public static boolean acquireLock(RedisTemplate<String, String> redisTemplate, |
||||
String lockKey, String lockValue, long acquireExpire) { |
||||
Object lockResult = redisTemplate.execute(SCRIPT_LOCK, |
||||
redisTemplate.getStringSerializer(), |
||||
redisTemplate.getStringSerializer(), |
||||
Collections.singletonList(lockKey), |
||||
lockValue, String.valueOf(acquireExpire)); |
||||
// 加锁失败 lockResult 为 null
|
||||
return LOCK_SUCCESS.equals(lockResult); |
||||
} |
||||
|
||||
/** |
||||
* <pre> |
||||
* 为何解锁需要校验lockValue |
||||
* 客户端A加锁,一段时间之后客户端A解锁,在执行releaseLock之前,锁突然过期了。 |
||||
* 此时客户端B尝试加锁成功,然后客户端A再执行releaseLock方法,则将客户端B的锁给解除了。 |
||||
* </pre> |
||||
* @param lockKey redis 键 |
||||
* @param lockValue lock 的值 |
||||
* @return 是否成功 |
||||
*/ |
||||
public static boolean releaseLock(RedisTemplate<String, String> redisTemplate, |
||||
String lockKey, String lockValue) { |
||||
Object releaseResult = redisTemplate.execute(SCRIPT_UNLOCK, |
||||
redisTemplate.getStringSerializer(), |
||||
redisTemplate.getStringSerializer(), |
||||
Collections.singletonList(lockKey), |
||||
lockValue); |
||||
return Boolean.parseBoolean(releaseResult.toString()); |
||||
} |
||||
|
||||
/** |
||||
* 指定缓存失效时间 |
||||
* @param key 键 |
||||
* @param time 时间(秒) |
||||
* @return 是否成功 |
||||
*/ |
||||
public static boolean expire(RedisTemplate<String, String> redisTemplate, |
||||
String key, long time) { |
||||
try { |
||||
if (time > 0) { |
||||
redisTemplate.expire(key, time, TimeUnit.SECONDS); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
throw new RuntimeException("指定缓存失效时间异常"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 根据key 获取过期时间 |
||||
* @param key 键 不能为null |
||||
* @return 时间(秒) 返回0代表为永久有效 |
||||
*/ |
||||
public static long getExpire(RedisTemplate<String, String> redisTemplate, String key) { |
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS); |
||||
} |
||||
|
||||
/** |
||||
* 判断key是否存在 |
||||
* @param key 键 |
||||
* @return true 存在 false不存在 |
||||
*/ |
||||
public static boolean hasKey(RedisTemplate<String, String> redisTemplate, String key) { |
||||
try { |
||||
return redisTemplate.hasKey(key); |
||||
} catch (Exception e) { |
||||
logger.error(e.getMessage(), e); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 删除缓存 |
||||
* @param keys 可以传一个值 或多个 |
||||
*/ |
||||
public static void del(RedisTemplate<String, String> redisTemplate, String... keys) { |
||||
if (keys != null && keys.length > 0) { |
||||
if (keys.length == 1) { |
||||
redisTemplate.delete(keys[0]); |
||||
} else { |
||||
redisTemplate.delete(Arrays.asList(keys)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,22 @@
|
||||
spring: |
||||
application: |
||||
name: segment-id |
||||
datasource: |
||||
type: com.zaxxer.hikari.HikariDataSource |
||||
url: jdbc:mysql://cd-cdb-mrz9fw80.sql.tencentcdb.com:61843/soim_db?serverTimezone=GMT%2B8 |
||||
username: root |
||||
password: sopod@2347# |
||||
redis: |
||||
host: 124.222.131.236:3379 |
||||
password: sopod@redis# |
||||
|
||||
dubbo: |
||||
application: |
||||
name: ${spring.application.name} |
||||
registry: |
||||
address: nacos://124.222.131.236:3848 |
||||
protocol: |
||||
port: 3001 |
||||
|
||||
mybatis-plus: |
||||
mapper-locations: classpath:mapper/*.xml |
||||
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > |
||||
<mapper namespace="net.sopod.soim.logic.segmentid.mapper.SegmentIdMapper"> |
||||
|
||||
</mapper> |
||||
Loading…
Reference in new issue