Redis集群与springboot项目及与springmvc项目整合

我之前自己搞的redis集群和springboot项目整合,以及springmvc项目整合案例,在此记录,方便以后查找。

Redis集群与springboot项目整合

1、引入对应jar

如果springboot使用的版本较新,比如:2.2.6.RELEASE;

Jedis jar包版本要和spring-boot-starter-data-redis 中的版本相对应,可以点进去查看对应的jedis版本;

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

    <version>3.1.0</version>

</dependency>

 

2、在src\main\resources\application.properties  或者application.yml 文件中进行对应配置(使用redis连接池);

##redis集群

spring.redis.cluster.nodes=192.168.233.128:6379,192.168.233.128:6380,192.168.233.128:6381,192.168.233.128:6389,192.168.233.128:6390,192.168.233.128:6391

spring.redis.cluster.max-redirects=6

#客户端超时时间单位是毫秒 默认是2000

redis.timeout=10000

#最大空闲数

redis.maxIdle=300

#控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性

redis.maxTotal=1000

#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。

redis.maxWaitMillis=1000

#连接的最小空闲时间 默认1800000毫秒(30分钟)

redis.minEvictableIdleTimeMillis=300000

#每次释放连接的最大数目,默认3

redis.numTestsPerEvictionRun=1024

#逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1

redis.timeBetweenEvictionRunsMillis=30000

#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个

redis.testOnBorrow=true

#在空闲时检查有效性, 默认false

redis.testWhileIdle=true

redis.database=1

以上参数大家视自己需要修改;

 

3、定义对应的配置类;最终得到注册的RedisTemplate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

@Configuration
public class JedisClusterConfig {

    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.maxWaitMillis}")
    private long maxWaitMillis;
    @Value("${redis.minEvictableIdleTimeMillis}")
    private long minEvictableIdl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值