|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.data.redis; |
| 18 | + |
| 19 | +import java.net.UnknownHostException; |
| 20 | + |
| 21 | +import io.lettuce.core.RedisClient; |
| 22 | +import io.lettuce.core.cluster.RedisClusterClient; |
| 23 | +import io.lettuce.core.resource.ClientResources; |
| 24 | +import io.lettuce.core.resource.DefaultClientResources; |
| 25 | +import org.apache.commons.pool2.impl.GenericObjectPool; |
| 26 | +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.ObjectProvider; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.data.redis.connection.RedisClusterConfiguration; |
| 34 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 35 | +import org.springframework.data.redis.connection.RedisSentinelConfiguration; |
| 36 | +import org.springframework.data.redis.connection.lettuce.DefaultLettucePool; |
| 37 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 38 | +import org.springframework.util.StringUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * Redis connection configuration using Lettuce. |
| 42 | + * |
| 43 | + * @author Mark Paluch |
| 44 | + */ |
| 45 | +@Configuration |
| 46 | +@ConditionalOnClass({ GenericObjectPool.class, RedisClient.class, RedisClusterClient.class }) |
| 47 | +class LettuceConnectionConfiguration extends RedisConnectionConfiguration { |
| 48 | + |
| 49 | + private final RedisProperties properties; |
| 50 | + |
| 51 | + LettuceConnectionConfiguration(RedisProperties properties, |
| 52 | + ObjectProvider<RedisSentinelConfiguration> sentinelConfigurationProvider, |
| 53 | + ObjectProvider<RedisClusterConfiguration> clusterConfigurationProvider) { |
| 54 | + super(properties, sentinelConfigurationProvider, clusterConfigurationProvider); |
| 55 | + this.properties = properties; |
| 56 | + } |
| 57 | + |
| 58 | + @Bean(destroyMethod = "shutdown") |
| 59 | + @ConditionalOnMissingBean(ClientResources.class) |
| 60 | + public DefaultClientResources lettuceClientResources() { |
| 61 | + return DefaultClientResources.create(); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + @ConditionalOnMissingBean(RedisConnectionFactory.class) |
| 66 | + public LettuceConnectionFactory redisConnectionFactory( |
| 67 | + ClientResources clientResources) throws UnknownHostException { |
| 68 | + return applyProperties(createLettuceConnectionFactory(clientResources)); |
| 69 | + } |
| 70 | + |
| 71 | + private LettuceConnectionFactory applyProperties(LettuceConnectionFactory factory) { |
| 72 | + configureConnection(factory); |
| 73 | + if (this.properties.isSsl()) { |
| 74 | + factory.setUseSsl(true); |
| 75 | + } |
| 76 | + if (this.properties.getLettuce() != null) { |
| 77 | + RedisProperties.Lettuce lettuce = this.properties.getLettuce(); |
| 78 | + if (lettuce.getShutdownTimeout() >= 0) { |
| 79 | + factory.setShutdownTimeout( |
| 80 | + this.properties.getLettuce().getShutdownTimeout()); |
| 81 | + } |
| 82 | + } |
| 83 | + return factory; |
| 84 | + } |
| 85 | + |
| 86 | + private void configureConnection(LettuceConnectionFactory factory) { |
| 87 | + if (StringUtils.hasText(this.properties.getUrl())) { |
| 88 | + configureConnectionFromUrl(factory); |
| 89 | + } |
| 90 | + else { |
| 91 | + factory.setHostName(this.properties.getHost()); |
| 92 | + factory.setPort(this.properties.getPort()); |
| 93 | + if (this.properties.getPassword() != null) { |
| 94 | + factory.setPassword(this.properties.getPassword()); |
| 95 | + } |
| 96 | + factory.setDatabase(this.properties.getDatabase()); |
| 97 | + if (this.properties.getTimeout() > 0) { |
| 98 | + factory.setTimeout(this.properties.getTimeout()); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void configureConnectionFromUrl(LettuceConnectionFactory factory) { |
| 104 | + ConnectionInfo connectionInfo = parseUrl(this.properties.getUrl()); |
| 105 | + factory.setUseSsl(connectionInfo.isUseSsl()); |
| 106 | + factory.setHostName(connectionInfo.getHostName()); |
| 107 | + factory.setPort(connectionInfo.getPort()); |
| 108 | + if (connectionInfo.getPassword() != null) { |
| 109 | + factory.setPassword(connectionInfo.getPassword()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private DefaultLettucePool applyProperties(DefaultLettucePool pool) { |
| 114 | + if (StringUtils.hasText(this.properties.getUrl())) { |
| 115 | + configureConnectionFromUrl(pool); |
| 116 | + } |
| 117 | + else { |
| 118 | + pool.setHostName(this.properties.getHost()); |
| 119 | + pool.setPort(this.properties.getPort()); |
| 120 | + if (this.properties.getPassword() != null) { |
| 121 | + pool.setPassword(this.properties.getPassword()); |
| 122 | + } |
| 123 | + pool.setDatabase(this.properties.getDatabase()); |
| 124 | + } |
| 125 | + if (this.properties.getTimeout() > 0) { |
| 126 | + pool.setTimeout(this.properties.getTimeout()); |
| 127 | + } |
| 128 | + pool.afterPropertiesSet(); |
| 129 | + return pool; |
| 130 | + } |
| 131 | + |
| 132 | + private void configureConnectionFromUrl(DefaultLettucePool lettucePool) { |
| 133 | + ConnectionInfo connectionInfo = parseUrl(this.properties.getUrl()); |
| 134 | + lettucePool.setHostName(connectionInfo.getHostName()); |
| 135 | + lettucePool.setPort(connectionInfo.getPort()); |
| 136 | + if (connectionInfo.getPassword() != null) { |
| 137 | + lettucePool.setPassword(connectionInfo.getPassword()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private LettuceConnectionFactory createLettuceConnectionFactory( |
| 142 | + ClientResources clientResources) { |
| 143 | + |
| 144 | + if (getSentinelConfig() != null) { |
| 145 | + if (this.properties.getLettuce() != null |
| 146 | + && this.properties.getLettuce().getPool() != null) { |
| 147 | + DefaultLettucePool lettucePool = new DefaultLettucePool( |
| 148 | + getSentinelConfig()); |
| 149 | + return new LettuceConnectionFactory(applyProperties( |
| 150 | + applyClientResources(lettucePool, clientResources))); |
| 151 | + } |
| 152 | + return applyClientResources( |
| 153 | + new LettuceConnectionFactory(getSentinelConfig()), |
| 154 | + clientResources); |
| 155 | + } |
| 156 | + |
| 157 | + if (getClusterConfiguration() != null) { |
| 158 | + return applyClientResources( |
| 159 | + new LettuceConnectionFactory(getClusterConfiguration()), |
| 160 | + clientResources); |
| 161 | + } |
| 162 | + |
| 163 | + if (this.properties.getLettuce() != null |
| 164 | + && this.properties.getLettuce().getPool() != null) { |
| 165 | + GenericObjectPoolConfig config = lettucePoolConfig( |
| 166 | + this.properties.getLettuce().getPool()); |
| 167 | + DefaultLettucePool lettucePool = new DefaultLettucePool( |
| 168 | + this.properties.getHost(), this.properties.getPort(), config); |
| 169 | + return new LettuceConnectionFactory(applyProperties( |
| 170 | + applyClientResources(lettucePool, clientResources))); |
| 171 | + } |
| 172 | + |
| 173 | + return applyClientResources(new LettuceConnectionFactory(), clientResources); |
| 174 | + } |
| 175 | + |
| 176 | + private DefaultLettucePool applyClientResources(DefaultLettucePool lettucePool, |
| 177 | + ClientResources clientResources) { |
| 178 | + lettucePool.setClientResources(clientResources); |
| 179 | + return lettucePool; |
| 180 | + } |
| 181 | + |
| 182 | + private LettuceConnectionFactory applyClientResources( |
| 183 | + LettuceConnectionFactory factory, ClientResources clientResources) { |
| 184 | + factory.setClientResources(clientResources); |
| 185 | + return factory; |
| 186 | + } |
| 187 | + |
| 188 | + private GenericObjectPoolConfig lettucePoolConfig(RedisProperties.Pool props) { |
| 189 | + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); |
| 190 | + config.setMaxTotal(props.getMaxActive()); |
| 191 | + config.setMaxIdle(props.getMaxIdle()); |
| 192 | + config.setMinIdle(props.getMinIdle()); |
| 193 | + config.setMaxWaitMillis(props.getMaxWait()); |
| 194 | + return config; |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments