feat: 使用 redission 作为消息中间件

This commit is contained in:
2023-08-20 22:17:32 +08:00
parent fa7f07657f
commit b8d35f3fce
39 changed files with 435 additions and 1380 deletions

View File

@@ -1,40 +0,0 @@
setArchivesBaseName("HamsterBall-BungeeCord")
evaluationDependsOn(':ball-common')
dependencies {
apiShade project(":ball-common") transitive false
//noinspection VulnerableLibrariesLocal
compileOnly 'net.md-5:bungeecord-api:1.20-R0.1-SNAPSHOT' exclude group: 'io.netty'
compileOnly "cn.hamster3.mc.plugin:core-bungeecord:${hamster_core_version}"
}
processResources {
filesMatching("bungee.yml") {
expand(project.properties)
}
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
tasks.compileJava.dependsOn(":ball-common:build")
tasks.register("shadowJar", Jar) {
dependsOn("jar")
from([
tasks.jar.outputs.files.collect {
it.isDirectory() ? it : zipTree(it)
},
configurations.shade.collect {
it.isDirectory() ? it : zipTree(it)
},
configurations.apiShade.collect {
it.isDirectory() ? it : zipTree(it)
},
configurations.implementationShade.collect {
it.isDirectory() ? it : zipTree(it)
}
])
destinationDirectory = rootProject.buildDir
}
tasks.build.dependsOn(shadowJar)

View File

@@ -0,0 +1,30 @@
evaluationDependsOn(":ball-common")
dependencies {
implementation(project(":ball-common")) { isTransitive = false }
compileOnly("net.md-5:bungeecord-api:1.20-R0.1-SNAPSHOT")
val hamsterCoreVersion = property("hamster_core_version")
compileOnly("cn.hamster3.mc.plugin:core-bungeecord:${hamsterCoreVersion}")
compileOnly("me.clip:placeholderapi:2.11.2") { isTransitive = false }
val redissionVersion = property("redission_version")
implementation("org.redisson:redisson:${redissionVersion}") {
exclude(group = "io.netty")
exclude(group = "org.yaml")
exclude(group = "org.slf4j")
}
}
tasks {
processResources {
filesMatching("bungee.yml") {
expand(project.properties)
}
}
withType<Jar>() {
archiveBaseName = "HamsterBall-BungeeCord"
}
}

View File

@@ -22,9 +22,9 @@ public class HamsterBallPlugin extends Plugin {
public void onLoad() {
instance = this;
Logger logger = getLogger();
BallBungeeCordAPI.init();
logger.info("BallBungeeCordAPI 已初始化.");
try {
BallBungeeCordAPI.init();
logger.info("BallBungeeCordAPI 已初始化.");
BallBungeeCordAPI.getInstance().enable();
logger.info("BallBungeeCordAPI 已启动.");
} catch (Exception e) {
@@ -36,7 +36,7 @@ public class HamsterBallPlugin extends Plugin {
@Override
public void onEnable() {
Logger logger = getLogger();
if (!BallAPI.getInstance().isConnected()) {
if (!BallAPI.getInstance().isEnabled()) {
ProxyServer.getInstance().stop("由于 HamsterBall 未能成功连接, 服务器将立即关闭.");
return;
}

View File

@@ -1,73 +1,83 @@
package cn.hamster3.mc.plugin.core.bungee.api;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
import cn.hamster3.mc.plugin.ball.common.codec.BallMessageInfoCodec;
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
import cn.hamster3.mc.plugin.core.bungee.HamsterBallPlugin;
import cn.hamster3.mc.plugin.core.bungee.listener.BallBungeeCordListener;
import cn.hamster3.mc.plugin.core.bungee.util.BallBungeeCordUtils;
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import net.md_5.bungee.config.Configuration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.redisson.Redisson;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Logger;
public class BallBungeeCordAPI extends BallAPI {
private DataSource datasource;
@Nullable
private final DataSource datasource;
@NotNull
private final RedissonClient redissonClient;
public BallBungeeCordAPI(@NotNull BallConfig config) {
super(config);
public BallBungeeCordAPI(@NotNull BallServerInfo localServerInfo, @Nullable DataSource datasource, @NotNull RedissonClient redissonClient) {
super(localServerInfo);
this.datasource = datasource;
this.redissonClient = redissonClient;
}
public static BallBungeeCordAPI getInstance() {
return (BallBungeeCordAPI) instance;
}
public static void init() {
public static void init() throws IOException {
if (instance != null) {
return;
}
HamsterBallPlugin plugin = HamsterBallPlugin.getInstance();
Configuration pluginConfig = CoreBungeeCordUtils.getPluginConfig(plugin);
Configuration config = CoreBungeeCordUtils.getPluginConfig(plugin);
Map<String, String> env = System.getenv();
String serverInfoID = env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID",
pluginConfig.getString("server-info.id"));
String serverInfoName = env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME",
pluginConfig.getString("server-info.name"));
String serverInfoHost = env.getOrDefault("BALL_LOCAL_SERVER_IP",
pluginConfig.getString("server-info.host", "0.0.0.0"));
int serverInfoPort = Integer.parseInt(env.getOrDefault("BALL_LOCAL_SERVER_PORT",
String.valueOf(pluginConfig.getInt("server-info.port", 25577))));
BallServerInfo serverInfo = new BallServerInfo(
serverInfoID,
serverInfoName,
env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID", config.getString("server-info.id")),
env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME", config.getString("server-info.name")),
BallServerType.PROXY,
serverInfoHost,
serverInfoPort
env.getOrDefault("BALL_LOCAL_SERVER_IP", config.getString("server-info.host", "0.0.0.0")),
Integer.parseInt(
env.getOrDefault("BALL_LOCAL_SERVER_PORT", config.getString("server-info.port", "25577"))
)
);
String serverHost = env.getOrDefault("BALL_SERVER_HOST",
pluginConfig.getString("ball-server.host", "ball.hamster3.cn"));
int serverPort = Integer.parseInt(env.getOrDefault("BALL_SERVER_PORT",
String.valueOf(pluginConfig.getInt("ball-server.port", 58888))));
int eventLoopThread = Integer.parseInt(env.getOrDefault("BALL_EVENT_LOOP_THREAD",
String.valueOf(pluginConfig.getInt("ball-server.event-loop-thread", 2))));
BallConfig config = new BallConfig(serverInfo, serverHost, serverPort, eventLoopThread);
BallBungeeCordAPI apiInstance = new BallBungeeCordAPI(config);
apiInstance.datasource = BallBungeeCordUtils.getDataSource(pluginConfig.getSection("datasource"));
apiInstance.addListener(BallBungeeCordListener.INSTANCE);
if (pluginConfig.getBoolean("debug", false)) {
apiInstance.addListener(BallDebugListener.INSTANCE);
DataSource datasource = BallBungeeCordUtils.getDataSource(config.getSection("datasource"));
File redissionConfig = new File(plugin.getDataFolder(), "redission.yml");
if (!redissionConfig.exists()) {
Files.copy(
plugin.getResourceAsStream("redission.yml"),
redissionConfig.toPath(),
StandardCopyOption.REPLACE_EXISTING
);
}
RedissonClient redissonClient = Redisson.create(Config.fromYAML(redissionConfig));
BallBungeeCordAPI apiInstance = new BallBungeeCordAPI(serverInfo, datasource, redissonClient);
if (config.getBoolean("debug", false)) {
RTopic topic = redissonClient.getTopic(BALL_CHANNEL, BallMessageInfoCodec.INSTANCE);
topic.addListener(BallMessageInfo.class, BallDebugListener.INSTANCE);
}
instance = apiInstance;
}
@@ -90,4 +100,9 @@ public class BallBungeeCordAPI extends BallAPI {
public @NotNull DataSource getDatasource() {
return datasource == null ? CoreAPI.getInstance().getDataSource() : datasource;
}
@Override
public @NotNull RedissonClient getRedissonClient() {
return redissonClient;
}
}