springboot集成Redis步骤(基于lettuce连接池)

基于lettuce连接池方式集成

1.添加pom.xml文件

<!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            &lt;!&ndash; 1.5的版本默认采用的连接池技术是jedis,2.0以上版本默认连接池是lettuce, 因为此次是采用jedis,所以需要排除lettuce的jar &ndash;&gt;
            <exclusions>
                <exclusion>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        &lt;!&ndash; jedis客户端 &ndash;&gt;
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--spring2.X集成redis所需common-pool2,使用jedis必须依赖它-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

2.application.properties文件 配置(可配置也可不配置)

#redis配置开始
# Redis数据库索引(默认为0)
spring.redis.database=0
  # Redis服务器地址
spring.redis.host=localhost
  # Redis服务器连接端口
spring.redis.port=6379
  # Redis服务器连接密码(默认为空)
spring.redis.password=

#jedis连接池
  # 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=1024
  # 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=10000
  # 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=200
  # 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0

#lettuce连接池
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=1024
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=10000
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=200
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0


  # 连接超时时间(毫秒)
spring.redis.timeout=10000
  #redis配置结束
spring.redis.block-when-exhausted=true

3.配置RedisConfig —配完这个就可以用了

@Configuration
@EnableCaching
public class RedisConfig {

    /**
     * RedisTemplate配置
     * @param connectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
        // 配置redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());//key序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//value序列化
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

4.调用的时候还用一个坑,在调用的时候必须用@Resource这个注解,用@Autowired会报错,我看见好多的文章都是用Autowired,也不知道是我的问题还是别人的问题,有发现还请告知

 @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;

希望这篇文章希望能帮得到你。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值