-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Enable Redis connection pool if commons-pool2 is on the classpath #26326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
d84e602
cc2f9e7
c5950ef
53beeee
b46a2e6
bce3f22
21a1461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright 2012-2020 the original author or authors. | ||
| * Copyright 2012-2021 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -24,9 +24,12 @@ | |
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.boot.autoconfigure.data.redis.RedisProperties.ClientType; | ||
| import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool; | ||
| import org.springframework.boot.context.properties.PropertyMapper; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.data.redis.connection.RedisClusterConfiguration; | ||
| import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| import org.springframework.data.redis.connection.RedisSentinelConfiguration; | ||
|
|
@@ -41,6 +44,7 @@ | |
| * | ||
| * @author Mark Paluch | ||
| * @author Stephane Nicoll | ||
| * @author Weix Sun | ||
| */ | ||
| @Configuration(proxyBeanMethods = false) | ||
| @ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class }) | ||
|
|
@@ -56,13 +60,13 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration { | |
|
|
||
| @Bean | ||
| JedisConnectionFactory redisConnectionFactory( | ||
| ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) { | ||
| return createJedisConnectionFactory(builderCustomizers); | ||
| ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers, Environment environment) { | ||
| return createJedisConnectionFactory(builderCustomizers, environment); | ||
| } | ||
|
|
||
| private JedisConnectionFactory createJedisConnectionFactory( | ||
| ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) { | ||
| JedisClientConfiguration clientConfiguration = getJedisClientConfiguration(builderCustomizers); | ||
| ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers, Environment environment) { | ||
| JedisClientConfiguration clientConfiguration = getJedisClientConfiguration(builderCustomizers, environment); | ||
| if (getSentinelConfig() != null) { | ||
| return new JedisConnectionFactory(getSentinelConfig(), clientConfiguration); | ||
| } | ||
|
|
@@ -73,12 +77,9 @@ private JedisConnectionFactory createJedisConnectionFactory( | |
| } | ||
|
|
||
| private JedisClientConfiguration getJedisClientConfiguration( | ||
| ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) { | ||
| ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers, Environment environment) { | ||
| JedisClientConfigurationBuilder builder = applyProperties(JedisClientConfiguration.builder()); | ||
| RedisProperties.Pool pool = getProperties().getJedis().getPool(); | ||
| if (pool != null) { | ||
| applyPooling(pool, builder); | ||
| } | ||
| createPoolingBuilder(builder, environment, getProperties().getJedis().getPool()); | ||
| if (StringUtils.hasText(getProperties().getUrl())) { | ||
| customizeConfigurationFromUrl(builder); | ||
| } | ||
|
|
@@ -95,6 +96,19 @@ private JedisClientConfigurationBuilder applyProperties(JedisClientConfiguration | |
| return builder; | ||
| } | ||
|
|
||
| private void createPoolingBuilder(JedisClientConfigurationBuilder builder, Environment environment, Pool pool) { | ||
| final boolean poolEnabled = environment.getProperty("spring.redis.jedis.pool.enabled", Boolean.class, true); | ||
weixsun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (poolEnabled) { | ||
| applyPooling(pool, builder); | ||
| return; | ||
| } | ||
| final boolean isSentinelConfig = (getSentinelConfig() != null); | ||
| // Jedis Sentinel cannot operate without a pool. | ||
| if (isSentinelConfig) { | ||
| throw new RedisClientPoolingException(ClientType.JEDIS); | ||
|
||
| } | ||
| } | ||
|
|
||
| private void applyPooling(RedisProperties.Pool pool, | ||
| JedisClientConfiguration.JedisClientConfigurationBuilder builder) { | ||
| builder.usePooling().poolConfig(jedisPoolConfig(pool)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright 2012-2021 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.boot.autoconfigure.data.redis; | ||
|
|
||
| import org.springframework.boot.autoconfigure.data.redis.RedisProperties.ClientType; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| /** | ||
| * Exception thrown when a Redis client pooling failure. | ||
| * | ||
| * @author Weix Sun | ||
| */ | ||
| class RedisClientPoolingException extends RuntimeException { | ||
weixsun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| static final String JEDIS_SENTINEL_POOLING_EXPECTED_MESSAGE = "Jedis Sentinel cannot operate without a pool"; | ||
|
|
||
| static final String LETTUCE_LACK_COMMONSPOOL2_EXPECTED_MESSAGE = "Lettuce pool cannot enable if \"commons-pool2\" don't exists on the classpath"; | ||
|
|
||
| private final ClientType clientType; | ||
|
|
||
| RedisClientPoolingException(ClientType clientType) { | ||
| super(buildMessage(clientType, null)); | ||
| this.clientType = clientType; | ||
| } | ||
|
|
||
| RedisClientPoolingException(ClientType clientType, String message) { | ||
| super(buildMessage(clientType, message)); | ||
| this.clientType = clientType; | ||
| } | ||
|
|
||
| ClientType getClientType() { | ||
| return this.clientType; | ||
| } | ||
|
|
||
| private static String buildMessage(ClientType clientType, String message) { | ||
| if (StringUtils.hasText(message)) { | ||
| return message; | ||
| } | ||
| return getDefaultPoolingExceptionMessage(clientType); | ||
| } | ||
|
|
||
| private static String getDefaultPoolingExceptionMessage(ClientType clientType) { | ||
| if (ClientType.JEDIS.equals(clientType)) { | ||
| return JEDIS_SENTINEL_POOLING_EXPECTED_MESSAGE; | ||
| } | ||
| if (ClientType.LETTUCE.equals(clientType)) { | ||
| return LETTUCE_LACK_COMMONSPOOL2_EXPECTED_MESSAGE; | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2012-2021 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.boot.autoconfigure.data.redis; | ||
|
|
||
| import org.springframework.boot.autoconfigure.data.redis.RedisProperties.ClientType; | ||
| import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; | ||
| import org.springframework.boot.diagnostics.FailureAnalysis; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| /** | ||
| * A {@code FailureAnalyzer} that performs analysis of failures caused by a | ||
| * {@link RedisClientPoolingException}. | ||
| * | ||
| * @author Weix Sun | ||
| */ | ||
| class RedisClientPoolingFailureAnalyzer extends AbstractFailureAnalyzer<RedisClientPoolingException> { | ||
weixsun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| protected FailureAnalysis analyze(Throwable rootFailure, RedisClientPoolingException cause) { | ||
| return new FailureAnalysis(getDescription(cause.getClientType(), cause.getMessage()), | ||
| getAction(cause.getClientType(), cause.getMessage()), cause); | ||
| } | ||
|
|
||
| private String getDescription(ClientType clientType, String message) { | ||
| if (StringUtils.hasText(message)) { | ||
| StringBuilder detailsMessage = new StringBuilder(); | ||
| detailsMessage.append(String.format("%s failed to pooling. Details are %s.", clientType.name(), message)); | ||
| return detailsMessage.toString(); | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| private String getAction(ClientType clientType, String message) { | ||
| if (StringUtils.hasText(message)) { | ||
| if (isGetDefaultPoolingFailureAction(message)) { | ||
| return getDefaultPoolingFailureAction(clientType); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| private boolean isGetDefaultPoolingFailureAction(String message) { | ||
| return RedisClientPoolingException.JEDIS_SENTINEL_POOLING_EXPECTED_MESSAGE.equals(message) | ||
| || RedisClientPoolingException.LETTUCE_LACK_COMMONSPOOL2_EXPECTED_MESSAGE.equals(message); | ||
weixsun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private String getDefaultPoolingFailureAction(ClientType clientType) { | ||
| if (ClientType.JEDIS.equals(clientType)) { | ||
| return "Set spring.redis.jedis.pool.enabled=true instead of spring.redis.jedis.pool.enabled=false or delete this configuration item.(Default: true)"; | ||
| } | ||
| if (ClientType.LETTUCE.equals(clientType)) { | ||
| return "Add \"commons-pool2\" dependency to the classpath."; | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.