feat: 支持 Redis 5.x

This commit is contained in:
2026-02-14 04:17:14 +08:00
parent cee9c0aa1e
commit a9c83cd544
2 changed files with 27 additions and 24 deletions

View File

@@ -74,7 +74,9 @@ public abstract class BallAPI {
private final Map<UUID, BallPlayerInfo> allPlayerInfo;
@NotNull
private final StatefulRedisPubSubConnection<String, String> redisPubSub;
private final StatefulRedisPubSubConnection<String, String> redisPub;
@NotNull
private final StatefulRedisPubSubConnection<String, String> redisSub;
@Nullable
private ScheduledFuture<?> lockUpdater;
@@ -102,7 +104,8 @@ public abstract class BallAPI {
eventBus.register(BallCommonListener.INSTANCE);
allServerInfo = new ConcurrentHashMap<>();
allPlayerInfo = new ConcurrentHashMap<>();
redisPubSub = getRedisClient().connectPubSub();
redisPub = getRedisClient().connectPubSub();
redisSub = getRedisClient().connectPubSub();
getLogger().info("频道前缀: " + ballConfig.getChannelPrefix());
getLogger().info("启用子服更新玩家状态: " + ballConfig.isGameServerUpdatePlayerInfo());
if (ballConfig.isGameServerUpdatePlayerInfo()) {
@@ -112,8 +115,8 @@ public abstract class BallAPI {
getLogger().warning("已启用调试模式");
eventBus.register(BallDebugListener.INSTANCE);
}
redisPubSub.addListener(BallRedisListener.INSTANCE);
redisPubSub.sync().subscribe(BALL_CHANNEL);
redisSub.addListener(BallRedisListener.INSTANCE);
redisSub.sync().subscribe(BALL_CHANNEL);
}
protected void enable() throws SQLException, InterruptedException {
@@ -144,19 +147,19 @@ public abstract class BallAPI {
try (Connection connection = getDatasource().getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_player_info`(" +
"`uuid` CHAR(36) PRIMARY KEY," +
"`name` VARCHAR(16) NOT NULL," +
"`game_server` VARCHAR(32) NOT NULL," +
"`proxy_server` VARCHAR(32) NOT NULL," +
"`online` BOOLEAN NOT NULL" +
") CHARSET utf8mb4;");
"`uuid` CHAR(36) PRIMARY KEY," +
"`name` VARCHAR(16) NOT NULL," +
"`game_server` VARCHAR(32) NOT NULL," +
"`proxy_server` VARCHAR(32) NOT NULL," +
"`online` BOOLEAN NOT NULL" +
") CHARSET utf8mb4;");
statement.execute("CREATE TABLE IF NOT EXISTS `hamster_ball_cached_message`(" +
"`uuid` CHAR(36) NOT NULL," +
"`message` TEXT NOT NULL," +
"`time` DATETIME NOT NULL DEFAULT NOW()," +
"INDEX `idx_uuid` USING BTREE (`uuid`)," +
"INDEX `idx_time` USING BTREE (`time`)" +
") CHARSET utf8mb4;");
"`uuid` CHAR(36) NOT NULL," +
"`message` TEXT NOT NULL," +
"`time` DATETIME NOT NULL DEFAULT NOW()," +
"INDEX `idx_uuid` USING BTREE (`uuid`)," +
"INDEX `idx_time` USING BTREE (`time`)" +
") CHARSET utf8mb4;");
}
if (getBallConfig().isGameServerUpdatePlayerInfo()) {
try (Statement statement = connection.createStatement()) {
@@ -219,7 +222,7 @@ public abstract class BallAPI {
statement.executeUpdate();
}
}
redisPubSub.close();
redisPub.close();
}
/**
@@ -501,12 +504,12 @@ public abstract class BallAPI {
channel = ballConfig.getChannelPrefix() + channel;
}
if (block) {
redisPubSub.sync().publish(channel, CoreAPI.getInstance().getGson().toJson(message));
redisPub.sync().publish(channel, CoreAPI.getInstance().getGson().toJson(message));
eventBus.post(new MessageSentEvent(channel, message));
} else {
@NotNull String finalChannel = channel;
CoreAPI.getInstance().getExecutorService().execute(() -> {
redisPubSub.sync().publish(finalChannel, CoreAPI.getInstance().getGson().toJson(message));
redisPub.sync().publish(finalChannel, CoreAPI.getInstance().getGson().toJson(message));
eventBus.post(new MessageSentEvent(finalChannel, message));
});
}
@@ -532,7 +535,7 @@ public abstract class BallAPI {
* @param channels 频道名称
*/
public void subscribeRaw(@NotNull String... channels) {
redisPubSub.sync().subscribe(channels);
redisSub.sync().subscribe(channels);
}
/**
@@ -541,7 +544,7 @@ public abstract class BallAPI {
* @param patterns 频道名称正则表达式
*/
public void subscribePatterns(@NotNull String patterns) {
redisPubSub.sync().psubscribe(patterns);
redisSub.sync().psubscribe(patterns);
}
/**
@@ -564,7 +567,7 @@ public abstract class BallAPI {
* @param channels 频道名称
*/
public void unsubscribeRaw(@NotNull String... channels) {
redisPubSub.sync().unsubscribe(channels);
redisSub.sync().unsubscribe(channels);
}
/**
@@ -573,7 +576,7 @@ public abstract class BallAPI {
* @param patterns 频道名称正则表达式
*/
public void unsubscribePatterns(@NotNull String patterns) {
redisPubSub.sync().punsubscribe(patterns);
redisSub.sync().punsubscribe(patterns);
}
@NotNull