Add 'client-name' property to specify a Redis client name

See gh-17330
This commit is contained in:
Dmytro Nosan 2019-06-27 10:55:29 +03:00 committed by Andy Wilkinson
parent e496203740
commit f877caf118
5 changed files with 39 additions and 0 deletions

View File

@ -94,6 +94,9 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
Duration timeout = getProperties().getTimeout();
builder.readTimeout(timeout).connectTimeout(timeout);
}
if (StringUtils.hasText(getProperties().getClientName())) {
builder.clientName(getProperties().getClientName());
}
return builder;
}

View File

@ -114,6 +114,9 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
builder.shutdownTimeout(getProperties().getLettuce().getShutdownTimeout());
}
}
if (StringUtils.hasText(getProperties().getClientName())) {
builder.clientName(getProperties().getClientName());
}
return builder;
}

View File

@ -71,6 +71,11 @@ public class RedisProperties {
*/
private Duration timeout;
/**
* Configure a clientName to be set with CLIENT SETNAME.
*/
private String clientName;
private Sentinel sentinel;
private Cluster cluster;
@ -135,6 +140,14 @@ public class RedisProperties {
return this.timeout;
}
public String getClientName() {
return this.clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public Sentinel getSentinel() {
return this.sentinel;
}

View File

@ -134,6 +134,16 @@ public class RedisAutoConfigurationJedisTests {
});
}
@Test
public void testRedisConfigurationWithClientName() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.client-name:spring-boot")
.run((context) -> {
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getClientName()).isEqualTo("spring-boot");
});
}
@Test
public void testRedisConfigurationWithSentinel() {
this.contextRunner

View File

@ -160,6 +160,16 @@ class RedisAutoConfigurationTests {
});
}
@Test
void testRedisConfigurationWithClientName() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.client-name:spring-boot")
.run((context) -> {
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getClientName()).isEqualTo("spring-boot");
});
}
@Test
void testRedisConfigurationWithSentinel() {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");