使用 Jedis 连接 Redis Cluster

文章目录


1. 引入依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

2. 参数配置

# Redis集群服务器地址
redis.host1=192.168.000.11
redis.host2=192.168.000.12
redis.host3=192.168.000.13
# Redis服务器连接端口
redis.master.port=6379
redis.slave.port=6380
# Redis服务器连接密码(默认为空)
redis.password=xxxx
# 连接超时时间
redis.connection-timeout=2000
# 读取数据超时时间
redis.so-timeout=2000
# 连接超时或读取超时进行重试的次数
redis.max-attempts=3
# 开启对象验证,保证可用
redis.testOnBorrow=true

3. 代码实现

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 * Redis Cluster 配置
 *
 * @author wangbo
 * @date 2021/6/15
 */
@Slf4j
public class JedisClusterManager {

    private JedisClusterManager() {
    }

    private static final JedisCluster JEDIS_CLUSTER;

    static {
        Properties props = new Properties();
        try {
            props.load(JedisClusterManager.class.getResourceAsStream(PropertiesConstants.PROPERTIES_FILE_REDIS));
        } catch (IOException e) {
            log.error("load redis config properties exception", e);
        }

        String redisHost1 = props.getProperty("redis.host1");
        String redisHost2 = props.getProperty("redis.host2");
        String redisHost3 = props.getProperty("redis.host3");
        int masterPort = Integer.parseInt(props.getProperty("redis.master.port"));
        int slavePort = Integer.parseInt(props.getProperty("redis.slave.port"));

        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort(redisHost1, masterPort));
        nodes.add(new HostAndPort(redisHost2, masterPort));
        nodes.add(new HostAndPort(redisHost3, masterPort));
        nodes.add(new HostAndPort(redisHost1, slavePort));
        nodes.add(new HostAndPort(redisHost2, slavePort));
        nodes.add(new HostAndPort(redisHost3, slavePort));

        String password = props.getProperty("redis.password");
        int connectionTimeout = Integer.parseInt(props.getProperty("redis.connection-timeout"));
        int soTimeout = Integer.parseInt(props.getProperty("redis.so-timeout"));
        int maxAttempts = Integer.parseInt(props.getProperty("redis.max-attempts"));

        boolean testOnBorrow = Boolean.parseBoolean(props.getProperty("redis.testOnBorrow"));

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);

        JEDIS_CLUSTER = new JedisCluster(nodes, connectionTimeout, soTimeout, maxAttempts, password, jedisPoolConfig);
    }

    /**
     * 获取JedisCluster对象
     */
    public static JedisCluster getJedis() {
        return JEDIS_CLUSTER;
    }

}

然后每次使用的时候直接在程序中使用如下代码获取 JedisCluster 对象即可使用 Jedis 提供的各种操作 Redis 的方法:

JedisCluster jedisCluster = JedisClusterManager.getJedis();
上一篇:7.24Java之配置JDBC配置文件


下一篇:如何写.bat批处理文件