springboot中默认的redis配置是只能对单个redis库进行操作的。
那么我们需要多个库操作的时候这个时候就可以采用redis多数据源。
本代码参考RedisAutoConfiguration源码进行整改成的多数据源配置
注意:数据源2就是数据源1copy了一份代码,把所有的gisredis改成appredis,并去除数据源2的@Primary即可。如果还需要增加多个数据源,以此类推
配置redis数据源1:
1 package com.aaaa.config.redis; 2 3 import java.net.UnknownHostException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.springframework.beans.factory.annotation.Qualifier; 8 import org.springframework.boot.autoconfigure.data.redis.RedisProperties; 9 import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster; 10 import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel; 11 import org.springframework.boot.context.properties.ConfigurationProperties; 12 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Configuration; 14 import org.springframework.context.annotation.Primary; 15 import org.springframework.data.redis.connection.RedisClusterConfiguration; 16 import org.springframework.data.redis.connection.RedisConnectionFactory; 17 import org.springframework.data.redis.connection.RedisNode; 18 import org.springframework.data.redis.connection.RedisSentinelConfiguration; 19 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 20 import org.springframework.data.redis.core.StringRedisTemplate; 21 import org.springframework.util.Assert; 22 import org.springframework.util.StringUtils; 23 24 import redis.clients.jedis.JedisPoolConfig; 25 26 /** 27 * 28 * @desc Redis配置 29 * @author josnow 30 * @date 2018年1月17日 上午10:51:36 31 * @version 1.0.0 32 */ 33 @Configuration 34 public class RedisConfiggisredis { 35 36 @Bean(name = "gisredisRedisProperties") 37 @ConfigurationProperties(prefix = "spring.redis.gisredis") 38 @Primary 39 public RedisProperties gisredisRedisProperties() { 40 RedisProperties redisProperties = new RedisProperties(); 41 return redisProperties; 42 } 43 44 // //@Bean("gisredisRedisTemplate") 45 // public RedisTemplate<Object, Object> redisTemplate(@Qualifier("gisredisRedisConnectionFactory")RedisConnectionFactory 46 // redisConnectionFactory) 47 // throws UnknownHostException { 48 // RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); 49 // template.setConnectionFactory(redisConnectionFactory); 50 // return template; 51 // } 52 53 @Bean("gisredisStringRedisTemplate") 54 @Primary 55 public StringRedisTemplate stringRedisTemplate( 56 @Qualifier("gisredisRedisConnectionFactory")RedisConnectionFactory redisConnectionFactory) 57 throws UnknownHostException { 58 StringRedisTemplate template = new StringRedisTemplate(); 59 template.setConnectionFactory(redisConnectionFactory); 60 return template; 61 } 62 63 @Bean("gisredisRedisConnectionFactory") 64 @Primary 65 public RedisConnectionFactory redisConnectionFactory( 66 @Qualifier("gisredisRedisProperties") RedisProperties redisProperties) throws UnknownHostException { 67 68 return setRedisConnectionFactory(redisProperties, null, null); 69 } 70 71 private JedisConnectionFactory setRedisConnectionFactory(RedisProperties redisProperties, 72 RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration) 73 throws UnknownHostException { 74 return applyProperties( 75 createJedisConnectionFactory(redisProperties, sentinelConfiguration, clusterConfiguration), 76 redisProperties); 77 } 78 79 private final JedisConnectionFactory applyProperties(JedisConnectionFactory factory, 80 RedisProperties redisProperties) { 81 factory.setHostName(redisProperties.getHost()); 82 factory.setPort(redisProperties.getPort()); 83 if (redisProperties.getPassword() != null) { 84 factory.setPassword(redisProperties.getPassword()); 85 } 86 factory.setDatabase(redisProperties.getDatabase()); 87 if (redisProperties.getTimeout() > 0) { 88 factory.setTimeout(redisProperties.getTimeout()); 89 } 90 return factory; 91 } 92 93 private final RedisSentinelConfiguration getSentinelConfig(RedisProperties redisProperties, 94 RedisSentinelConfiguration sentinelConfiguration) { 95 if (sentinelConfiguration != null) { 96 return sentinelConfiguration; 97 } 98 Sentinel sentinelProperties = redisProperties.getSentinel(); 99 if (sentinelProperties != null) { 100 RedisSentinelConfiguration config = new RedisSentinelConfiguration(); 101 config.master(sentinelProperties.getMaster()); 102 config.setSentinels(createSentinels(sentinelProperties)); 103 return config; 104 } 105 return null; 106 } 107 108 /** 109 * Create a {@link RedisClusterConfiguration} if necessary. 110 * 111 * @return {@literal null} if no cluster settings are set. 112 */ 113 private final RedisClusterConfiguration getClusterConfiguration(RedisProperties redisProperties, 114 RedisClusterConfiguration clusterConfiguration) { 115 if (clusterConfiguration != null) { 116 return clusterConfiguration; 117 } 118 if (redisProperties.getCluster() == null) { 119 return null; 120 } 121 Cluster clusterProperties = redisProperties.getCluster(); 122 RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes()); 123 124 if (clusterProperties.getMaxRedirects() != null) { 125 config.setMaxRedirects(clusterProperties.getMaxRedirects()); 126 } 127 return config; 128 } 129 130 private List<RedisNode> createSentinels(Sentinel sentinel) { 131 List<RedisNode> nodes = new ArrayList<RedisNode>(); 132 for (String node : StringUtils.commaDelimitedListToStringArray(sentinel.getNodes())) { 133 try { 134 String[] parts = StringUtils.split(node, ":"); 135 Assert.state(parts.length == 2, "Must be defined as 'host:port'"); 136 nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1]))); 137 } catch (RuntimeException ex) { 138 throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex); 139 } 140 } 141 return nodes; 142 } 143 144 private JedisConnectionFactory createJedisConnectionFactory(RedisProperties redisProperties, 145 RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration) { 146 JedisPoolConfig poolConfig = redisProperties.getPool() != null ? jedisPoolConfig(redisProperties) 147 : new JedisPoolConfig(); 148 149 if (getSentinelConfig(redisProperties, sentinelConfiguration) != null) { 150 return new JedisConnectionFactory(getSentinelConfig(redisProperties, sentinelConfiguration), poolConfig); 151 } 152 if (getClusterConfiguration(redisProperties, clusterConfiguration) != null) { 153 return new JedisConnectionFactory(getClusterConfiguration(redisProperties, clusterConfiguration), 154 poolConfig); 155 } 156 return new JedisConnectionFactory(poolConfig); 157 } 158 159 private JedisPoolConfig jedisPoolConfig(RedisProperties redisProperties) { 160 JedisPoolConfig config = new JedisPoolConfig(); 161 RedisProperties.Pool props = redisProperties.getPool(); 162 config.setMaxTotal(props.getMaxActive()); 163 config.setMaxIdle(props.getMaxIdle()); 164 config.setMinIdle(props.getMinIdle()); 165 config.setMaxWaitMillis(props.getMaxWait()); 166 return config; 167 } 168 169 }
配置redis数据源2:
1 package com.aaaa.config.redis; 2 3 import java.net.UnknownHostException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.springframework.beans.factory.annotation.Qualifier; 8 import org.springframework.boot.autoconfigure.data.redis.RedisProperties; 9 import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster; 10 import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel; 11 import org.springframework.boot.context.properties.ConfigurationProperties; 12 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Configuration; 14 import org.springframework.data.redis.connection.RedisClusterConfiguration; 15 import org.springframework.data.redis.connection.RedisConnectionFactory; 16 import org.springframework.data.redis.connection.RedisNode; 17 import org.springframework.data.redis.connection.RedisSentinelConfiguration; 18 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 19 import org.springframework.data.redis.core.StringRedisTemplate; 20 import org.springframework.util.Assert; 21 import org.springframework.util.StringUtils; 22 23 import redis.clients.jedis.JedisPoolConfig; 24 25 /** 26 * 27 * @desc Redis配置 28 * @author josnow 29 * @date 2018年1月17日 上午10:51:36 30 * @version 1.0.0 31 */ 32 @Configuration 33 public class RedisConfigappredis { 34 35 @Bean(name = "appredisRedisProperties") 36 @ConfigurationProperties(prefix = "spring.redis.appredis") 37 public RedisProperties appredisRedisProperties() { 38 RedisProperties redisProperties = new RedisProperties(); 39 return redisProperties; 40 } 41 42 // //@Bean("appredisRedisTemplate") 43 // public RedisTemplate<Object, Object> redisTemplate(@Qualifier("appredisRedisConnectionFactory")RedisConnectionFactory 44 // redisConnectionFactory) 45 // throws UnknownHostException { 46 // RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); 47 // template.setConnectionFactory(redisConnectionFactory); 48 // return template; 49 // } 50 51 @Bean("appredisStringRedisTemplate") 52 public StringRedisTemplate stringRedisTemplate( 53 @Qualifier("appredisRedisConnectionFactory") RedisConnectionFactory redisConnectionFactory) 54 throws UnknownHostException { 55 StringRedisTemplate template = new StringRedisTemplate(); 56 template.setConnectionFactory(redisConnectionFactory); 57 return template; 58 } 59 60 @Bean("appredisRedisConnectionFactory") 61 public RedisConnectionFactory redisConnectionFactory( 62 @Qualifier("appredisRedisProperties") RedisProperties redisProperties) throws UnknownHostException { 63 64 return setRedisConnectionFactory(redisProperties, null, null); 65 } 66 67 private JedisConnectionFactory setRedisConnectionFactory(RedisProperties redisProperties, 68 RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration) 69 throws UnknownHostException { 70 return applyProperties( 71 createJedisConnectionFactory(redisProperties, sentinelConfiguration, clusterConfiguration), 72 redisProperties); 73 } 74 75 private final JedisConnectionFactory applyProperties(JedisConnectionFactory factory, 76 RedisProperties redisProperties) { 77 factory.setHostName(redisProperties.getHost()); 78 factory.setPort(redisProperties.getPort()); 79 if (redisProperties.getPassword() != null) { 80 factory.setPassword(redisProperties.getPassword()); 81 } 82 factory.setDatabase(redisProperties.getDatabase()); 83 if (redisProperties.getTimeout() > 0) { 84 factory.setTimeout(redisProperties.getTimeout()); 85 } 86 return factory; 87 } 88 89 private final RedisSentinelConfiguration getSentinelConfig(RedisProperties redisProperties, 90 RedisSentinelConfiguration sentinelConfiguration) { 91 if (sentinelConfiguration != null) { 92 return sentinelConfiguration; 93 } 94 Sentinel sentinelProperties = redisProperties.getSentinel(); 95 if (sentinelProperties != null) { 96 RedisSentinelConfiguration config = new RedisSentinelConfiguration(); 97 config.master(sentinelProperties.getMaster()); 98 config.setSentinels(createSentinels(sentinelProperties)); 99 return config; 100 } 101 return null; 102 } 103 104 /** 105 * Create a {@link RedisClusterConfiguration} if necessary. 106 * 107 * @return {@literal null} if no cluster settings are set. 108 */ 109 private final RedisClusterConfiguration getClusterConfiguration(RedisProperties redisProperties, 110 RedisClusterConfiguration clusterConfiguration) { 111 if (clusterConfiguration != null) { 112 return clusterConfiguration; 113 } 114 if (redisProperties.getCluster() == null) { 115 return null; 116 } 117 Cluster clusterProperties = redisProperties.getCluster(); 118 RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes()); 119 120 if (clusterProperties.getMaxRedirects() != null) { 121 config.setMaxRedirects(clusterProperties.getMaxRedirects()); 122 } 123 return config; 124 } 125 126 private List<RedisNode> createSentinels(Sentinel sentinel) { 127 List<RedisNode> nodes = new ArrayList<RedisNode>(); 128 for (String node : StringUtils.commaDelimitedListToStringArray(sentinel.getNodes())) { 129 try { 130 String[] parts = StringUtils.split(node, ":"); 131 Assert.state(parts.length == 2, "Must be defined as 'host:port'"); 132 nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1]))); 133 } catch (RuntimeException ex) { 134 throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex); 135 } 136 } 137 return nodes; 138 } 139 140 private JedisConnectionFactory createJedisConnectionFactory(RedisProperties redisProperties, 141 RedisSentinelConfiguration sentinelConfiguration, RedisClusterConfiguration clusterConfiguration) { 142 JedisPoolConfig poolConfig = redisProperties.getPool() != null ? jedisPoolConfig(redisProperties) 143 : new JedisPoolConfig(); 144 145 if (getSentinelConfig(redisProperties, sentinelConfiguration) != null) { 146 return new JedisConnectionFactory(getSentinelConfig(redisProperties, sentinelConfiguration), poolConfig); 147 } 148 if (getClusterConfiguration(redisProperties, clusterConfiguration) != null) { 149 return new JedisConnectionFactory(getClusterConfiguration(redisProperties, clusterConfiguration), 150 poolConfig); 151 } 152 return new JedisConnectionFactory(poolConfig); 153 } 154 155 private JedisPoolConfig jedisPoolConfig(RedisProperties redisProperties) { 156 JedisPoolConfig config = new JedisPoolConfig(); 157 RedisProperties.Pool props = redisProperties.getPool(); 158 config.setMaxTotal(props.getMaxActive()); 159 config.setMaxIdle(props.getMaxIdle()); 160 config.setMinIdle(props.getMinIdle()); 161 config.setMaxWaitMillis(props.getMaxWait()); 162 return config; 163 } 164 165 }
redis数据源1和数据源2的配置yml
spring:
1 redis: 2 gisredis: 3 host: 10.142.142.142 4 port: 6379 5 database: 6 6 testOnBorrow: true 7 pool: 8 max-active: 10000 9 max-idle: 1000 10 max-wait: 60000 11 min-idle: 0 12 appredis: 13 host: 10.142.141.84 14 port: 6379 15 database: 5 16 testOnBorrow: true 17 pool: 18 max-active: 10000 19 max-idle: 1000 20 max-wait: 60000 21 min-idle: 0
配置就这么多,要注意的是多个redis数据源必须指定一个默认的数据源。使用@Primary来指定,并且只能指定一个默认redis数据源。
注意:数据源2就是数据源1copy了一份代码,把所有的gisredis改成appredis,并去除数据源2的@Primary即可。如果还需要增加多个数据源,以此类推
最后在使用的时候
1 @Autowired 2 @Qualifier("appredisStringRedisTemplate") 3 private StringRedisTemplate appredisStringRedisTemplate;
即可得到指定的redis源,使用起来也是很方便
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springboot redis多数据源 - Python技术站