Commit 4cde17c3 by zhangxingmin

push

parent 6dabbff2
......@@ -161,32 +161,70 @@ public class CoWebSocketServer {
*/
@EventListener(ApplicationReadyEvent.class)
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(() -> {
log.info("应用已就绪,开始启动 Redis 跨节点订阅...");
doSubscribe();
}).start();
}
/**
* 执行 Redis 订阅(支持自动重连)
* 增加详细日志,便于排查 Redis 连接问题
*/
private void doSubscribe() {
log.info("=== Redis 订阅线程启动 ===");
log.info("当前节点 ID: {}", NODE_ID);
while (true) {
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) -> {
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()
);
log.warn("Redis 订阅意外退出,将重新尝试...");
return null;
}, true);
} catch (Exception e) {
log.error("Redis 订阅断开,5秒后重试...", e);
log.error("Redis 订阅异常,错误类型: {}, 消息: {}",
e.getClass().getSimpleName(), e.getMessage(), e);
log.error("完整堆栈: ", e);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
log.warn("Redis 订阅线程被中断,退出循环");
break;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment