SpringBoot整合Redis动态切换Redis库

SpringBoot动态切换Redis库
摘要由CSDN通过智能技术生成

本文讲解如何实现Redis库的动态切换

application.yml配置

# Tomcat 
server: 
  tomcat: 
    uri-encoding: UTF-8 
    max-threads: 1000 
    min-spare-threads: 30 
  port: 8088 
  servlet: 
    context-path: /redis-demo 
 
# mysql 
spring: 
  http: 
    multipart: 
      max-file-size: 100MB 
      max-request-size: 100MB 
      enabled: true 
  redis: 
    database: 0 
    host: localhost 
    port: 6379 
    password:    # 密码(默认为空) 
    timeout: 6000ms  # 连接超时时长(毫秒) 
    jedis: 
      pool: 
        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制) 
        max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制) 
        max-idle: 10      # 连接池中的最大空闲连接 
        min-idle: 5       # 连接池中的最小空闲连接 
  cache: 
    type: none 
 
xxx: 
  redis: 
    open: false  # 是否开启redis缓存  true开启   false关闭 
 
logging: 
  level: debug 
  level.com.example: debug 
  path: logs/ 
  file: xxx.log

创建自定义注解 RedisSelect

import java.lang.annotation.*; 
 
@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface RedisSelect { 
    /** 
     * redis库 
     * @return 
     */ 
    int value() default 0; 
}

Redis配置类 RedisConfig (下面的注解即可直接回去yml中的redis配置)

import com.example.redis.SelectableRedisTemplate; 
import org.springframework.boot.autoconfigure.AutoConfigureAfter; 
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; 
import org.springframework.boot.autoconfigure.data.redis.RedisProperties; 
import org.springframework.boot.context.properties.EnableConfigurationProperties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Primary; 
import org.springframework.data.redis.connection.RedisPassword; 
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; 
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; 
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 
import redis.clients.jedis.JedisPoolConfig; 
 
import java.time.Duration; 
 
/** 
 * Redis配置相关 
 */ 
@Configuration 
@AutoConfigureAfter(RedisAutoConfiguration.class)// 自动获取application.yml中的配置 
@EnableConfigurationProperties(RedisProperties.class) 
public class RedisConfig { 
    private
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值