提交 cc696fa6 编写于 作者: W whomhim

feat( #506 ): 30秒重新设置 redis 中 TxManager 过期时间一次,相当于心跳一样告诉其他 TM “我还存活”

上级 a254c1b0
......@@ -17,6 +17,7 @@ package com.codingapi.txlcn.tm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* Description: Company: CodingApi Date: 2018/11/29
......@@ -24,6 +25,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author lorne
*/
@SpringBootApplication
@EnableScheduling
public class TMApplication {
public static void main(String[] args) {
......
......@@ -38,7 +38,7 @@ public class SnowflakeInitiator {
private static String snowflakeRedisKey;
private static long LockExpire = 60 * 60 * 24;
private static long LockExpire = 30;
private static boolean stopTrying = false;
......@@ -73,11 +73,13 @@ public class SnowflakeInitiator {
*/
public boolean tryInit() {
snowflakeVo = nextKey(snowflakeVo);
snowflakeRedisKey = String.format("%s_%d_%d", TX_MANAGER, snowflakeVo.getDataCenterId(), snowflakeVo.getWorkerId());
snowflakeRedisKey = String.format("%s_%d_%d", TX_MANAGER, snowflakeVo.getDataCenterId(),
snowflakeVo.getWorkerId());
String hostAddress = NetUtil.getLocalhost().getHostAddress();
String hostAndPort = String.format("%s:%s", hostAddress, port);
Boolean isNotHasKey = !redisTemplate.hasKey(snowflakeRedisKey);
Boolean isSetKey = redisTemplate.opsForValue().setIfAbsent(snowflakeRedisKey, hostAndPort, LockExpire, TimeUnit.SECONDS);
Boolean isSetKey = redisTemplate.opsForValue().setIfAbsent(snowflakeRedisKey, hostAndPort,
LockExpire + randomDigits(), TimeUnit.SECONDS);
if (isNotHasKey && isSetKey) {
log.info("snowflake setIfAbsent key:{}", JSON.toJSONString(snowflakeVo));
return true;
......@@ -115,11 +117,15 @@ public class SnowflakeInitiator {
return snowflakeVo;
}
private int randomDigits() {
return (int) (Math.random() * 10);
}
/**
* 重新设置过期时间,由定时任务调用
*/
public void resetExpire() {
redisTemplate.expire(snowflakeRedisKey, (LockExpire - 600), TimeUnit.SECONDS);
redisTemplate.expire(snowflakeRedisKey, (LockExpire + randomDigits()), TimeUnit.SECONDS);
log.info("reset the snowflakeRedisKey's resetExpire time,redisKey :{}", snowflakeRedisKey);
}
......
......@@ -6,15 +6,16 @@ import org.springframework.stereotype.Component;
/**
* @author WhomHim
* @description 24小时重新设置过期时间一次
* @description 30秒重新设置过期时间一次,相当于心跳一样告诉其他 TM “我还存活”
* @date Create in 2020-8-17 22:34:28
*/
@Component
public class SnowflakeSchedule {
@Autowired
private SnowflakeInitiator snowflakeInitiator;
@Scheduled(fixedDelay = 1000 * 60 * 60 * 23)
@Scheduled(cron = "0/30 * * * * ?")
private void snowflakeInitiatorResetExpire() {
snowflakeInitiator.resetExpire();
}
......
......@@ -85,9 +85,6 @@ public class TmNode {
protocolServer.connectTo(iNetSocketAddress.getHostString(), iNetSocketAddress.getPort()));
}
public void sentToOtherNode(){
}
}
......@@ -33,7 +33,6 @@ public class TmNodeServerRunner {
@Autowired
private RedisTmNodeRepository redisTmNodeRepository;
public TmNodeServerRunner(ProtocolServer protocolServer) {
this.protocolServer = protocolServer;
this.scheduledExecutorService = new ScheduledThreadPoolExecutor(1,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册