Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
55ff1630
Commit
55ff1630
authored
Apr 30, 2021
by
weixsun
Committed by
Stephane Nicoll
Jun 14, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable Redis connection pool if commons-pool2 is available
See gh-26326
parent
a72da74c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
71 additions
and
22 deletions
+71
-22
JedisConnectionConfiguration.java
...utoconfigure/data/redis/JedisConnectionConfiguration.java
+3
-2
LettuceConnectionConfiguration.java
...oconfigure/data/redis/LettuceConnectionConfiguration.java
+5
-4
RedisProperties.java
...mework/boot/autoconfigure/data/redis/RedisProperties.java
+19
-11
RedisAutoConfigurationJedisTests.java
...onfigure/data/redis/RedisAutoConfigurationJedisTests.java
+11
-0
RedisAutoConfigurationTests.java
...autoconfigure/data/redis/RedisAutoConfigurationTests.java
+32
-5
application.properties
...test-data-redis/src/main/resources/application.properties
+1
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java
View file @
55ff1630
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
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.
...
...
@@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
*
* @author Mark Paluch
* @author Stephane Nicoll
* @author Weix Sun
*/
@Configuration
(
proxyBeanMethods
=
false
)
@ConditionalOnClass
({
GenericObjectPool
.
class
,
JedisConnection
.
class
,
Jedis
.
class
})
...
...
@@ -76,7 +77,7 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
ObjectProvider
<
JedisClientConfigurationBuilderCustomizer
>
builderCustomizers
)
{
JedisClientConfigurationBuilder
builder
=
applyProperties
(
JedisClientConfiguration
.
builder
());
RedisProperties
.
Pool
pool
=
getProperties
().
getJedis
().
getPool
();
if
(
pool
!=
null
)
{
if
(
pool
.
isEnabled
()
)
{
applyPooling
(
pool
,
builder
);
}
if
(
StringUtils
.
hasText
(
getProperties
().
getUrl
()))
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java
View file @
55ff1630
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
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.
...
...
@@ -51,6 +51,7 @@ import org.springframework.util.StringUtils;
*
* @author Mark Paluch
* @author Andy Wilkinson
* @author Weix Sun
*/
@Configuration
(
proxyBeanMethods
=
false
)
@ConditionalOnClass
(
RedisClient
.
class
)
...
...
@@ -104,10 +105,10 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
}
private
LettuceClientConfigurationBuilder
createBuilder
(
Pool
pool
)
{
if
(
pool
==
null
)
{
return
LettuceClientConfiguration
.
builder
(
);
if
(
pool
.
isEnabled
()
)
{
return
new
PoolBuilderFactory
().
createBuilder
(
pool
);
}
return
new
PoolBuilderFactory
().
createBuilder
(
pool
);
return
LettuceClientConfiguration
.
builder
(
);
}
private
LettuceClientConfigurationBuilder
applyProperties
(
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java
View file @
55ff1630
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
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.
...
...
@@ -233,6 +233,8 @@ public class RedisProperties {
*/
public
static
class
Pool
{
private
boolean
enabled
;
/**
* Maximum number of "idle" connections in the pool. Use a negative value to
* indicate an unlimited number of idle connections.
...
...
@@ -265,6 +267,14 @@ public class RedisProperties {
*/
private
Duration
timeBetweenEvictionRuns
;
public
boolean
isEnabled
()
{
return
this
.
enabled
;
}
public
void
setEnabled
(
boolean
enabled
)
{
this
.
enabled
=
enabled
;
}
public
int
getMaxIdle
()
{
return
this
.
maxIdle
;
}
...
...
@@ -396,14 +406,16 @@ public class RedisProperties {
/**
* Jedis pool configuration.
*/
private
Pool
pool
;
private
final
Pool
pool
;
public
Pool
getPool
()
{
return
this
.
pool
;
public
Jedis
()
{
Pool
pool
=
new
Pool
();
pool
.
setEnabled
(
true
);
this
.
pool
=
pool
;
}
public
void
setPool
(
Pool
pool
)
{
this
.
pool
=
pool
;
public
Pool
getPool
(
)
{
return
this
.
pool
;
}
}
...
...
@@ -421,7 +433,7 @@ public class RedisProperties {
/**
* Lettuce pool configuration.
*/
private
Pool
pool
;
private
final
Pool
pool
=
new
Pool
()
;
private
final
Cluster
cluster
=
new
Cluster
();
...
...
@@ -437,10 +449,6 @@ public class RedisProperties {
return
this
.
pool
;
}
public
void
setPool
(
Pool
pool
)
{
this
.
pool
=
pool
;
}
public
Cluster
getCluster
()
{
return
this
.
cluster
;
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java
View file @
55ff1630
...
...
@@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Mark Paluch
* @author Stephane Nicoll
* @author Weix Sun
*/
@ClassPathExclusions
(
"lettuce-core-*.jar"
)
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
void
testRedisConfigurationWithTimeoutAndConnectTimeout
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.redis.host:foo"
,
"spring.redis.timeout:250"
,
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java
View file @
55ff1630
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
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.
...
...
@@ -65,6 +65,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* @author Stephane Nicoll
* @author Alen Turkovic
* @author Scott Frederick
* @author Weix Sun
*/
class
RedisAutoConfigurationTests
{
...
...
@@ -155,10 +156,25 @@ class RedisAutoConfigurationTests {
}
@Test
void
testRedisConfigurationWithPool
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.redis.host:foo"
,
"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"
,
void
testRedisConfigurationWithPoolUsingDefaultValue
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.redis.host:foo"
,
"spring.redis.lettuce.pool.enabled:true"
)
.
run
((
context
)
->
{
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
)
->
{
LettuceConnectionFactory
cf
=
context
.
getBean
(
LettuceConnectionFactory
.
class
);
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
void
testRedisConfigurationWithTimeoutAndConnectTimeout
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.redis.host:foo"
,
"spring.redis.timeout:250"
,
...
...
spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-redis/src/main/resources/application.properties
0 → 100644
View file @
55ff1630
spring.redis.lettuce.pool.enabled
=
false
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment