Skip to content

Commit 7426b27

Browse files
mp911deodrotbohm
authored andcommitted
DATAREDIS-348 - Switch to mp911de/lettuce 3.0.
Updated lettuce redis client to latest snapshot, adopt changed API and use API for commands which were emulated using LUA script Implemente Sentinel support using lettuce library, adopt finer grained exceptions and aligned exception behavior to match other libraries. Added shutdown-timeout property in order to control shutdown duration of the netty framework which reduces test run duration. Documentation updated as well. Contains also a fix to use psetex instead of setex (typo). Switch to Jedis for Redis Version discovery in tests. Polished documentation. Use LettuceConverter and native time() method. Original pull request: #144. Related pull request: #104.
1 parent 4cd6b82 commit 7426b27

23 files changed

+1237
-200
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ group = 'org.springframework.data'
1919
repositories {
2020
maven { url "https://repo.spring.io/libs-snapshot" }
2121
maven { url "https://repo.spring.io/plugins-release" }
22+
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
2223
mavenCentral()
2324
jcenter()
2425
}
@@ -84,7 +85,7 @@ dependencies {
8485
compile("com.github.spullara.redis:client:$srpVersion", optional)
8586
compile("org.jredis:jredis-core-api:$jredisVersion", optional)
8687
compile("org.jredis:jredis-core-ri:$jredisVersion", optional)
87-
compile("com.lambdaworks:lettuce:$lettuceVersion", optional)
88+
compile("biz.paluch.redis:lettuce:$lettuceVersion", optional)
8889

8990
// Mappers
9091
compile("org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion", optional)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ srpVersion=0.7
1010
jacksonVersion=1.8.8
1111
fasterXmlJacksonVersion=2.5.1
1212
fasterXmlJacksonDatabindVersion=2.5.1
13-
lettuceVersion=2.3.3
13+
lettuceVersion=3.2.Final
1414
mockitoVersion=1.10.19

src/asciidoc/reference/redis.adoc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Needless to say, the configuration is quite similar to that of the other connect
141141
[[redis:connectors:lettuce]]
142142
=== Configuring Lettuce connector
143143

144-
https://github.com/wg/lettuce[Lettuce] is the fourth open-source connector supported by Spring Data Redis through the `org.springframework.data.redis.connection.lettuce` package.
144+
https://github.com/mp911de/lettuce[Lettuce] is the fourth open-source connector supported by Spring Data Redis through the `org.springframework.data.redis.connection.lettuce` package.
145145

146146
Its configuration is probably easy to guess:
147147

@@ -165,15 +165,15 @@ There are also a few Lettuce-specific connection parameters that can be tweaked.
165165

166166
For dealing with high available Redis there is support for http://redis.io/topics/sentinel[Redis Sentinel] using `RedisSentinelConfiguration`.
167167

168-
NOTE: Please note that currently only http://github.com/xetorthio/jedis[Jedis] supports Redis Sentinel.
168+
NOTE: Please note that currently only http://github.com/xetorthio/jedis[Jedis] and lettuce http://github.com/mp911de/lettuce[Lettuce] support Redis Sentinel.
169169

170170
[source,java]
171171
----
172172
@Bean
173173
public RedisConnectionFactory jedisConnectionFactory() {
174174
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
175175
.sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
176-
return new JedisConnectionFactory(sentinelConfig);
176+
return new JedisConnectionFactory(sentinelConfig);
177177
}
178178
----
179179

@@ -185,6 +185,16 @@ public RedisConnectionFactory jedisConnectionFactory() {
185185
- `spring.redis.sentinel.master`: name of the master node.
186186
- `spring.redis.sentinel.nodes`: Comma delimited list of host:port pairs.
187187
====
188+
=======
189+
[source,java]
190+
----
191+
@Bean
192+
public RedisConnectionFactory lettuceConnectionFactory() {
193+
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
194+
.sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
195+
return new LettuceConnectionFactory(sentinelConfig);
196+
}
197+
----
188198
189199
Sometimes direct interaction with the one of the Sentinels is required. Using `RedisConnectionFactory.getSentinelConnection()` or `RedisConnection.getSentinelCommands()` gives you access to the first active Sentinel configured.
190200

src/main/java/org/springframework/data/redis/connection/RedisConnectionFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public interface RedisConnectionFactory extends PersistenceExceptionTranslator {
4444
boolean getConvertPipelineAndTxResults();
4545

4646
/**
47-
* @return
47+
* Provides a suitable connection for interacting with Redis Sentinel.
48+
* @return connection for interacting with Redis Sentinel.
4849
* @since 1.4
4950
*/
5051
RedisSentinelConnection getSentinelConnection();

src/main/java/org/springframework/data/redis/connection/lettuce/AuthenticatingRedisClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
* Extension of {@link RedisClient} that calls auth on all new connections using the supplied credentials
2626
*
2727
* @author Jennifer Hickey
28+
* @deprecated Use password in RedisURI
2829
*/
30+
@Deprecated
2931
public class AuthenticatingRedisClient extends RedisClient {
3032

3133
private String password;

src/main/java/org/springframework/data/redis/connection/lettuce/DefaultLettucePool.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
import java.util.concurrent.TimeUnit;
1919

20+
import com.lambdaworks.redis.RedisURI;
2021
import org.apache.commons.pool2.BasePooledObjectFactory;
2122
import org.apache.commons.pool2.PooledObject;
2223
import org.apache.commons.pool2.impl.DefaultPooledObject;
2324
import org.apache.commons.pool2.impl.GenericObjectPool;
2425
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
2526
import org.springframework.beans.factory.InitializingBean;
2627
import org.springframework.data.redis.connection.PoolException;
28+
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
2729
import org.springframework.util.Assert;
2830

2931
import com.lambdaworks.redis.RedisAsyncConnection;
@@ -34,6 +36,7 @@
3436
*
3537
* @author Jennifer Hickey
3638
* @author Christoph Strobl
39+
* @author Mark Paluch
3740
*/
3841
public class DefaultLettucePool implements LettucePool, InitializingBean {
3942

@@ -46,6 +49,7 @@ public class DefaultLettucePool implements LettucePool, InitializingBean {
4649
private int port = 6379;
4750
private String password;
4851
private long timeout = TimeUnit.MILLISECONDS.convert(60, TimeUnit.SECONDS);
52+
private RedisSentinelConfiguration sentinelConfiguration;
4953

5054
/**
5155
* Constructs a new <code>DefaultLettucePool</code> instance with default settings.
@@ -63,6 +67,16 @@ public DefaultLettucePool(String hostName, int port) {
6367
this.port = port;
6468
}
6569

70+
/**
71+
* Uses the {@link RedisSentinelConfiguration} and {@link RedisClient} defaults for configuring the connection pool
72+
* based on sentinels.
73+
*
74+
* @param sentinelConfiguration The Sentinel configuration
75+
*/
76+
public DefaultLettucePool(RedisSentinelConfiguration sentinelConfiguration) {
77+
this.sentinelConfiguration = sentinelConfiguration;
78+
}
79+
6680
/**
6781
* Uses the {@link RedisClient} defaults for configuring the connection pool
6882
*
@@ -76,14 +90,43 @@ public DefaultLettucePool(String hostName, int port, GenericObjectPoolConfig poo
7690
this.poolConfig = poolConfig;
7791
}
7892

93+
/**
94+
* @return true when {@link RedisSentinelConfiguration} is present.
95+
* @since 1.5
96+
*/
97+
public boolean isRedisSentinelAware() {
98+
return sentinelConfiguration != null;
99+
}
100+
79101
@SuppressWarnings({ "rawtypes" })
80102
public void afterPropertiesSet() {
81-
this.client = password != null ? new AuthenticatingRedisClient(hostName, port, password) : new RedisClient(
82-
hostName, port);
103+
this.client = new RedisClient(getRedisURI());
83104
client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
84105
this.internalPool = new GenericObjectPool<RedisAsyncConnection>(new LettuceFactory(client, dbIndex), poolConfig);
85106
}
86107

108+
/**
109+
*
110+
* @return a RedisURI pointing either to a single Redis host or containing a set of sentinels.
111+
*/
112+
private RedisURI getRedisURI() {
113+
114+
if(isRedisSentinelAware()) {
115+
return LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
116+
}
117+
118+
return createSimpleHostRedisURI();
119+
}
120+
121+
private RedisURI createSimpleHostRedisURI() {
122+
RedisURI.Builder builder = RedisURI.Builder.redis(hostName, port);
123+
if(password != null) {
124+
builder.withPassword(password);
125+
}
126+
builder.withTimeout(timeout, TimeUnit.MILLISECONDS);
127+
return builder.build();
128+
}
129+
87130
@SuppressWarnings("unchecked")
88131
public RedisAsyncConnection<byte[], byte[]> getResource() {
89132
try {
@@ -118,6 +161,10 @@ public void destroy() {
118161
}
119162
}
120163

164+
/**
165+
*
166+
* @return The Redis client
167+
*/
121168
public RedisClient getClient() {
122169
return client;
123170
}

0 commit comments

Comments
 (0)