Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
holden_csdn
tx-lcn
提交
cc696fa6
T
tx-lcn
项目概览
holden_csdn
/
tx-lcn
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
tx-lcn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
cc696fa6
编写于
9月 13, 2020
作者:
W
whomhim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat( #506 ): 30秒重新设置 redis 中 TxManager 过期时间一次,相当于心跳一样告诉其他 TM “我还存活”
上级
a254c1b0
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
15 addition
and
10 deletion
+15
-10
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/TMApplication.java
...m/src/main/java/com/codingapi/txlcn/tm/TMApplication.java
+2
-0
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/id/SnowflakeInitiator.java
...in/java/com/codingapi/txlcn/tm/id/SnowflakeInitiator.java
+10
-4
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/id/SnowflakeSchedule.java
...ain/java/com/codingapi/txlcn/tm/id/SnowflakeSchedule.java
+3
-2
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/node/TmNode.java
...-tm/src/main/java/com/codingapi/txlcn/tm/node/TmNode.java
+0
-3
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/runner/TmNodeServerRunner.java
...ava/com/codingapi/txlcn/tm/runner/TmNodeServerRunner.java
+0
-1
未找到文件。
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/TMApplication.java
浏览文件 @
cc696fa6
...
...
@@ -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
)
{
...
...
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/id/SnowflakeInitiator.java
浏览文件 @
cc696fa6
...
...
@@ -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
);
}
...
...
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/id/SnowflakeSchedule.java
浏览文件 @
cc696fa6
...
...
@@ -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
();
}
...
...
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/node/TmNode.java
浏览文件 @
cc696fa6
...
...
@@ -85,9 +85,6 @@ public class TmNode {
protocolServer
.
connectTo
(
iNetSocketAddress
.
getHostString
(),
iNetSocketAddress
.
getPort
()));
}
public
void
sentToOtherNode
(){
}
}
txlcn-tm/src/main/java/com/codingapi/txlcn/tm/runner/TmNodeServerRunner.java
浏览文件 @
cc696fa6
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录