Redis整合spring-boot

1、maven依赖包

        <!--redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.4.5</version>
        </dependency>
        <!-- spring2.X 集成 redis 所需 common-pool-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId> <!--不要带版本号,防止冲突-->
        </dependency>

2、RedisTemplate配置类RedisConfig

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;


@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        // 设置 key 和 value 的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 设置 hash key 和 hash value 的序列化器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        
        //----- 解决jackson2无法反序列化LocalDateTime的问题 ----
        Jackson2JsonRedisSerializer<Object> j2jrs = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        // 注册JavaTimeModule模块,用于支持LocalDateTime等Java 8时间类型的序列化和反序列化
        om.registerModule(new JavaTimeModule());
        // 设置ObjectMapper的属性,用于支持Java 8时间类型的序列化和反序列化
        om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        om.disable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
        // 将ObjectMapper设置到Jackson2JsonRedisSerializer中
        j2jrs.setObjectMapper(om);

        //----- 解决jackson2无法反序列化LocalDateTime的问题结束 ----
        return redisTemplate;
    }

}


3、application.yml配置Redis

spring:
  redis:
    host: 192.168.0.188           # Redis 服务器主机名,默认为 localhost
    port: 6379                # Redis 服务器端口,默认为 6379
#   password:                  Redis 服务器密码,默认为空
    database: 0               # Redis 数据库编号,默认为 0
    timeout: 10000ms             # 连接超时时间(毫秒),默认为 3000
    lettuce:
      pool:
        max-active: 8         # 最大活动连接数,默认为 8
        max-idle: 200         # 最大空闲连接数,默认为 8
        min-idle: 5           # 最小空闲连接数,默认为 0
        max-wait: 10000ms     # 最大等待时间(毫秒),默认为 -1,表示无限等待

4、使用Redis演示

package com.wwk.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("redisT1")
    public void testRedis(){
        redisTemplate.opsForValue().set("k1","v1");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值