feat: 将redission更换为lettuce

This commit is contained in:
2023-10-29 22:54:09 +08:00
parent 64b110718e
commit ef0960b6e8
14 changed files with 161 additions and 224 deletions

View File

@@ -5,18 +5,8 @@ dependencies {
compileOnly("net.md-5:bungeecord-api:+")
compileOnly("cn.hamster3.mc.plugin:core-bungeecord:+")
implementation("org.redisson:redisson:+") {
// exclude(group = "io.netty", module="netty-codec")
// exclude(group = "io.netty", module="netty-codec-dns")
// exclude(group = "io.netty", module="netty-buffer")
// exclude(group = "io.netty", module="netty-handler")
// exclude(group = "io.netty", module="netty-resolver")
// exclude(group = "io.netty", module="netty-transport")
// exclude(group = "org.yaml")
// exclude(group = "io.netty")
exclude(group = "org.slf4j")
}
// https://mvnrepository.com/artifact/io.lettuce/lettuce-core
implementation("io.lettuce:lettuce-core:6.2.6.RELEASE")
}
tasks {

View File

@@ -1,49 +1,32 @@
package cn.hamster3.mc.plugin.core.bungee.api;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
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.util.BallBungeeCordUtils;
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import io.lettuce.core.RedisClient;
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 {
@Nullable
private final DataSource datasource;
@NotNull
private final RedissonClient redissonClient;
public BallBungeeCordAPI(@NotNull BallServerInfo localServerInfo, @Nullable DataSource datasource, @NotNull RedissonClient redissonClient) {
super(localServerInfo);
this.datasource = datasource;
this.redissonClient = redissonClient;
public BallBungeeCordAPI(@NotNull BallServerInfo localServerInfo, @Nullable DataSource datasource, @NotNull RedisClient redisClient, boolean debug) {
super(localServerInfo, datasource, redisClient, debug);
}
public static BallBungeeCordAPI getInstance() {
return (BallBungeeCordAPI) instance;
}
public static void init() throws IOException {
public static void init() {
if (instance != null) {
return;
}
@@ -65,28 +48,14 @@ public class BallBungeeCordAPI extends BallAPI {
if (config.contains("datasource")) {
plugin.getLogger().info("检测到配置文件中包含 datasource 节点,启用自定义数据库连接.");
datasource = BallBungeeCordUtils.getDataSource(config.getSection("datasource"));
}else {
} else {
plugin.getLogger().info("未检测到配置文件中的 datasource 节点,复用 HamsterCore 数据库连接.");
datasource = CoreAPI.getInstance().getDataSource();
}
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);
RedisClient redisClient = RedisClient.create(config.getString("redis-url", "redis://localhost:6379?clientName=HamsterBall"));
if (config.getBoolean("debug", false)) {
RTopic topic = redissonClient.getTopic(BALL_CHANNEL, BallMessageInfoCodec.INSTANCE);
topic.addListener(BallMessageInfo.class, BallDebugListener.INSTANCE);
}
instance = apiInstance;
instance = new BallBungeeCordAPI(serverInfo, datasource, redisClient, config.getBoolean("debug", false));
}
@Override
@@ -103,14 +72,4 @@ public class BallBungeeCordAPI extends BallAPI {
public @NotNull Logger getLogger() {
return HamsterBallPlugin.getInstance().getLogger();
}
@Override
public @NotNull DataSource getDatasource() {
return datasource == null ? CoreAPI.getInstance().getDataSource() : datasource;
}
@Override
public @NotNull RedissonClient getRedissonClient() {
return redissonClient;
}
}

View File

@@ -1,6 +1,13 @@
# 是否允许在控制台输出调试信息
debug: false
# redis 连接配置,连接格式如下:
# redis://用户名:密码@主机名:端口/数据库索引?参数名=参数值&参数名=参数值
# 若没有设置用户名和密码,则可以省略
# 若不设置数据库,则默认使用 0
# 详细信息https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details
redis-url: "redis://localhost:6379?clientName=HamsterBall"
server-info:
# 服务器唯一识别码,最长 32 字符
id: "BungeeCord"