29
29
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
30
30
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
31
31
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
32
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingClass ;
32
33
import org .springframework .boot .autoconfigure .data .redis .RedisProperties .Cluster ;
34
+ import org .springframework .boot .autoconfigure .data .redis .RedisProperties .Lettuce ;
33
35
import org .springframework .boot .autoconfigure .data .redis .RedisProperties .Sentinel ;
34
36
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
35
37
import org .springframework .context .annotation .Bean ;
40
42
import org .springframework .data .redis .connection .RedisSentinelConfiguration ;
41
43
import org .springframework .data .redis .connection .jedis .JedisConnection ;
42
44
import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
43
- import org .springframework .data .redis .connection .lettuce .LettuceConnection ;
44
45
import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
45
46
import org .springframework .data .redis .core .RedisOperations ;
46
47
import org .springframework .data .redis .core .RedisTemplate ;
47
48
import org .springframework .data .redis .core .StringRedisTemplate ;
48
49
import org .springframework .util .Assert ;
49
50
import org .springframework .util .StringUtils ;
50
51
52
+
51
53
/**
52
54
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis support.
53
55
*
61
63
* @author Mark Paluch
62
64
*/
63
65
@ Configuration
64
- @ ConditionalOnClass ({ RedisOperations .class , })
66
+ @ ConditionalOnClass ({ RedisOperations .class })
65
67
@ EnableConfigurationProperties (RedisProperties .class )
66
68
public class RedisAutoConfiguration {
67
69
68
70
/**
69
- * Redis connection configuration.
71
+ * Jedis Redis connection configuration.
70
72
*/
71
73
@ Configuration
72
- @ ConditionalOnClass (GenericObjectPool .class )
73
- protected static class RedisConnectionConfiguration {
74
+ @ ConditionalOnClass ({ GenericObjectPool .class , JedisConnection . class , Jedis . class } )
75
+ protected static class JedisRedisConnectionConfiguration extends RedisBaseConfiguration {
74
76
75
77
private final RedisProperties properties ;
76
78
77
- private final RedisSentinelConfiguration sentinelConfiguration ;
78
-
79
- private final RedisClusterConfiguration clusterConfiguration ;
80
-
81
- public RedisConnectionConfiguration (RedisProperties properties ,
79
+ public JedisRedisConnectionConfiguration (RedisProperties properties ,
82
80
ObjectProvider <RedisSentinelConfiguration > sentinelConfigurationProvider ,
83
81
ObjectProvider <RedisClusterConfiguration > clusterConfigurationProvider ) {
82
+ super (properties , sentinelConfigurationProvider , clusterConfigurationProvider );
84
83
this .properties = properties ;
85
- this .sentinelConfiguration = sentinelConfigurationProvider .getIfAvailable ();
86
- this .clusterConfiguration = clusterConfigurationProvider .getIfAvailable ();
87
84
}
88
85
89
86
@ Bean
@@ -93,68 +90,6 @@ public JedisConnectionFactory redisConnectionFactory()
93
90
return applyProperties (createJedisConnectionFactory ());
94
91
}
95
92
96
- protected final RedisSentinelConfiguration getSentinelConfig () {
97
- if (this .sentinelConfiguration != null ) {
98
- return this .sentinelConfiguration ;
99
- }
100
- Sentinel sentinelProperties = this .properties .getSentinel ();
101
- if (sentinelProperties != null ) {
102
- RedisSentinelConfiguration config = new RedisSentinelConfiguration ();
103
- config .master (sentinelProperties .getMaster ());
104
- config .setSentinels (createSentinels (sentinelProperties ));
105
- return config ;
106
- }
107
- return null ;
108
- }
109
-
110
- /**
111
- * Create a {@link RedisClusterConfiguration} if necessary.
112
- * @return {@literal null} if no cluster settings are set.
113
- */
114
- protected final RedisClusterConfiguration getClusterConfiguration () {
115
- if (this .clusterConfiguration != null ) {
116
- return this .clusterConfiguration ;
117
- }
118
- if (this .properties .getCluster () == null ) {
119
- return null ;
120
- }
121
- Cluster clusterProperties = this .properties .getCluster ();
122
- RedisClusterConfiguration config = new RedisClusterConfiguration (
123
- clusterProperties .getNodes ());
124
-
125
- if (clusterProperties .getMaxRedirects () != null ) {
126
- config .setMaxRedirects (clusterProperties .getMaxRedirects ());
127
- }
128
- return config ;
129
- }
130
-
131
- private List <RedisNode > createSentinels (Sentinel sentinel ) {
132
- List <RedisNode > nodes = new ArrayList <RedisNode >();
133
- for (String node : StringUtils
134
- .commaDelimitedListToStringArray (sentinel .getNodes ())) {
135
- try {
136
- String [] parts = StringUtils .split (node , ":" );
137
- Assert .state (parts .length == 2 , "Must be defined as 'host:port'" );
138
- nodes .add (new RedisNode (parts [0 ], Integer .valueOf (parts [1 ])));
139
- }
140
- catch (RuntimeException ex ) {
141
- throw new IllegalStateException (
142
- "Invalid redis sentinel " + "property '" + node + "'" , ex );
143
- }
144
- }
145
- return nodes ;
146
- }
147
- }
148
-
149
- /**
150
- * Base class for Redis configurations using the Jedis driver.
151
- */
152
- protected static abstract class AbstractJedisRedisConfiguration
153
- extends AbstractRedisConfiguration {
154
-
155
- @ Autowired
156
- protected RedisProperties properties ;
157
-
158
93
protected final JedisConnectionFactory applyProperties (
159
94
JedisConnectionFactory factory ) {
160
95
factory .setHostName (this .properties .getHost ());
@@ -169,52 +104,15 @@ protected final JedisConnectionFactory applyProperties(
169
104
return factory ;
170
105
}
171
106
172
- }
173
-
174
- /**
175
- * Redis connection configuration.
176
- */
177
- @ Configuration
178
- @ ConditionalOnMissingClass ("org.apache.commons.pool2.impl.GenericObjectPool" )
179
- protected static class RedisConnectionConfiguration
180
- extends AbstractRedisConfiguration {
181
-
182
- @ Bean
183
- @ ConditionalOnMissingBean (RedisConnectionFactory .class )
184
- public JedisConnectionFactory redisConnectionFactory ()
185
- throws UnknownHostException {
186
- return applyProperties (createJedisConnectionFactory ());
187
- }
188
-
189
107
private JedisConnectionFactory createJedisConnectionFactory () {
190
- if (getSentinelConfig () != null ) {
191
- return new JedisConnectionFactory (getSentinelConfig ());
108
+
109
+ JedisPoolConfig poolConfig ;
110
+ if (this .properties .getJedis () != null && this .properties .getJedis ().getPool () != null ) {
111
+ poolConfig = jedisPoolConfig (this .properties .getJedis ().getPool ());
192
112
}
193
- if ( getClusterConfiguration () != null ) {
194
- return new JedisConnectionFactory ( getClusterConfiguration () );
113
+ else {
114
+ poolConfig = new JedisPoolConfig ( );
195
115
}
196
- return new JedisConnectionFactory ();
197
- }
198
- }
199
-
200
- /**
201
- * Redis pooled connection configuration.
202
- */
203
- @ Configuration
204
- @ ConditionalOnClass (GenericObjectPool .class )
205
- protected static class RedisPooledConnectionConfiguration
206
- extends AbstractRedisConfiguration {
207
-
208
- @ Bean
209
- @ ConditionalOnMissingBean (RedisConnectionFactory .class )
210
- public JedisConnectionFactory redisConnectionFactory ()
211
- throws UnknownHostException {
212
- return applyProperties (createJedisConnectionFactory ());
213
- }
214
-
215
- private JedisConnectionFactory createJedisConnectionFactory () {
216
- JedisPoolConfig poolConfig = this .properties .getPool () != null
217
- ? jedisPoolConfig () : new JedisPoolConfig ();
218
116
219
117
if (getSentinelConfig () != null ) {
220
118
return new JedisConnectionFactory (getSentinelConfig (), poolConfig );
@@ -225,9 +123,8 @@ private JedisConnectionFactory createJedisConnectionFactory() {
225
123
return new JedisConnectionFactory (poolConfig );
226
124
}
227
125
228
- private JedisPoolConfig jedisPoolConfig () {
126
+ private JedisPoolConfig jedisPoolConfig (RedisProperties . Pool props ) {
229
127
JedisPoolConfig config = new JedisPoolConfig ();
230
- RedisProperties .Pool props = this .properties .getPool ();
231
128
config .setMaxTotal (props .getMaxActive ());
232
129
config .setMaxIdle (props .getMaxIdle ());
233
130
config .setMinIdle (props .getMinIdle ());
@@ -238,13 +135,28 @@ private JedisPoolConfig jedisPoolConfig() {
238
135
}
239
136
240
137
/**
241
- * Base class for Redis configurations using the Jedis driver .
138
+ * Lettuce Redis connection configuration .
242
139
*/
243
- protected static abstract class AbstractLettuceRedisConfiguration
244
- extends AbstractRedisConfiguration {
140
+ @ Configuration
141
+ @ ConditionalOnClass (RedisClient .class )
142
+ @ ConditionalOnMissingClass ("redis.clients.jedis.Jedis" )
143
+ protected static class LettuceRedisConnectionConfiguration extends RedisBaseConfiguration {
245
144
246
- @ Autowired
247
- protected LettuceRedisProperties lettuceRedisProperties ;
145
+ protected final RedisProperties properties ;
146
+
147
+ public LettuceRedisConnectionConfiguration (RedisProperties properties ,
148
+ ObjectProvider <RedisSentinelConfiguration > sentinelConfigurationProvider ,
149
+ ObjectProvider <RedisClusterConfiguration > clusterConfigurationProvider ) {
150
+ super (properties , sentinelConfigurationProvider , clusterConfigurationProvider );
151
+ this .properties = properties ;
152
+ }
153
+
154
+ @ Bean
155
+ @ ConditionalOnMissingBean (RedisConnectionFactory .class )
156
+ public LettuceConnectionFactory redisConnectionFactory ()
157
+ throws UnknownHostException {
158
+ return applyProperties (createLettuceConnectionFactory ());
159
+ }
248
160
249
161
protected final LettuceConnectionFactory applyProperties (
250
162
LettuceConnectionFactory factory ) {
@@ -257,27 +169,16 @@ protected final LettuceConnectionFactory applyProperties(
257
169
if (this .properties .getTimeout () > 0 ) {
258
170
factory .setTimeout (this .properties .getTimeout ());
259
171
}
260
- if (this .lettuceRedisProperties .getShutdownTimeout () > 0 ) {
261
- factory .setShutdownTimeout (this .lettuceRedisProperties .getShutdownTimeout ());
262
- }
263
- return factory ;
264
- }
265
- }
266
172
267
- /**
268
- * Redis connection configuration using the Lettuce driver.
269
- */
270
- @ Configuration
271
- @ ConditionalOnClass ({ LettuceConnection .class , RedisClient .class })
272
- @ ConditionalOnMissingClass ("redis.clients.jedis.Jedis" )
273
- protected static class LettuceRedisConnectionConfiguration
274
- extends AbstractLettuceRedisConfiguration {
173
+ if (this .properties .getLettuce () != null ) {
275
174
276
- @ Bean
277
- @ ConditionalOnMissingBean (RedisConnectionFactory .class )
278
- public LettuceConnectionFactory redisConnectionFactory ()
279
- throws UnknownHostException {
280
- return applyProperties (createLettuceConnectionFactory ());
175
+ Lettuce lettuce = this .properties .getLettuce ();
176
+ if (lettuce .getShutdownTimeout () >= 0 ) {
177
+ factory .setShutdownTimeout (this .properties .getLettuce ().getShutdownTimeout ());
178
+ }
179
+ }
180
+
181
+ return factory ;
281
182
}
282
183
283
184
private LettuceConnectionFactory createLettuceConnectionFactory () {
@@ -289,6 +190,80 @@ private LettuceConnectionFactory createLettuceConnectionFactory() {
289
190
}
290
191
return new LettuceConnectionFactory ();
291
192
}
193
+
194
+ }
195
+
196
+ protected abstract static class RedisBaseConfiguration {
197
+
198
+ protected final RedisProperties properties ;
199
+
200
+ protected final RedisSentinelConfiguration sentinelConfiguration ;
201
+
202
+ protected final RedisClusterConfiguration clusterConfiguration ;
203
+
204
+ public RedisBaseConfiguration (RedisProperties properties ,
205
+ ObjectProvider <RedisSentinelConfiguration > sentinelConfigurationProvider ,
206
+ ObjectProvider <RedisClusterConfiguration > clusterConfigurationProvider ) {
207
+ this .properties = properties ;
208
+ this .sentinelConfiguration = sentinelConfigurationProvider .getIfAvailable ();
209
+ this .clusterConfiguration = clusterConfigurationProvider .getIfAvailable ();
210
+ }
211
+
212
+ protected final RedisSentinelConfiguration getSentinelConfig () {
213
+ if (this .sentinelConfiguration != null ) {
214
+ return this .sentinelConfiguration ;
215
+ }
216
+
217
+ Sentinel sentinelProperties = this .properties .getSentinel ();
218
+ if (sentinelProperties != null ) {
219
+ RedisSentinelConfiguration config = new RedisSentinelConfiguration ();
220
+ config .master (sentinelProperties .getMaster ());
221
+ config .setSentinels (createSentinels (sentinelProperties ));
222
+ return config ;
223
+ }
224
+ return null ;
225
+ }
226
+
227
+ /**
228
+ * Create a {@link RedisClusterConfiguration} if necessary.
229
+ * @return {@literal null} if no cluster settings are set.
230
+ */
231
+ protected final RedisClusterConfiguration getClusterConfiguration () {
232
+ if (this .clusterConfiguration != null ) {
233
+ return this .clusterConfiguration ;
234
+ }
235
+
236
+ if (this .properties .getCluster () == null ) {
237
+ return null ;
238
+ }
239
+
240
+ Cluster clusterProperties = this .properties .getCluster ();
241
+ RedisClusterConfiguration config = new RedisClusterConfiguration (
242
+ clusterProperties .getNodes ());
243
+
244
+ if (clusterProperties .getMaxRedirects () != null ) {
245
+ config .setMaxRedirects (clusterProperties .getMaxRedirects ());
246
+ }
247
+ return config ;
248
+ }
249
+
250
+ private List <RedisNode > createSentinels (Sentinel sentinel ) {
251
+ List <RedisNode > nodes = new ArrayList <RedisNode >();
252
+ for (String node : StringUtils
253
+ .commaDelimitedListToStringArray (sentinel .getNodes ())) {
254
+ try {
255
+ String [] parts = StringUtils .split (node , ":" );
256
+ Assert .state (parts .length == 2 , "Must be defined as 'host:port'" );
257
+ nodes .add (new RedisNode (parts [0 ], Integer .valueOf (parts [1 ])));
258
+ }
259
+ catch (RuntimeException ex ) {
260
+ throw new IllegalStateException (
261
+ "Invalid redis sentinel " + "property '" + node + "'" , ex );
262
+ }
263
+ }
264
+ return nodes ;
265
+ }
266
+
292
267
}
293
268
294
269
/**
0 commit comments