Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-communication
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-communication
Commits
4cde17c3
Commit
4cde17c3
authored
Jul 30, 2026
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
6dabbff2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
4 deletions
+42
-4
yd-communication-api/src/main/java/com/yd/communication/api/websocket/CoWebSocketServer.java
+42
-4
No files found.
yd-communication-api/src/main/java/com/yd/communication/api/websocket/CoWebSocketServer.java
View file @
4cde17c3
...
@@ -161,32 +161,70 @@ public class CoWebSocketServer {
...
@@ -161,32 +161,70 @@ public class CoWebSocketServer {
*/
*/
@EventListener
(
ApplicationReadyEvent
.
class
)
@EventListener
(
ApplicationReadyEvent
.
class
)
public
void
startRedisSubscriber
()
{
public
void
startRedisSubscriber
()
{
// 启动后台线程订阅 Redis
log
.
info
(
"========================================"
);
log
.
info
(
"应用已完全启动,开始初始化 Redis 订阅..."
);
log
.
info
(
"当前节点ID: {}"
,
NODE_ID
);
log
.
info
(
"RedisTemplate 是否为空: {}"
,
redisTemplate
==
null
?
"是"
:
"否"
);
if
(
redisTemplate
!=
null
)
{
log
.
info
(
"RedisTemplate 类型: {}"
,
redisTemplate
.
getClass
().
getName
());
}
log
.
info
(
"========================================"
);
new
Thread
(()
->
{
new
Thread
(()
->
{
log
.
info
(
"应用已就绪,开始启动 Redis 跨节点订阅..."
);
doSubscribe
();
doSubscribe
();
}).
start
();
}).
start
();
}
}
/**
/**
* 执行 Redis 订阅(支持自动重连)
* 执行 Redis 订阅(支持自动重连)
* 增加详细日志,便于排查 Redis 连接问题
*/
*/
private
void
doSubscribe
()
{
private
void
doSubscribe
()
{
log
.
info
(
"=== Redis 订阅线程启动 ==="
);
log
.
info
(
"当前节点 ID: {}"
,
NODE_ID
);
while
(
true
)
{
while
(
true
)
{
try
{
try
{
log
.
info
(
"尝试连接 Redis,地址: {}:{}"
,
redisTemplate
.
getConnectionFactory
().
getConnection
().
getNativeConnection
()
// 这行可能报错,改用手动获取配置
);
// 改为从配置中读取 host/port(更好的方式,但这里简化为打印配置)
// 由于无法直接获取,可打印 RedisTemplate 的哈希码,确认不为 null
log
.
info
(
"RedisTemplate 实例: {}"
,
System
.
identityHashCode
(
redisTemplate
));
// 先发送 PING 测试连接
try
{
String
pingResult
=
redisTemplate
.
execute
((
connection
)
->
{
return
connection
.
ping
();
},
true
);
log
.
info
(
"Redis PING 响应: {}"
,
pingResult
);
}
catch
(
Exception
pingEx
)
{
log
.
error
(
"Redis PING 失败: {}"
,
pingEx
.
getMessage
());
// 继续尝试订阅,因为可能订阅能成功但 PING 失败(少见)
}
log
.
info
(
"开始订阅 Redis 频道: room:channel:*"
);
redisTemplate
.
execute
((
connection
)
->
{
redisTemplate
.
execute
((
connection
)
->
{
connection
.
subscribe
(
connection
.
subscribe
(
(
message
,
pattern
)
->
handleCrossNodeMessage
(
new
String
(
message
.
getBody
())),
(
message
,
pattern
)
->
{
log
.
debug
(
"收到跨节点消息,频道: {}, 消息长度: {}"
,
new
String
(
pattern
),
message
.
getBody
().
length
);
handleCrossNodeMessage
(
new
String
(
message
.
getBody
()));
},
"room:channel:*"
.
getBytes
()
"room:channel:*"
.
getBytes
()
);
);
log
.
warn
(
"Redis 订阅意外退出,将重新尝试..."
);
return
null
;
return
null
;
},
true
);
},
true
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"Redis 订阅断开,5秒后重试..."
,
e
);
log
.
error
(
"Redis 订阅异常,错误类型: {}, 消息: {}"
,
e
.
getClass
().
getSimpleName
(),
e
.
getMessage
(),
e
);
log
.
error
(
"完整堆栈: "
,
e
);
try
{
try
{
Thread
.
sleep
(
5000
);
Thread
.
sleep
(
5000
);
}
catch
(
InterruptedException
ex
)
{
}
catch
(
InterruptedException
ex
)
{
Thread
.
currentThread
().
interrupt
();
Thread
.
currentThread
().
interrupt
();
log
.
warn
(
"Redis 订阅线程被中断,退出循环"
);
break
;
break
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment