diff --git a/ball-bukkit/src/main/resources/config.yml b/ball-bukkit/src/main/resources/config.yml index 61c4411..a50931c 100644 --- a/ball-bukkit/src/main/resources/config.yml +++ b/ball-bukkit/src/main/resources/config.yml @@ -42,6 +42,11 @@ server-info: # 不填则自动获取 server.properties 文件中的设置 # port: 25577 +# Redis 配置 +# 如果注释该选项则默认使用 HamsterCore 中的连接配置 +# 否则 HamsterBall 将会使用与 HamsterCore 不同的 Redis 链接 +# redis-url: redis://localhost:6379/0?clientName=HamsterBall&timeout=5s + # 数据库连接池配置 # 如果注释该选项则默认使用 HamsterCore 中的连接池配置 # 否则 HamsterBall 将会使用与 HamsterCore 不同的数据库链接 @@ -53,9 +58,6 @@ server-info: # # MySQL数据库链接填写格式: # # jdbc:mysql://{数据库地址}:{数据库端口}/{使用的库名}?参数 # url: "jdbc:mysql://localhost:3306/Test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true" -# # 如果你不需要做多端跨服,那么请使用 sqlite 作本地数据库 ↓ -# # driver: "org.sqlite.JDBC" -# # url: "jdbc:sqlite:./plugins/HamsterCore/database.db" # # 用户名 # username: "root" # # 密码 diff --git a/ball-bungee/src/main/resources/config.yml b/ball-bungee/src/main/resources/config.yml index 907f391..7028948 100644 --- a/ball-bungee/src/main/resources/config.yml +++ b/ball-bungee/src/main/resources/config.yml @@ -33,6 +33,11 @@ server-info: # 不填则自动设置为 25577 port: 25577 +# Redis 配置 +# 如果注释该选项则默认使用 HamsterCore 中的连接配置 +# 否则 HamsterBall 将会使用与 HamsterCore 不同的 Redis 链接 +# redis-url: redis://localhost:6379/0?clientName=HamsterBall&timeout=5s + # 数据库连接池配置 # 如果注释该选项则默认使用 HamsterCore 中的连接池配置 # 否则 HamsterBall 将会使用与 HamsterCore 不同的数据库链接 diff --git a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java index 783e27a..207d756 100644 --- a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java +++ b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/api/BallAPI.java @@ -20,6 +20,7 @@ import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; import cn.hamster3.mc.plugin.core.common.util.CoreUtils; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; +import io.lettuce.core.RedisClient; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.sync.RedisCommands; import io.lettuce.core.pubsub.StatefulRedisPubSubConnection; @@ -60,6 +61,8 @@ public abstract class BallAPI { @NotNull private final DataSource datasource; @NotNull + private final RedisClient redisClient; + @NotNull private final BallServerInfo localServerInfo; @NotNull @@ -81,6 +84,11 @@ public abstract class BallAPI { throw new IllegalArgumentException("配置文件中未找到 server-info 节点"); } localServerInfo = new BallServerInfo(serverInfoConfig, type); + if (config.hasKey("redis-url")) { + redisClient = RedisClient.create(config.getString("redis-url")); + } else { + redisClient = CoreAPI.getInstance().getRedisClient(); + } ConfigSection section = config.getSection("datasource"); if (section != null) { getLogger().info("启用仓鼠球自定义数据库连接池"); @@ -94,7 +102,7 @@ public abstract class BallAPI { eventBus.register(BallCommonListener.INSTANCE); allServerInfo = new ConcurrentHashMap<>(); allPlayerInfo = new ConcurrentHashMap<>(); - redisPubSub = CoreAPI.getInstance().getRedisClient().connectPubSub(); + redisPubSub = getRedisClient().connectPubSub(); getLogger().info("频道前缀: " + ballConfig.getChannelPrefix()); getLogger().info("启用子服更新玩家状态: " + ballConfig.isGameServerUpdatePlayerInfo()); if (ballConfig.isGameServerUpdatePlayerInfo()) { @@ -109,7 +117,7 @@ public abstract class BallAPI { } protected void enable() throws SQLException, InterruptedException { - try (StatefulRedisConnection connect = CoreAPI.getInstance().getRedisClient().connect()) { + try (StatefulRedisConnection connect = getRedisClient().connect()) { RedisCommands redis = connect.sync(); String key = "HamsterBall:ServerInfo:" + localServerInfo.getId(); if (redis.exists(key) > 0 && ballConfig.isSingletonServerID()) { @@ -196,7 +204,7 @@ public abstract class BallAPI { if (lockUpdater != null) { lockUpdater.cancel(true); lockUpdater = null; - try (StatefulRedisConnection connect = CoreAPI.getInstance().getRedisClient().connect()) { + try (StatefulRedisConnection connect = getRedisClient().connect()) { RedisCommands redis = connect.sync(); String key = "HamsterBall:ServerInfo:" + localServerInfo.getId(); redis.del(key); diff --git a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/thread/LockUpdateThread.java b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/thread/LockUpdateThread.java index c010f73..2bef93d 100644 --- a/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/thread/LockUpdateThread.java +++ b/ball-common/src/main/java/cn/hamster3/mc/plugin/ball/common/thread/LockUpdateThread.java @@ -1,7 +1,6 @@ package cn.hamster3.mc.plugin.ball.common.thread; import cn.hamster3.mc.plugin.ball.common.api.BallAPI; -import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.sync.RedisCommands; @@ -14,7 +13,7 @@ public class LockUpdateThread implements Runnable { @Override public void run() { String key = "HamsterBall:ServerInfo:" + BallAPI.getInstance().getLocalServerInfo().getId(); - try (StatefulRedisConnection connect = CoreAPI.getInstance().getRedisClient().connect()) { + try (StatefulRedisConnection connect = BallAPI.getInstance().getRedisClient().connect()) { RedisCommands redis = connect.sync(); redis.expire(key, 180); } diff --git a/ball-velocity/src/main/resources/config.yml b/ball-velocity/src/main/resources/config.yml index 2f15707..a9a8b31 100644 --- a/ball-velocity/src/main/resources/config.yml +++ b/ball-velocity/src/main/resources/config.yml @@ -33,6 +33,11 @@ server-info: # 不填则自动设置为 25577 port: 25577 +# Redis 配置 +# 如果注释该选项则默认使用 HamsterCore 中的连接配置 +# 否则 HamsterBall 将会使用与 HamsterCore 不同的 Redis 链接 +# redis-url: redis://localhost:6379/0?clientName=HamsterBall&timeout=5s + # 数据库连接池配置 # 如果注释该选项则默认使用 HamsterCore 中的连接池配置 # 否则 HamsterBall 将会使用与 HamsterCore 不同的数据库链接