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

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

1. spring boot集成redis

  • redis集成依赖:

        <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配置

    spring:
      redis:
        host: xxxxx
        port: 6379
        password: xxxx
        database: 1
        jedis:
          pool:
            max-active: 10
            max-idle: 10
            min-idle: 0
    
  • 序列化

    @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分布式锁等快捷管理工具

  • 集成依赖

      <!-- redis便捷分布式锁管理 -->
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>${redisson.version}</version>
            </dependency>
    
  • 配置注入

    @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);
        }
    }
    
  • 示例

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

    @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即可生效。

 @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);
  }
  • 24
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值