feat: 将redission更换为lettuce
This commit is contained in:
@@ -3,15 +3,10 @@
|
||||
dependencies {
|
||||
compileOnly("cn.hamster3.mc.plugin:core-common:+")
|
||||
|
||||
compileOnly("io.netty:netty-buffer:4+")
|
||||
compileOnly("com.google.code.gson:gson:2.8.0")
|
||||
|
||||
implementation("org.redisson:redisson:+") {
|
||||
isTransitive = false
|
||||
exclude(group = "io.netty")
|
||||
exclude(group = "org.yaml")
|
||||
exclude(group = "org.slf4j")
|
||||
}
|
||||
// https://mvnrepository.com/artifact/io.lettuce/lettuce-core
|
||||
compileOnly("io.lettuce:lettuce-core:6.2.6.RELEASE")
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.api;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.codec.BallMessageInfoCodec;
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallLocation;
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
@@ -11,18 +10,18 @@ import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerConnectServerEve
|
||||
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOfflineEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.BallListener;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.BallMessageListener;
|
||||
import cn.hamster3.mc.plugin.ball.common.listener.ListenerPriority;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.redisson.api.RTopic;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.client.codec.StringCodec;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.*;
|
||||
@@ -47,19 +46,31 @@ public abstract class BallAPI {
|
||||
protected final Map<UUID, BallPlayerInfo> playerInfo;
|
||||
@NotNull
|
||||
private final BallServerInfo localServerInfo;
|
||||
@Nullable
|
||||
private final DataSource datasource;
|
||||
@NotNull
|
||||
private final RedisClient redisClient;
|
||||
@NotNull
|
||||
private final StatefulRedisPubSubConnection<String, BallMessageInfo> pubSubConnection;
|
||||
|
||||
@NotNull
|
||||
private List<BallListener> listeners;
|
||||
|
||||
@Getter
|
||||
private boolean enabled;
|
||||
|
||||
public BallAPI(@NotNull BallServerInfo localServerInfo) {
|
||||
public BallAPI(@NotNull BallServerInfo localServerInfo, @Nullable DataSource datasource, @NotNull RedisClient redisClient, boolean debug) {
|
||||
this.localServerInfo = localServerInfo;
|
||||
|
||||
this.datasource = datasource;
|
||||
this.redisClient = redisClient;
|
||||
pubSubConnection = redisClient.connectPubSub(BallMessageInfo.CODEC);
|
||||
serverInfo = new ConcurrentHashMap<>();
|
||||
playerInfo = new ConcurrentHashMap<>();
|
||||
listeners = new ArrayList<>();
|
||||
enabled = false;
|
||||
initListener(debug);
|
||||
}
|
||||
|
||||
private void initListener(boolean debug) {
|
||||
addListener(new BallListener() {
|
||||
@Override
|
||||
public ListenerPriority getPriority() {
|
||||
@@ -129,7 +140,10 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
});
|
||||
enabled = false;
|
||||
if (debug) {
|
||||
getLogger().warning("已启用调试模式");
|
||||
pubSubConnection.addListener(BallDebugListener.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
protected void enable() throws SQLException, InterruptedException {
|
||||
@@ -201,9 +215,9 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
}
|
||||
RTopic topic = getRedissonClient().getTopic(BALL_CHANNEL, BallMessageInfoCodec.INSTANCE);
|
||||
topic.addListener(BallMessageInfo.class, BallMessageListener.INSTANCE);
|
||||
|
||||
RedisClient client = getRedisClient();
|
||||
pubSubConnection.addListener(BallMessageListener.INSTANCE);
|
||||
pubSubConnection.async().subscribe("HamsterBall");
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
@@ -226,7 +240,7 @@ public abstract class BallAPI {
|
||||
}
|
||||
}
|
||||
getLogger().info("正在关闭 redission...");
|
||||
getRedissonClient().shutdown();
|
||||
getRedisClient().close();
|
||||
getLogger().info("已关闭 redission.");
|
||||
}
|
||||
|
||||
@@ -535,11 +549,12 @@ public abstract class BallAPI {
|
||||
*/
|
||||
public void sendBallMessage(@NotNull BallMessageInfo messageInfo, boolean block) {
|
||||
String string = CoreAPI.getInstance().getGson().toJson(messageInfo);
|
||||
RTopic topic = getRedissonClient().getTopic(BallAPI.BALL_CHANNEL, StringCodec.INSTANCE);
|
||||
if (block) {
|
||||
topic.publish(string);
|
||||
} else {
|
||||
topic.publishAsync(string);
|
||||
try (StatefulRedisPubSubConnection<String, BallMessageInfo> connection = getRedisClient().connectPubSub(BallMessageInfo.CODEC)) {
|
||||
if (block) {
|
||||
connection.sync().publish("HamsterBall", messageInfo);
|
||||
} else {
|
||||
connection.async().publish("HamsterBall", messageInfo);
|
||||
}
|
||||
}
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
@@ -709,8 +724,12 @@ public abstract class BallAPI {
|
||||
public abstract Logger getLogger();
|
||||
|
||||
@NotNull
|
||||
public abstract DataSource getDatasource();
|
||||
public DataSource getDatasource() {
|
||||
return datasource == null ? CoreAPI.getInstance().getDataSource() : datasource;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract RedissonClient getRedissonClient();
|
||||
public RedisClient getRedisClient() {
|
||||
return redisClient;
|
||||
}
|
||||
}
|
||||
|
@@ -1,41 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.ball.common.codec;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.redisson.client.codec.BaseCodec;
|
||||
import org.redisson.client.protocol.Decoder;
|
||||
import org.redisson.client.protocol.Encoder;
|
||||
import org.redisson.codec.JsonCodec;
|
||||
|
||||
public class BallMessageInfoCodec extends BaseCodec implements JsonCodec<BallMessageInfoCodec> {
|
||||
public static final BallMessageInfoCodec INSTANCE = new BallMessageInfoCodec();
|
||||
|
||||
private final Encoder encoder = in -> {
|
||||
ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
|
||||
out.writeCharSequence(in.toString(), CharsetUtil.UTF_8);
|
||||
return out;
|
||||
};
|
||||
|
||||
private final Decoder<Object> decoder = (buf, state) -> {
|
||||
String str = buf.toString(CharsetUtil.UTF_8);
|
||||
buf.readerIndex(buf.readableBytes());
|
||||
return CoreAPI.getInstance().getGson().fromJson(str, BallMessageInfo.class);
|
||||
};
|
||||
|
||||
private BallMessageInfoCodec() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Decoder<Object> getValueDecoder() {
|
||||
return decoder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Encoder getValueEncoder() {
|
||||
return encoder;
|
||||
}
|
||||
|
||||
}
|
@@ -6,12 +6,15 @@ import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.lettuce.core.codec.RedisCodec;
|
||||
import io.lettuce.core.codec.StringCodec;
|
||||
import lombok.Data;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -20,6 +23,31 @@ import java.util.UUID;
|
||||
@Data
|
||||
@SuppressWarnings("unused")
|
||||
public class BallMessageInfo {
|
||||
/**
|
||||
* 编解码器
|
||||
*/
|
||||
public static final RedisCodec<String, BallMessageInfo> CODEC = new RedisCodec<String, BallMessageInfo>() {
|
||||
@Override
|
||||
public String decodeKey(ByteBuffer bytes) {
|
||||
return StringCodec.UTF8.decodeKey(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BallMessageInfo decodeValue(ByteBuffer bytes) {
|
||||
String string = StringCodec.UTF8.decodeValue(bytes);
|
||||
return CoreAPI.getInstance().getGson().fromJson(string, BallMessageInfo.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer encodeKey(String key) {
|
||||
return StringCodec.UTF8.encodeKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer encodeValue(BallMessageInfo value) {
|
||||
return StringCodec.UTF8.encodeValue(CoreAPI.getInstance().getGson().toJson(value));
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 消息发送者
|
||||
*/
|
||||
|
@@ -2,16 +2,42 @@ package cn.hamster3.mc.plugin.ball.common.listener;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
|
||||
import org.redisson.api.listener.MessageListener;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
public class BallDebugListener implements MessageListener<BallMessageInfo> {
|
||||
public class BallDebugListener implements RedisPubSubListener<String, BallMessageInfo> {
|
||||
public static final BallDebugListener INSTANCE = new BallDebugListener();
|
||||
|
||||
private BallDebugListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(CharSequence channel, BallMessageInfo event) {
|
||||
BallAPI.getInstance().getLogger().info("收到了一条消息: " + event);
|
||||
public void message(String channel, BallMessageInfo event) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + channel + " 收到了一条消息: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, BallMessageInfo event) {
|
||||
BallAPI.getInstance().getLogger().info("从 " + pattern + "(" + channel + ") 收到了一条消息: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已订阅 redis 频道: " + channel);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则): " + pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道: " + channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) {
|
||||
BallAPI.getInstance().getLogger().info("已取消订阅 redis 频道(正则): " + pattern);
|
||||
}
|
||||
}
|
||||
|
@@ -6,16 +6,16 @@ import cn.hamster3.mc.plugin.ball.common.event.player.*;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOfflineEvent;
|
||||
import cn.hamster3.mc.plugin.ball.common.event.server.ServerOnlineEvent;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import org.redisson.api.listener.MessageListener;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
public class BallMessageListener implements MessageListener<BallMessageInfo> {
|
||||
public class BallMessageListener implements RedisPubSubListener<String, BallMessageInfo> {
|
||||
public static final BallMessageListener INSTANCE = new BallMessageListener();
|
||||
|
||||
private BallMessageListener() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(CharSequence channel, BallMessageInfo info) {
|
||||
public void message(String channel, BallMessageInfo info) {
|
||||
for (BallListener listener : BallAPI.getInstance().getListeners()) {
|
||||
try {
|
||||
listener.onMessageReceived(info);
|
||||
@@ -139,4 +139,29 @@ public class BallMessageListener implements MessageListener<BallMessageInfo> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, BallMessageInfo info) {
|
||||
message(channel, info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,36 +0,0 @@
|
||||
singleServerConfig:
|
||||
address: "redis://localhost:6379"
|
||||
password: "Reids123.."
|
||||
username: "default"
|
||||
database: 1
|
||||
clientName: "HamsterBall"
|
||||
idleConnectionTimeout: 10000
|
||||
connectTimeout: 10000
|
||||
timeout: 3000
|
||||
retryAttempts: 3
|
||||
retryInterval: 1500
|
||||
subscriptionsPerConnection: 5
|
||||
sslEnableEndpointIdentification: true
|
||||
sslProvider: "JDK"
|
||||
pingConnectionInterval: 30000
|
||||
keepAlive: false
|
||||
tcpNoDelay: true
|
||||
subscriptionConnectionMinimumIdleSize: 1
|
||||
subscriptionConnectionPoolSize: 50
|
||||
connectionMinimumIdleSize: 1
|
||||
connectionPoolSize: 10
|
||||
dnsMonitoringInterval: 5000
|
||||
threads: 4
|
||||
nettyThreads: 4
|
||||
referenceEnabled: true
|
||||
lockWatchdogTimeout: 30000
|
||||
checkLockSyncedSlaves: true
|
||||
slavesSyncTimeout: 1000
|
||||
reliableTopicWatchdogTimeout: 600000
|
||||
keepPubSubOrder: true
|
||||
useScriptCache: false
|
||||
minCleanUpDelay: 5
|
||||
maxCleanUpDelay: 1800
|
||||
cleanUpKeysAmount: 100
|
||||
useThreadClassLoader: true
|
||||
lazyInitialization: false
|
Reference in New Issue
Block a user