欢迎您的访问
专注于分享最有价值的互联网技术干货

SpringBoot下JedisCluster方式连接Redis集群的配置

几个T的资料等你来白嫖
双倍快乐
资源帮找!!!

最近在使用springboot做项目,使用redis做缓存。在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器。但是项目代码挪到内网开发以后,内网redis服务器使用了集群的配置方式。所以原来的配置文件和帮助类 都完全不能使用了,所以最近对redis集群的配置进行了简单的研究。

1.首先是引入配置文件

gradle方式的配置文件
compile 'redis.clients:jedis:2.9.0'

2.application.yml的配置

spring:
  application:
    name: xxxx
  session:
    store-type: redis
  redis:
    password: xxxxx
    clusterNodes: xxxxxxx
    expireSeconds: 120
    commandTimeout: 10000  #redis操作的超时时间
    pool:
     maxActive: 5000 #最大连接数
     maxIdle: 30 #最大空闲连接数
     minIdle: 5 #最小空闲连接数
     maxWait: 3000  #获取连接最大等待时间 ms  #default -1

3.新增类RedisProperties

@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    private int    expireSeconds;
    private String clusterNodes;
    private String password;
    private int    commandTimeout;
}

4.JedisCluster方式连接集群的配置

@Configuration
public class JedisClusterConfig {

    @Autowired
    private RedisProperties redisProperties;

    /**
     * 注意:
     * 这里返回的JedisCluster是单例的,并且可以直接注入到其他类中去使用
     * @return
     */
    @Bean
    public JedisCluster getJedisCluster() {
        String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)
        Set<HostAndPort> nodes = new HashSet<>();

        for (String ipPort : serverArray) {
            String[] ipPortPair = ipPort.split(":");
            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
        }

        return new JedisCluster(nodes,redisProperties.getCommandTimeout(),1000,1,redisProperties.getPassword() ,new GenericObjectPoolConfig());//需要密码连接的创建对象方式
    }

}

5.redis帮助

@Component
public class RedisUtil {
    private static final Logger LOGGER    = LoggerFactory.getLogger(RedisUtil.class);

    @Autowired
    private JedisCluster  jedisCluster;

    /**
     * 设置缓存
     * @param key    缓存key
     * @param value  缓存value
     */
    public void set(String key, String value) {
        jedisCluster.set(key, value);
        LOGGER.debug("RedisUtil:set cache key={},value={}", key, value);
    }

    /**
     * 设置缓存对象
     * @param key    缓存key
     * @param obj  缓存value
     */
    public <T> void setObject(String key, T obj , int expireTime) {
        jedisCluster.setex(key, expireTime, JSON.toJSONString(obj));
    }

    /**
     * 获取指定key的缓存
     * @param key---JSON.parseObject(value, User.class);
     */
    public String getObject(String key) {
        return jedisCluster.get(key);
    }

    /**
     * 判断当前key值 是否存在
     *
     * @param key
     */
    public boolean hasKey(String key) {
        return jedisCluster.exists(key);
    }

    /**
     * 设置缓存,并且自己指定过期时间
     * @param key
     * @param value
     * @param expireTime 过期时间
     */
    public void setWithExpireTime( String key, String value, int expireTime) {
        jedisCluster.setex(key, expireTime, value);
        LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", key, value, expireTime);
    }

    /**
     * 获取指定key的缓存
     * @param key
     */
    public String get(String key) {
        String value = jedisCluster.get(key);
        LOGGER.debug("RedisUtil:get cache key={},value={}",key, value);
        return value;
    }

    /**
     * 删除指定key的缓存
     * @param key
     */
    public void delete(String key) {
        jedisCluster.del(key);
        LOGGER.debug("RedisUtil:delete cache key={}", key);
    }

}

6.帮助类的使用方式

@Autowired
    private RedisUtil redisUtil;

  

赞(0) 打赏
版权归原创作者所有,任何形式转载请联系我们:大白菜博客 » SpringBoot下JedisCluster方式连接Redis集群的配置

评论 抢沙发

8 + 4 =
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏