Enable Redis connection pool if commons-pool2 is available

See gh-26326
pull/26903/head
weixsun 4 years ago committed by Stephane Nicoll
parent a72da74cdc
commit 55ff163018

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
* *
* @author Mark Paluch * @author Mark Paluch
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Weix Sun
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class }) @ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
@ -76,7 +77,7 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) { ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) {
JedisClientConfigurationBuilder builder = applyProperties(JedisClientConfiguration.builder()); JedisClientConfigurationBuilder builder = applyProperties(JedisClientConfiguration.builder());
RedisProperties.Pool pool = getProperties().getJedis().getPool(); RedisProperties.Pool pool = getProperties().getJedis().getPool();
if (pool != null) { if (pool.isEnabled()) {
applyPooling(pool, builder); applyPooling(pool, builder);
} }
if (StringUtils.hasText(getProperties().getUrl())) { if (StringUtils.hasText(getProperties().getUrl())) {

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -51,6 +51,7 @@ import org.springframework.util.StringUtils;
* *
* @author Mark Paluch * @author Mark Paluch
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Weix Sun
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisClient.class) @ConditionalOnClass(RedisClient.class)
@ -104,10 +105,10 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
} }
private LettuceClientConfigurationBuilder createBuilder(Pool pool) { private LettuceClientConfigurationBuilder createBuilder(Pool pool) {
if (pool == null) { if (pool.isEnabled()) {
return LettuceClientConfiguration.builder(); return new PoolBuilderFactory().createBuilder(pool);
} }
return new PoolBuilderFactory().createBuilder(pool); return LettuceClientConfiguration.builder();
} }
private LettuceClientConfigurationBuilder applyProperties( private LettuceClientConfigurationBuilder applyProperties(

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -233,6 +233,8 @@ public class RedisProperties {
*/ */
public static class Pool { public static class Pool {
private boolean enabled;
/** /**
* Maximum number of "idle" connections in the pool. Use a negative value to * Maximum number of "idle" connections in the pool. Use a negative value to
* indicate an unlimited number of idle connections. * indicate an unlimited number of idle connections.
@ -265,6 +267,14 @@ public class RedisProperties {
*/ */
private Duration timeBetweenEvictionRuns; private Duration timeBetweenEvictionRuns;
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public int getMaxIdle() { public int getMaxIdle() {
return this.maxIdle; return this.maxIdle;
} }
@ -396,14 +406,16 @@ public class RedisProperties {
/** /**
* Jedis pool configuration. * Jedis pool configuration.
*/ */
private Pool pool; private final Pool pool;
public Pool getPool() { public Jedis() {
return this.pool; Pool pool = new Pool();
pool.setEnabled(true);
this.pool = pool;
} }
public void setPool(Pool pool) { public Pool getPool() {
this.pool = pool; return this.pool;
} }
} }
@ -421,7 +433,7 @@ public class RedisProperties {
/** /**
* Lettuce pool configuration. * Lettuce pool configuration.
*/ */
private Pool pool; private final Pool pool = new Pool();
private final Cluster cluster = new Cluster(); private final Cluster cluster = new Cluster();
@ -437,10 +449,6 @@ public class RedisProperties {
return this.pool; return this.pool;
} }
public void setPool(Pool pool) {
this.pool = pool;
}
public Cluster getCluster() { public Cluster getCluster() {
return this.cluster; return this.cluster;
} }

@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Mark Paluch * @author Mark Paluch
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Weix Sun
*/ */
@ClassPathExclusions("lettuce-core-*.jar") @ClassPathExclusions("lettuce-core-*.jar")
class RedisAutoConfigurationJedisTests { class RedisAutoConfigurationJedisTests {
@ -142,6 +143,16 @@ class RedisAutoConfigurationJedisTests {
}); });
} }
@Test
void testRedisConfigurationDisabledPool() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.jedis.pool.enabled:false")
.run((context) -> {
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getClientConfiguration().isUsePooling()).isEqualTo(false);
});
}
@Test @Test
void testRedisConfigurationWithTimeoutAndConnectTimeout() { void testRedisConfigurationWithTimeoutAndConnectTimeout() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250", this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250",

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,6 +65,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Alen Turkovic * @author Alen Turkovic
* @author Scott Frederick * @author Scott Frederick
* @author Weix Sun
*/ */
class RedisAutoConfigurationTests { class RedisAutoConfigurationTests {
@ -155,10 +156,25 @@ class RedisAutoConfigurationTests {
} }
@Test @Test
void testRedisConfigurationWithPool() { void testRedisConfigurationWithPoolUsingDefaultValue() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.min-idle:1", this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:true")
"spring.redis.lettuce.pool.max-idle:4", "spring.redis.lettuce.pool.max-active:16", .run((context) -> {
"spring.redis.lettuce.pool.max-wait:2000", "spring.redis.lettuce.pool.time-between-eviction-runs:30000", LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
GenericObjectPoolConfig<?> poolConfig = getPoolingClientConfiguration(cf).getPoolConfig();
assertThat(poolConfig.getMinIdle()).isEqualTo(0);
assertThat(poolConfig.getMaxIdle()).isEqualTo(8);
assertThat(poolConfig.getMaxTotal()).isEqualTo(8);
assertThat(poolConfig.getMaxWaitMillis()).isEqualTo(-1);
});
}
@Test
void testRedisConfigurationWithPoolUsingCustomValue() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:true",
"spring.redis.lettuce.pool.min-idle:1", "spring.redis.lettuce.pool.max-idle:4",
"spring.redis.lettuce.pool.max-active:16", "spring.redis.lettuce.pool.max-wait:2000",
"spring.redis.lettuce.pool.time-between-eviction-runs:30000",
"spring.redis.lettuce.shutdown-timeout:1000").run((context) -> { "spring.redis.lettuce.shutdown-timeout:1000").run((context) -> {
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class); LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo"); assertThat(cf.getHostName()).isEqualTo("foo");
@ -172,6 +188,17 @@ class RedisAutoConfigurationTests {
}); });
} }
@Test
void testRedisConfigurationDisabledPool() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:false")
.run((context) -> {
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(ReflectionTestUtils.getField(cf, "clientConfiguration"))
.isNotInstanceOf(LettucePoolingClientConfiguration.class);
});
}
@Test @Test
void testRedisConfigurationWithTimeoutAndConnectTimeout() { void testRedisConfigurationWithTimeoutAndConnectTimeout() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250", this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250",

Loading…
Cancel
Save