spring boot集成redis,以及配置database不生效问题

备注:记录一次spring-boot + redis 配置redis.database后,仍然使用db0默认库的情况。

springboot集成redis及相关问题

1. spring boot集成redis

redis集成依赖:

xml 复制代码
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- redis依赖commons-pool 这个依赖一定要添加 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>

redis配置

yaml 复制代码
spring:
  redis:
    host: xxxxx
    port: 6379
    password: xxxx
    database: 1
    jedis:
      pool:
        max-active: 10
        max-idle: 10
        min-idle: 0

序列化

java 复制代码
@Configuration
public class RedisConfig {
    /**
     * @Author: Huey
     * @Date: 2024/1/12 18:24
     * @Params:
     * @Return:
     * @Description: 初始化设置redis序列化类型,否则会出现乱码
     **/
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //使用fastjson序列化
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);

        return template;
    }
}	

完成以上配置,即redis已经正常集成进入项目。

2. 集成redission redis分布式锁等快捷管理工具

集成依赖

xml 复制代码
  <!-- redis便捷分布式锁管理 -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>${redisson.version}</version>
        </dependency>

配置注入

java 复制代码
@Configuration
public class RedissionConfig {
    @Value("${spring.redis.host}")
    private String redisHost;

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

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

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

    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        config.useSingleServer().
                setAddress("redis://" + redisHost + ":" + port)
                .setPassword(password)
        config.setCodec(new JsonJacksonCodec());
        return Redisson.create(config);
    }
}

示例

完成以上配置,即可正常使用了

java 复制代码
@Autowired
private RedissonClient redissonClient;

 RLock rLock = redissonClient.getLock(lockName);
 if (rLock.isLocked()) {
     //当前锁正在使用,当前用户还在同步订单,不处理业务
     logger.info("method handleReceiptMsg重复加锁,请求入参:{},不处理业务,当前锁状态:{}", JSONObject.toJSONString(payRqst), rLock.isLocked());
     return Result.error("handleReceiptMsg重复加锁");
 }
boolean isLocked = rLock.tryLock(RedisLockNameConstants.ORDER_SYNC_LOCK_TIME, TimeUnit.SECONDS);

3. 配置redis database不生效

如果是单独仅集成redis,database 配置是肯定生效的,因为没有影响,这里记录一个遇到的情景:

即:当spring-boot集成redis后,同时集成redission使用redis锁相关管理插件,此时,配置了RedissonClient,注入了Config配置,即第二部中的代码部分。而第一步中集成redis时,使用redistemplate仅仅只是对其做了序列化,至于redistemplate加载的配置,全靠程序默认加载,那么这里就涉及了一个加载顺序问题。

springboot 的 @Configuration 也会默认加载redis的配置,步骤二中构建RedissonClient时候,也构建了Config对象,这个里面会覆盖掉RedisConfig里的配置,所以,在构建RedissonClient时候,设置database即可生效。

java 复制代码
 @Bean
 public RedissonClient getRedisson() {
      Config config = new Config();
      config.useSingleServer().
              setAddress("redis://" + redisHost + ":" + port)
              .setPassword(password)
              //指定redis db库
              .setDatabase(database);
      config.setCodec(new JsonJacksonCodec());
      return Redisson.create(config);
  }
相关推荐
还是做不到嘛\.35 分钟前
Dvwa靶场-SQL Injection (Blind)-基于sqlmap
数据库·sql·web安全
不写八个1 小时前
PHP教程004:php链接mysql数据库
数据库·mysql·php
Dylan~~~1 小时前
深度解析Cassandra:分布式数据库的王者之路
数据库·分布式
计算机学姐2 小时前
基于SpringBoot的咖啡店管理系统【个性化推荐+数据可视化统计+配送信息】
java·vue.js·spring boot·后端·mysql·信息可视化·tomcat
荒川之神2 小时前
Oracle HR 模式递归函数练习(基于 employees 表)
数据库·oracle
My的梦想已实现2 小时前
关于JAVA Springboot集成支付后打包JAR之后报安全错误的处理
java·spring boot·jar
小陈工2 小时前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle
小江的记录本3 小时前
【注解】常见 Java 注解系统性知识体系总结(附《全方位对比表》+ 思维导图)
java·前端·spring boot·后端·spring·mybatis·web
杨云龙UP3 小时前
Linux生产环境下Oracle RMAN 备份、核查、清理与验证常用命令整理_20260330
linux·运维·服务器·数据库·oracle
橙子家3 小时前
关于列式存储(Column-base Storage)的几个要点解读
数据库