feat: 允许单独配置 redis-url
All checks were successful
Publish Project / build (push) Successful in 4m23s

This commit is contained in:
2025-07-10 20:40:36 +08:00
parent 1e0417c814
commit de10bd7feb
5 changed files with 27 additions and 8 deletions

View File

@@ -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"
# # 密码

View File

@@ -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 不同的数据库链接

View File

@@ -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<String, String> connect = CoreAPI.getInstance().getRedisClient().connect()) {
try (StatefulRedisConnection<String, String> connect = getRedisClient().connect()) {
RedisCommands<String, String> 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<String, String> connect = CoreAPI.getInstance().getRedisClient().connect()) {
try (StatefulRedisConnection<String, String> connect = getRedisClient().connect()) {
RedisCommands<String, String> redis = connect.sync();
String key = "HamsterBall:ServerInfo:" + localServerInfo.getId();
redis.del(key);

View File

@@ -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<String, String> connect = CoreAPI.getInstance().getRedisClient().connect()) {
try (StatefulRedisConnection<String, String> connect = BallAPI.getInstance().getRedisClient().connect()) {
RedisCommands<String, String> redis = connect.sync();
redis.expire(key, 180);
}

View File

@@ -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 不同的数据库链接