SpringBoot旧版集成Redis集群
        【摘要】 
                    
                        
                    
                    SpringBoot旧版集成Redis集群 
pom 
<!-- redis -->
        <!--        <dependency>-->
     ...
    
    
    
    SpringBoot旧版集成Redis集群
pom
<!-- redis -->
        <!--        <dependency>-->
        <!--            <groupId>org.springframework.boot</groupId>-->
        <!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
        <!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!--        =======end======-->
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
application.properties
#集群配置 旧版
spring.cluster.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
# Redis服务器连接密码(默认为空)
spring.cluster.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.cluster.redis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.cluster.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.cluster.redis.pool.max-idle=20
# 连接池中的最小空闲连接
spring.cluster.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.cluster.redis.timeout=60000
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
RedisClusterConfig
@Configuration
public class RedisClusterConfig {
    @Value("${spring.cluster.redis.cluster.nodes}")
    private String clusterNodes;
    @Value("${spring.cluster.redis.timeout}")
    private int timeout;
    @Value("${spring.cluster.redis.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.cluster.redis.pool.min-idle}")
    private int minIdle;
    @Value("${spring.cluster.redis.pool.max-active}")
    private int maxActive;
    @Value("${spring.cluster.redis.pool.max-wait}")
    private long maxWait;
    @Bean
    public JedisCluster getJedisCluster() {
        return new JedisCluster(getNodes(), timeout, poolConfig());
    }
    private JedisPoolConfig poolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxTotal(maxActive);
        config.setMaxWaitMillis(maxWait);
        return config;
    }
    private Set<HostAndPort> getNodes() {
        String[] cNodes = clusterNodes.split(",");
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        // 分割出集群节点
        String[] hp;
        for (String node : cNodes) {
            hp = node.split(":");
            nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
        }
        return nodes;
    }
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
ClusterRedisUtil
@Component
public class ClusterRedisUtil {
    @Autowired
    private JedisCluster jedis;
    public boolean isExist(String key){
        return jedis.exists(key);
    }
    public void putValue(String key,String value){
        jedis.set(key,value);
    }
    public void putValue(String key,String value,int time){
        jedis.set(key,value);
        jedis.expire(key,time);
    }
    public String getValue(String key){
        return jedis.get(key);
    }
    public void delKey(String key){
        jedis.del(key);
    }
}
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
文章来源: blog.csdn.net,作者:小毕超,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43692950/article/details/107443610
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)