springboot连接多个redis

前言

我想不到,就这个问题还折腾了好一会儿

方法

yml配置文件

spring:
  application:
    name: multiredis
  redis:
    onedb:
      host: 192.168.2.1
      port: 6379
      password: 123456
      database: 10
      timeout: 5000
    threedb:
      host: 192.168.2.79
      port: 6379
      password: 654321
      database: 0
      timeout: 5000
    sixdb:
      host: 192.168.3.24
      port: 6379
      password: 123456
      database: 17
      timeout: 5000
server:
  port: 10556

创建

package com.example.multiredis;

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.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisTemplateConfig {
    //onedb
    @Value("${spring.redis.onedb.host}")
    private String oneHost;
    @Value("${spring.redis.onedb.port}")
    private Integer onePort;
    @Value("${spring.redis.onedb.password}")
    private String onePassword;
    @Value("${spring.redis.onedb.database}")
    private Integer oneDatabase;

    //threedb
    @Value("${spring.redis.threedb.host}")
    private String threeHost;
    @Value("${spring.redis.threedb.port}")
    private Integer threePort;
    @Value("${spring.redis.threedb.password}")
    private String threePassword;
    @Value("${spring.redis.threedb.database}")
    private Integer threeDataBase;

    //sixdb
    @Value("${spring.redis.sixdb.host}")
    private String sixHost;
    @Value("${spring.redis.sixdb.port}")
    private Integer sixPort;
    @Value("${spring.redis.sixdb.password}")
    private String sixPassword;
    @Value("${spring.redis.sixdb.database}")
    private Integer sixDatabase;

    private static final int MAX_IDLE = 200; //最大空闲连接数
    private static final int MAX_TOTAL = 1024; //最大连接数
    private static final long MAX_WAIT_MILLIS = 10000; //建立连接最长等待时间


    //配置工厂
    public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
                                                    int maxTotal, long maxWaitMillis, int index) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(port);

        if (!StringUtils.isEmpty(password)) {
            jedisConnectionFactory.setPassword(password);
        }

        if (index != 0) {
            jedisConnectionFactory.setDatabase(index);
        }

        jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    //连接池配置
    public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }


    @Bean(name = "redisTemplate1")
    public StringRedisTemplate redisTemplate1() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(
                connectionFactory(oneHost, onePort, onePassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, oneDatabase));
        return template;
    }

    @Bean(name = "redisTemplate3")
    public StringRedisTemplate redisTemplate3() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(
                connectionFactory(threeHost, threePort, threePassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, threeDataBase));
        return template;
    }

    @Bean(name = "redisTemplate6")
    public StringRedisTemplate redisTemplate6() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(
                connectionFactory(sixHost, sixPort, sixPassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, sixDatabase));
        return template;
    }
}

参考
注意:里面的代码有点过时了

使用

package com.example.multiredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController  //返回字符串
public class HelloController {
    final
    StringRedisTemplate oneRedis;
    final
    StringRedisTemplate threeRedis;
    final
    StringRedisTemplate sixRedis;

    public HelloController(@Qualifier("redisTemplate1")StringRedisTemplate oneRedis,
                           @Qualifier("redisTemplate3")StringRedisTemplate threeRedis,
                           @Qualifier("redisTemplate6")StringRedisTemplate sixRedis) {

        this.oneRedis = oneRedis;
        this.threeRedis = threeRedis;
        this.sixRedis = sixRedis;
    }


    @RequestMapping("/hello")
    public String getTest(){
        String t_value = threeRedis.opsForValue().get("861675040628601");
        return "helloworld:"+ t_value+"--"+Math.random();
    }

}

注意里面的 @Qualifier 注解。不然会报错的。

方式二
这种方式,在IDEA使用@Autowired,会报黄(但是能用)。因为spring最新版,推荐的注入方式是构造函数注入

    @Autowired
    @Resource(name = "redisTemplate1")
    StringRedisTemplate  oneRedis;
    @Autowired
    @Resource(name = "redisTemplate3")
    StringRedisTemplate threeRedis;
    @Autowired
    @Resource(name = "redisTemplate6")
    StringRedisTemplate sixRedis;

参考2

原生说明

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/doushou?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  data:
    mongodb:
      uri: mongodb://robin:123456@192.168.7.32/doushou
  redis:
    host: zhangsan.com
    port: 6379
    password: 222222
    database: 7
    timeout: 500

server:
  port: 12188

springboo 有默认的加载redis配置的方式,它自动化处理了,帮你隐藏了很多东西。
然后就写到唯一的里面了

@Autowired
    private RedisTemplate redisTemplate;

总结

受够了。感觉就是在纠结配置文件和语法糖。


版权声明:本文为lanxiaziyi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。