feat: 添加 singleton-server-id 配置

This commit is contained in:
2024-04-24 14:12:06 +08:00
parent 08a41c1209
commit 5c913fa2db
5 changed files with 19 additions and 1 deletions

View File

@@ -1,6 +1,13 @@
# 是否允许在控制台输出调试信息 # 是否允许在控制台输出调试信息
debug: false debug: false
# 是否启用服务器 ID 单例模式
# 启用后,当一个服务器启动后将会占用 服务器唯一识别码
# 其他使用相同 服务器唯一识别码 的服务器将无法启动
# 测试端中可关闭该功能
# 推荐在正式服中开启该功能以防止服务器 ID 重复
singleton-server-id: false
# 频道名前缀 # 频道名前缀
# 使用这个配置选项可以划分子服消息通信分组 # 使用这个配置选项可以划分子服消息通信分组
# 只有在同一个频道名的子服才能互相通信 # 只有在同一个频道名的子服才能互相通信

View File

@@ -1,6 +1,10 @@
# 是否允许在控制台输出调试信息 # 是否允许在控制台输出调试信息
debug: false debug: false
# 是否启用服务器 ID 单例模式
# 启用后,服务器唯一识别码 相同的服务器将无法启动
singleton-server-id: false
# 频道名前缀 # 频道名前缀
# 使用这个配置选项可以划分子服消息通信分组 # 使用这个配置选项可以划分子服消息通信分组
# 只有在同一个频道名的子服才能互相通信 # 只有在同一个频道名的子服才能互相通信

View File

@@ -108,7 +108,7 @@ public abstract class BallAPI {
protected void enable() throws SQLException, InterruptedException { protected void enable() throws SQLException, InterruptedException {
try (Jedis jedis = CoreAPI.getInstance().getJedisPool().getResource()) { try (Jedis jedis = CoreAPI.getInstance().getJedisPool().getResource()) {
String key = "HamsterBall:ServerInfo:" + localServerInfo.getId(); String key = "HamsterBall:ServerInfo:" + localServerInfo.getId();
if (jedis.exists(key)) { if (jedis.exists(key) && ballConfig.isSingletonServerID()) {
throw new IllegalStateException("已经有一个服务器占用了该 ID"); throw new IllegalStateException("已经有一个服务器占用了该 ID");
} }
jedis.hset(key, "id", localServerInfo.getId()); jedis.hset(key, "id", localServerInfo.getId());

View File

@@ -11,13 +11,16 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public class BallConfig { public class BallConfig {
private boolean debug; private boolean debug;
private boolean singletonServerID;
@NotNull @NotNull
private String channelPrefix; private String channelPrefix;
private boolean gameServerUpdatePlayerInfo; private boolean gameServerUpdatePlayerInfo;
@NotNull
private List<String> loadPlayerInfoFilter; private List<String> loadPlayerInfoFilter;
public BallConfig(@NotNull ConfigSection config) { public BallConfig(@NotNull ConfigSection config) {
debug = config.getBoolean("debug", false); debug = config.getBoolean("debug", false);
singletonServerID = config.getBoolean("singleton-server-id", false);
channelPrefix = config.getString("channel-prefix", ""); channelPrefix = config.getString("channel-prefix", "");
channelPrefix = channelPrefix.isEmpty() ? channelPrefix : channelPrefix + ":"; channelPrefix = channelPrefix.isEmpty() ? channelPrefix : channelPrefix + ":";
gameServerUpdatePlayerInfo = config.getBoolean("game-server-update-player-info", false); gameServerUpdatePlayerInfo = config.getBoolean("game-server-update-player-info", false);

View File

@@ -1,6 +1,10 @@
# 是否允许在控制台输出调试信息 # 是否允许在控制台输出调试信息
debug: false debug: false
# 是否启用服务器 ID 单例模式
# 启用后,服务器唯一识别码 相同的服务器将无法启动
singleton-server-id: false
# 频道名前缀 # 频道名前缀
# 使用这个配置选项可以划分子服消息通信分组 # 使用这个配置选项可以划分子服消息通信分组
# 只有在同一个频道名的子服才能互相通信 # 只有在同一个频道名的子服才能互相通信