Spring Boot连接Redis集群

一、场景

搭建Redis集群后,使用Spring Boot连接Redis集群

二、连接方式

1. 导入依赖

<!--redis start-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>
<!--redis end-->

2. 配置

application.properties

## redis配置  默认密码为空
spring.redis.cluster.nodes=ip1:7001,ip1:7002,ip2:7001,ip2:7002,ip3:7001,ip3:7002
spring.redis.password=集群密码
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=6000
# 使用连接时,检测连接是否成功
redis.testOnBorrow=true

使用方式:

@Resource
private RedisTemplate<Object,Object> redisTemplate

补充:
项目运行时,可能需要几秒连接Redis集群,因此,在项目一启动时立刻使用Redis进行增删改查可能会有部分请求报错。

到这里就配置好了,如果需要在静态方法中使用Redis集群,请往下看:

package com.xiaor.authority.utils.redis;

import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import java.util.Collections;
import java.util.List;

public class RedisUtil {

    public static JedisConnectionFactory connectionFactory;
    static {
        //集群模式
        RedisClusterConfiguration rcc = new RedisClusterConfiguration();
        rcc.setPassword("集群密码");
        List<RedisNode> nodes = Collections.singletonList(new RedisNode("主节点ip",7001));
        rcc.setClusterNodes(nodes);
        connectionFactory = new JedisConnectionFactory(rcc);
        connectionFactory.afterPropertiesSet();
    }

    public static RedisTemplate<Object,Object> getRedisTemplate(){

        RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer();
        redisTemplate.setDefaultSerializer(serializer);
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setValueSerializer(serializer);

        /**
         * 必须执行这个函数,初始化RedisTemplate
         */
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    public static void main(String[] args) {
        RedisTemplate<Object,Object> redisTemplate = getRedisTemplate();
        redisTemplate.opsForValue().set("abcd","123456");
        String ips = (String) redisTemplate.opsForValue().get("abcd");
        redisTemplate.delete("abcd");
        System.out.println(ips);
    }
}

注意:
这里节点ip只需要填写主节点即可。

使用方式:

private static RedisTemplate<Object,Object> redisTemplate = RedisUtil.getRedisTemplate();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值