此处提供了一个SpringBoot starter插件

客户端集成

1.在pom.xml中添加如下依赖:

<dependency>
    <groupId>com.mk</groupId>
    <artifactId>spring-boot-starter-dynamic-redis</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2.在yml配置文件中添加如下配置:

dms:
  dynamic:
    redis:
      enabled: true
      connection:
        # 第一个Redis连接
        demo1Redis:
          host: 127.0.0.1
          port: 6379
          database: 1
          timeout: 3000
          jedis:
            pool:
              max-wait: 3000
              max-active: 100
              max-idle: 20
              min-idle: 0
        # 第二个Redis连接
        demo2Redis:
          host: 127.0.0.1
          port: 6379
          database: 2
          timeout: 3000
          jedis:
            pool:
              max-wait: 3000
              max-active: 100
              max-idle: 20
              min-idle: 0

3.在启动类上面添加@EnableDynamicRedis注解,如下:

@EnableDynamicRedis
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

4.创建Redis配置类,如下:

@Configuration
public class RedisConfig {

    @Resource
    private DynamicRedisProvider dynamicRedisProvider;

    @Bean(name = "demo1Redis")
    public RedisTemplate demo1Redis() {
         return new StringRedisTemplate(dynamicRedisProvider.loadRedis().get("demo1Redis"));
    }

    @Bean(name = "demo2Redis")
    public RedisTemplate demo2Redis() {
        return new StringRedisTemplate(dynamicRedisProvider.loadRedis().get("demo2Redis"));
    }
}

5.测试

@RestController
public class TestController {
    @Resource(name = "demo1Redis")
    private StringRedisTemplate demo1Template;

    @Resource(name = "demo2Redis")
    private StringRedisTemplate demo2Template;

    @GetMapping("/testRedis")
    public String testRedis(){
        demo1Template.opsForValue().set("testKey", "我存放到Redis下db为1的库");
        demo2Template.opsForValue().set("testKey", "我存放到Redis下db为2的库");
        return "success";
    }
}

6、在浏览器访问/testRedis接口,通过Redis客户端查看数据如下:

redis-starter1.png
redis-starter2.png