【开发心得】在SpringBoot体系中正确使用redisConfig

一、代码示例

redis配置

java 复制代码
package com.xxx.xxx.config.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import org.springframework.beans.factory.annotation.Qualifier;
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.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
public class RedisConfig {
    @Value("${spring.redis.host}")
    private String HOSTNAME;

    @Value("${spring.redis.port}")
    private int PORT;

    @Value("${spring.redis.database}")
    private int DATABASE;

    @Value("${spring.redis.password}")
    private String PASSWORD;

    @Value("${spring.redis.timeout}")
    private long TIMEOUT;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(HOSTNAME);
        config.setPort(PORT);
        config.setDatabase(DATABASE);
        config.setPassword(PASSWORD);

        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(TIMEOUT))
                .build();

        return new LettuceConnectionFactory(config, clientConfig);
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    public RedisTemplate<String, byte[]> messagePackRedisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setEnableDefaultSerializer(false);

        return template;
    }

    @Bean
    public ObjectMapper messagePackObjectMapper() {
        return new ObjectMapper(new MessagePackFactory())
                .registerModule(new JavaTimeModule())
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    }

    /**
     *
     * RedisTemplate<String,Object>
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

application.yml

XML 复制代码
spring:
  redis:
    #数据库索引
    database: 0
    host: localhost
    port: 6379
    password: 你的密码
    #连接超时时间
    timeout: 5000

jackson配置

java 复制代码
package com.xxxx.config.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JsonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        return Jackson2ObjectMapperBuilder.json()
            .featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .modules(new JavaTimeModule())
            .build();
    }
}

二、详解

  1. 为什么单独配置messagePack?

为了在Redis中使用MessagePack进行高效序列化。

MessagePack是二进制序列化格式,比JSON:

  • 序列化/反序列化速度更快

  • 数据体积更小(节省30-50%空间)

  • 特别适合Redis这种内存数据库,节省内存

  1. Java8时间支持

.registerModule(new JavaTimeModule())

让ObjectMapper能正确处理LocalDateTime、LocalDate等Java 8时间类型

否则序列化时会抛异常

  1. 为什么单独json的禁用特性

.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)

日期不序列化为时间戳(如:1633046400000)

而是序列化为可读格式(如:"2021-10-01T00:00:00")

便于调试和跨语言兼容

相关推荐
skiy1 小时前
Spring Framework 中文官方文档
java·后端·spring
xifangge20252 小时前
【故障排查】IDEA 打开 Java 文件没有运行按钮(Run)?深度解析项目标识与环境配置的 3 大底层坑点
java·ide·intellij-idea
麻辣璐璐2 小时前
EditText属性运用之适配RTL语言和LTR语言的输入习惯
android·xml·java·开发语言·安卓
weisian1512 小时前
Java并发编程--33-Redis分布式缓存三大核心架构:主从、哨兵、分片,落地实战与选型
java·redis·缓存·主从架构·哨兵架构·分片架构
APIshop2 小时前
Python 爬虫获取京东商品详情 API 接口实战指南
java·服务器·数据库
wang09072 小时前
Linux性能优化之内存管理基础知识
java·linux·性能优化
StackNoOverflow2 小时前
SpringCloud的声明式服务调用 Feign 全面解析
后端·spring·spring cloud
范什么特西2 小时前
idea创建一个普通的Maven项目运行javaweb
java·maven·intellij-idea
好家伙VCC2 小时前
# 发散创新:用 Rust实现高性能物理引擎的底层架构设计与实战在游戏开发、虚拟仿真和机器人控
java·开发语言·python·rust·机器人