feat: Add support for Redis Sentinels (#216)

* Add support for Redis Sentinels

* Add some comments
feat/data-edit-commands
Roman Alexander 11 months ago committed by GitHub
parent 664c8c3352
commit 22eedc8522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -134,6 +134,17 @@ public class Settings {
@YamlKey("redis.use_ssl")
private boolean redisUseSsl = false;
@YamlComment("If you're using Redis Sentinel, specify the master set name. If you don't know what this is, don't change anything here.")
@YamlKey("redis.sentinel.master")
private String redisSentinelMaster = "";
@YamlComment("List of host:port pairs")
@YamlKey("redis.sentinel.nodes")
private List<String> redisSentinelNodes = new ArrayList<>();
@YamlKey("redis.sentinel.password")
private String redisSentinelPassword = "";
// Synchronization settings
@YamlComment("The mode of data synchronization to use (DELAY or LOCKSTEP). DELAY should be fine for most networks."
@ -324,6 +335,21 @@ public class Settings {
return redisUseSsl;
}
@NotNull
public String getRedisSentinelMaster() {
return redisSentinelMaster;
}
@NotNull
public List<String> getRedisSentinelNodes() {
return redisSentinelNodes;
}
@NotNull
public String getRedisSentinelPassword() {
return redisSentinelPassword;
}
@NotNull
public DataSyncer.Mode getSyncMode() {
return syncMode;

@ -24,11 +24,9 @@ import net.william278.husksync.data.DataSnapshot;
import net.william278.husksync.user.User;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.NotNull;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.Pool;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
@ -47,7 +45,7 @@ public class RedisManager extends JedisPubSub {
private final HuskSync plugin;
private final String clusterId;
private JedisPool jedisPool;
private Pool<Jedis> jedisPool;
private final Map<UUID, CompletableFuture<Optional<DataSnapshot.Packed>>> pendingRequests;
public RedisManager(@NotNull HuskSync plugin) {
@ -71,9 +69,16 @@ public class RedisManager extends JedisPubSub {
config.setMaxIdle(0);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
this.jedisPool = password.isEmpty()
? new JedisPool(config, host, port, 0, useSSL)
: new JedisPool(config, host, port, 0, password, useSSL);
Set<String> redisSentinelNodes = new HashSet<>(plugin.getSettings().getRedisSentinelNodes());
if (redisSentinelNodes.isEmpty()) {
this.jedisPool = password.isEmpty()
? new JedisPool(config, host, port, 0, useSSL)
: new JedisPool(config, host, port, 0, password, useSSL);
} else {
String sentinelPassword = plugin.getSettings().getRedisSentinelPassword();
String redisSentinelMaster = plugin.getSettings().getRedisSentinelMaster();
this.jedisPool = new JedisSentinelPool(redisSentinelMaster, redisSentinelNodes, password.isEmpty() ? null : password, sentinelPassword.isEmpty() ? null : sentinelPassword);
}
// Ping the server to check the connection
try {

Loading…
Cancel
Save