DATAREDIS-576 - Support client name for Redis connections using Lettuce.
We now allow configuration of the client name that is applied to connections using the Lettuce driver.
LettuceClientConfiguration configuration = LettuceClientConfiguration.builder().clientName("foo-bar").build();
LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(), configuration);
Fixed some typos, javadoc and method visibility along the way.
Original Pull Request: #285
This commit is contained in:
committed by
Christoph Strobl
parent
1d528f03f2
commit
5059887a76
@@ -42,6 +42,7 @@ import org.springframework.util.Assert;
|
||||
* <li>Optional {@link HostnameVerifier}</li>
|
||||
* <li>Whether to use connection-pooling</li>
|
||||
* <li>Optional {@link GenericObjectPoolConfig}</li>
|
||||
* <li>Optional client name</li>
|
||||
* <li>Connect {@link Duration timeout}</li>
|
||||
* <li>Read {@link Duration timeout}</li>
|
||||
* </ul>
|
||||
|
||||
@@ -37,23 +37,26 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
private final boolean startTls;
|
||||
private final Optional<ClientResources> clientResources;
|
||||
private final Optional<ClientOptions> clientOptions;
|
||||
private final Optional<String> clientName;
|
||||
private final Duration timeout;
|
||||
private final Duration shutdownTimeout;
|
||||
|
||||
DefaultLettuceClientConfiguration(boolean useSsl, boolean verifyPeer, boolean startTls,
|
||||
@Nullable ClientResources clientResources, @Nullable ClientOptions clientOptions, Duration timeout,
|
||||
Duration shutdownTimeout) {
|
||||
@Nullable ClientResources clientResources, @Nullable ClientOptions clientOptions, @Nullable String clientName,
|
||||
Duration timeout, Duration shutdownTimeout) {
|
||||
|
||||
this.useSsl = useSsl;
|
||||
this.verifyPeer = verifyPeer;
|
||||
this.startTls = startTls;
|
||||
this.clientResources = Optional.ofNullable(clientResources);
|
||||
this.clientOptions = Optional.ofNullable(clientOptions);
|
||||
this.clientName = Optional.ofNullable(clientName);
|
||||
this.timeout = timeout;
|
||||
this.shutdownTimeout = shutdownTimeout;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#useSsl()
|
||||
*/
|
||||
@Override
|
||||
@@ -61,7 +64,8 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
return useSsl;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isVerifyPeer()
|
||||
*/
|
||||
@Override
|
||||
@@ -69,7 +73,8 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
return verifyPeer;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#isStartTls()
|
||||
*/
|
||||
@Override
|
||||
@@ -77,7 +82,8 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
return startTls;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientResources()
|
||||
*/
|
||||
@Override
|
||||
@@ -85,7 +91,8 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
return clientResources;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientOptions()
|
||||
*/
|
||||
@Override
|
||||
@@ -93,7 +100,17 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
return clientOptions;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> getClientName() {
|
||||
return clientName;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getTimeout()
|
||||
*/
|
||||
@Override
|
||||
@@ -101,7 +118,8 @@ class DefaultLettuceClientConfiguration implements LettuceClientConfiguration {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getShutdownTimeout()
|
||||
*/
|
||||
@Override
|
||||
|
||||
@@ -87,6 +87,15 @@ class DefaultLettucePoolingClientConfiguration implements LettucePoolingClientCo
|
||||
return clientConfiguration.getClientOptions();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> getClientName() {
|
||||
return clientConfiguration.getClientName();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getCommandTimeout()
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.springframework.util.Assert;
|
||||
* <li>Whether to use StartTLS</li>
|
||||
* <li>Optional {@link ClientResources}</li>
|
||||
* <li>Optional {@link ClientOptions}</li>
|
||||
* <li>Optional client name</li>
|
||||
* <li>Client {@link Duration timeout}</li>
|
||||
* <li>Shutdown {@link Duration timeout}</li>
|
||||
* </ul>
|
||||
@@ -75,6 +76,12 @@ public interface LettuceClientConfiguration {
|
||||
*/
|
||||
Optional<ClientOptions> getClientOptions();
|
||||
|
||||
/**
|
||||
* @return the optional client name to be set with {@code CLIENT SETNAME}.
|
||||
* @since 2.1
|
||||
*/
|
||||
Optional<String> getClientName();
|
||||
|
||||
/**
|
||||
* @return the timeout.
|
||||
*/
|
||||
@@ -109,6 +116,8 @@ public interface LettuceClientConfiguration {
|
||||
* <dd>none</dd>
|
||||
* <dt>Client Resources</dt>
|
||||
* <dd>none</dd>
|
||||
* <dt>Client name</dt>
|
||||
* <dd>none</dd>
|
||||
* <dt>Connect Timeout</dt>
|
||||
* <dd>60 Seconds</dd>
|
||||
* <dt>Shutdown Timeout</dt>
|
||||
@@ -132,6 +141,7 @@ public interface LettuceClientConfiguration {
|
||||
boolean startTls;
|
||||
@Nullable ClientResources clientResources;
|
||||
@Nullable ClientOptions clientOptions;
|
||||
@Nullable String clientName;
|
||||
Duration timeout = Duration.ofSeconds(RedisURI.DEFAULT_TIMEOUT);
|
||||
Duration shutdownTimeout = Duration.ofMillis(100);
|
||||
|
||||
@@ -178,6 +188,21 @@ public interface LettuceClientConfiguration {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a {@code clientName} to be set with {@code CLIENT SETNAME}.
|
||||
*
|
||||
* @param clientName must not be {@literal null} or empty.
|
||||
* @return {@literal this} builder.
|
||||
* @throws IllegalArgumentException if clientName is {@literal null}.
|
||||
*/
|
||||
public LettuceClientConfigurationBuilder clientName(String clientName) {
|
||||
|
||||
Assert.hasText(clientName, "Client name must not be null or empty!");
|
||||
|
||||
this.clientName = clientName;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a command timeout.
|
||||
*
|
||||
@@ -216,7 +241,7 @@ public interface LettuceClientConfiguration {
|
||||
public LettuceClientConfiguration build() {
|
||||
|
||||
return new DefaultLettuceClientConfiguration(useSsl, verifyPeer, startTls, clientResources, clientOptions,
|
||||
timeout, shutdownTimeout);
|
||||
clientName, timeout, shutdownTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -586,6 +586,30 @@ public class LettuceConnectionFactory
|
||||
standaloneConfig.setDatabase(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the client name.
|
||||
*
|
||||
* @return the client name.
|
||||
* @since 2.1
|
||||
*/
|
||||
@Nullable
|
||||
public String getClientName() {
|
||||
return clientConfiguration.getClientName().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the client name used by this connection factory. Defaults to none which does not set a client name.
|
||||
*
|
||||
* @param clientName the client name.
|
||||
* @since 2.1
|
||||
* @deprecated configure the client name using {@link LettuceClientConfiguration}.
|
||||
* @throws IllegalStateException if {@link JedisClientConfiguration} is immutable.
|
||||
*/
|
||||
@Deprecated
|
||||
public void setClientName(String clientName) {
|
||||
this.getMutableConfiguration().setClientName(clientName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the password used for authenticating with the Redis server.
|
||||
*
|
||||
@@ -849,6 +873,7 @@ public class LettuceConnectionFactory
|
||||
RedisURI redisUri = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
|
||||
|
||||
getRedisPassword().toOptional().ifPresent(redisUri::setPassword);
|
||||
clientConfiguration.getClientName().ifPresent(redisUri::setClientName);
|
||||
redisUri.setTimeout(clientConfiguration.getCommandTimeout());
|
||||
|
||||
return redisUri;
|
||||
@@ -859,6 +884,7 @@ public class LettuceConnectionFactory
|
||||
RedisURI.Builder builder = RedisURI.Builder.redis(host, port);
|
||||
|
||||
getRedisPassword().toOptional().ifPresent(builder::withPassword);
|
||||
clientConfiguration.getClientName().ifPresent(builder::withClientName);
|
||||
|
||||
builder.withSsl(clientConfiguration.isUseSsl());
|
||||
builder.withVerifyPeer(clientConfiguration.isVerifyPeer());
|
||||
@@ -897,6 +923,7 @@ public class LettuceConnectionFactory
|
||||
private boolean verifyPeer = true;
|
||||
private boolean startTls;
|
||||
private @Nullable ClientResources clientResources;
|
||||
private @Nullable String clientName;
|
||||
private Duration timeout = Duration.ofSeconds(RedisURI.DEFAULT_TIMEOUT);
|
||||
private Duration shutdownTimeout = Duration.ofMillis(100);
|
||||
|
||||
@@ -908,7 +935,7 @@ public class LettuceConnectionFactory
|
||||
return useSsl;
|
||||
}
|
||||
|
||||
public void setUseSsl(boolean useSsl) {
|
||||
void setUseSsl(boolean useSsl) {
|
||||
this.useSsl = useSsl;
|
||||
}
|
||||
|
||||
@@ -920,7 +947,7 @@ public class LettuceConnectionFactory
|
||||
return verifyPeer;
|
||||
}
|
||||
|
||||
public void setVerifyPeer(boolean verifyPeer) {
|
||||
void setVerifyPeer(boolean verifyPeer) {
|
||||
this.verifyPeer = verifyPeer;
|
||||
}
|
||||
|
||||
@@ -932,7 +959,7 @@ public class LettuceConnectionFactory
|
||||
return startTls;
|
||||
}
|
||||
|
||||
public void setStartTls(boolean startTls) {
|
||||
void setStartTls(boolean startTls) {
|
||||
this.startTls = startTls;
|
||||
}
|
||||
|
||||
@@ -944,7 +971,7 @@ public class LettuceConnectionFactory
|
||||
return Optional.ofNullable(clientResources);
|
||||
}
|
||||
|
||||
public void setClientResources(ClientResources clientResources) {
|
||||
void setClientResources(ClientResources clientResources) {
|
||||
this.clientResources = clientResources;
|
||||
}
|
||||
|
||||
@@ -956,6 +983,19 @@ public class LettuceConnectionFactory
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getClientName()
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> getClientName() {
|
||||
return Optional.ofNullable(clientName);
|
||||
}
|
||||
|
||||
void setClientName(String clientName) {
|
||||
this.clientName = clientName;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration#getTimeout()
|
||||
*/
|
||||
@@ -964,7 +1004,7 @@ public class LettuceConnectionFactory
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(Duration timeout) {
|
||||
void setTimeout(Duration timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@@ -976,7 +1016,7 @@ public class LettuceConnectionFactory
|
||||
return shutdownTimeout;
|
||||
}
|
||||
|
||||
public void setShutdownTimeout(Duration shutdownTimeout) {
|
||||
void setShutdownTimeout(Duration shutdownTimeout) {
|
||||
this.shutdownTimeout = shutdownTimeout;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class JedisConnectionFactorySentinelIntegrationTests {
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-574
|
||||
public void shouldInitiaizeWithSentinelConfiguration() {
|
||||
public void shouldInitializeWithSentinelConfiguration() {
|
||||
|
||||
JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder() //
|
||||
.clientName("clientName") //
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.junit.Test;
|
||||
*/
|
||||
public class LettuceClientConfigurationUnitTests {
|
||||
|
||||
@Test // DATAREDIS-574, DATAREDIS-667
|
||||
@Test // DATAREDIS-574, DATAREDIS-576, DATAREDIS-667
|
||||
public void shouldCreateEmptyConfiguration() {
|
||||
|
||||
LettuceClientConfiguration configuration = LettuceClientConfiguration.defaultConfiguration();
|
||||
@@ -42,11 +42,12 @@ public class LettuceClientConfigurationUnitTests {
|
||||
assertThat(configuration.isStartTls()).isFalse();
|
||||
assertThat(configuration.getClientOptions()).isEmpty();
|
||||
assertThat(configuration.getClientResources()).isEmpty();
|
||||
assertThat(configuration.getClientName()).isEmpty();
|
||||
assertThat(configuration.getCommandTimeout()).isEqualTo(Duration.ofSeconds(60));
|
||||
assertThat(configuration.getShutdownTimeout()).isEqualTo(Duration.ofMillis(100));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-574, DATAREDIS-667
|
||||
@Test // DATAREDIS-574, DATAREDIS-576, DATAREDIS-667
|
||||
public void shouldConfigureAllProperties() {
|
||||
|
||||
ClientOptions clientOptions = ClientOptions.create();
|
||||
@@ -58,6 +59,7 @@ public class LettuceClientConfigurationUnitTests {
|
||||
.startTls().and() //
|
||||
.clientOptions(clientOptions) //
|
||||
.clientResources(sharedClientResources) //
|
||||
.clientName("foo") //
|
||||
.commandTimeout(Duration.ofMinutes(5)) //
|
||||
.shutdownTimeout(Duration.ofHours(2)) //
|
||||
.build();
|
||||
@@ -67,6 +69,7 @@ public class LettuceClientConfigurationUnitTests {
|
||||
assertThat(configuration.isStartTls()).isTrue();
|
||||
assertThat(configuration.getClientOptions()).contains(clientOptions);
|
||||
assertThat(configuration.getClientResources()).contains(sharedClientResources);
|
||||
assertThat(configuration.getClientName()).contains("foo");
|
||||
assertThat(configuration.getCommandTimeout()).isEqualTo(Duration.ofMinutes(5));
|
||||
assertThat(configuration.getShutdownTimeout()).isEqualTo(Duration.ofHours(2));
|
||||
}
|
||||
|
||||
@@ -361,4 +361,38 @@ public class LettuceConnectionFactoryTests {
|
||||
|
||||
factory.destroy();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-576
|
||||
public void connectionAppliesClientName() {
|
||||
|
||||
LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
|
||||
.clientResources(LettuceTestClientResources.getSharedClientResources()).clientName("clientName").build();
|
||||
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(), configuration);
|
||||
factory.setShareNativeConnection(false);
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
RedisConnection connection = factory.getConnection();
|
||||
|
||||
assertThat(connection.getClientName(), is(equalTo("clientName")));
|
||||
connection.close();
|
||||
|
||||
factory.destroy();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-576
|
||||
public void getClientNameShouldEqualWithFactorySetting() {
|
||||
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration());
|
||||
factory.setClientResources(LettuceTestClientResources.getSharedClientResources());
|
||||
factory.setClientName("clientName");
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
RedisConnection connection = factory.getConnection();
|
||||
assertThat(connection.getClientName(), equalTo("clientName"));
|
||||
|
||||
connection.close();
|
||||
|
||||
factory.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ import org.springframework.data.redis.test.util.MinimumRedisVersionRule;
|
||||
import org.springframework.data.redis.test.util.RedisSentinelRule;
|
||||
|
||||
/**
|
||||
* Integration tests for Lettuce and Redis Sentinel interaction.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@@ -148,4 +150,24 @@ public class LettuceSentinelIntegrationTests extends AbstractConnectionIntegrati
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-576
|
||||
public void connectionAppliesClientName() {
|
||||
|
||||
LettuceClientConfiguration clientName = LettuceClientConfiguration.builder()
|
||||
.clientResources(LettuceTestClientResources.getSharedClientResources()).clientName("clientName").build();
|
||||
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(SENTINEL_CONFIG, clientName);
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
ConnectionFactoryTracker.add(factory);
|
||||
|
||||
StringRedisConnection connection = new DefaultStringRedisConnection(factory.getConnection());
|
||||
|
||||
try {
|
||||
assertThat(connection.getClientName(), is(equalTo("clientName")));
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user