1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
package org.springframework.boot.autoconfigure.data.redis;
import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import org.apache.commons.pool2.impl.GenericObjectPool; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster; import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool; import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnection; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPoolConfig;
@Configuration @ConditionalOnClass({JedisConnection.class, RedisOperations.class, Jedis.class}) @EnableConfigurationProperties({RedisProperties.class}) public class RedisAutoConfiguration { public RedisAutoConfiguration() { }
@Configuration protected static class RedisConfiguration { protected RedisConfiguration() { }
@Bean @ConditionalOnMissingBean( name = {"redisTemplate"} ) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); retu template; }
@Bean @ConditionalOnMissingBean({StringRedisTemplate.class}) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); retu template; } }
@Configuration @ConditionalOnClass({GenericObjectPool.class}) protected static class RedisConnectionConfiguration { private final RedisProperties properties; private final RedisSentinelConfiguration sentinelConfiguration; private final RedisClusterConfiguration clusterConfiguration;
public RedisConnectionConfiguration(RedisProperties properties, ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration, ObjectProvider<RedisClusterConfiguration> clusterConfiguration) { this.properties = properties; this.sentinelConfiguration = (RedisSentinelConfiguration)sentinelConfiguration.getIfAvailable(); this.clusterConfiguration = (RedisClusterConfiguration)clusterConfiguration.getIfAvailable(); }
@Bean @ConditionalOnMissingBean({RedisConnectionFactory.class}) public JedisConnectionFactory redisConnectionFactory() throws UnknownHostException { retu this.applyProperties(this.createJedisConnectionFactory()); }
protected final JedisConnectionFactory applyProperties(JedisConnectionFactory factory) { this.configureConnection(factory); if (this.properties.isSsl()) { factory.setUseSsl(true); }
factory.setDatabase(this.properties.getDatabase()); if (this.properties.getTimeout() > 0) { factory.setTimeout(this.properties.getTimeout()); }
retu factory; }
private void configureConnection(JedisConnectionFactory factory) { if (StringUtils.hasText(this.properties.getUrl())) { this.configureConnectionFromUrl(factory); } else { factory.setHostName(this.properties.getHost()); factory.setPort(this.properties.getPort()); if (this.properties.getPassword() != null) { factory.setPassword(this.properties.getPassword()); } }
}
private void configureConnectionFromUrl(JedisConnectionFactory factory) { String url = this.properties.getUrl(); if (url.startsWith("rediss://")) { factory.setUseSsl(true); }
try { URI uri = new URI(url); factory.setHostName(uri.getHost()); factory.setPort(uri.getPort()); if (uri.getUserInfo() != null) { String password = uri.getUserInfo(); int index = password.lastIndexOf(":"); if (index >= 0) { password = password.substring(index + 1); }
factory.setPassword(password); }
} catch (URISyntaxException var6) { throw new IllegalArgumentException("Malformed 'spring.redis.url' " + url, var6); } }
protected final RedisSentinelConfiguration getSentinelConfig() { if (this.sentinelConfiguration != null) { retu this.sentinelConfiguration; } else { Sentinel sentinelProperties = this.properties.getSentinel(); if (sentinelProperties != null) { RedisSentinelConfiguration config = new RedisSentinelConfiguration(); config.master(sentinelProperties.getMaster()); config.setSentinels(this.createSentinels(sentinelProperties)); retu config; } else { retu null; } } }
protected final RedisClusterConfiguration getClusterConfiguration() { if (this.clusterConfiguration != null) { retu this.clusterConfiguration; } else if (this.properties.getCluster() == null) { retu null; } else { Cluster clusterProperties = this.properties.getCluster(); RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes()); if (clusterProperties.getMaxRedirects() != null) { config.setMaxRedirects(clusterProperties.getMaxRedirects().intValue()); }
retu config; } }
private List<RedisNode> createSentinels(Sentinel sentinel) { List<RedisNode> nodes = new ArrayList(); String[] var3 = StringUtils.commaDelimitedListToStringArray(sentinel.getNodes()); int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) { String node = var3[var5];
try { String[] parts = StringUtils.split(node, ":"); Assert.state(parts.length == 2, "Must be defined as 'host:port'"); nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1]).intValue())); } catch (RuntimeException var8) { throw new IllegalStateException("Invalid redis sentinel property '" + node + "'", var8); } }
retu nodes; }
private JedisConnectionFactory createJedisConnectionFactory() { JedisPoolConfig poolConfig = this.properties.getPool() != null ? this.jedisPoolConfig() : new JedisPoolConfig(); if (this.getSentinelConfig() != null) { retu new JedisConnectionFactory(this.getSentinelConfig(), poolConfig); } else { retu this.getClusterConfiguration() != null ? new JedisConnectionFactory(this.getClusterConfiguration(), poolConfig) : new JedisConnectionFactory(poolConfig); } }
private JedisPoolConfig jedisPoolConfig() { JedisPoolConfig config = new JedisPoolConfig(); Pool props = this.properties.getPool(); config.setMaxTotal(props.getMaxActive()); config.setMaxIdle(props.getMaxIdle()); config.setMinIdle(props.getMinIdle()); config.setMaxWaitMillis((long)props.getMaxWait()); retu config; } } }
|