redis配置文件_redis怎么连接

redis配置文件_redis怎么连接dd#redis配置开始#Redis数据库索引(默认为0)spring.redis.database=0#Redis服务器地址#redis.host=192.168.59.43redis.host1=192.168.58.11redis.host2=192.168.58.12redis.host3=192.168.58.13#Redis服务器连接端口redis.port=6379redis.master.port=6379redis.slave.port=6380#Re.

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺


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();
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195460.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号