Skip to content

Commit 015e790

Browse files
committed
Add 'client-name' property to specify a Redis client name.
gh-17315
1 parent 8730f32 commit 015e790

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ private JedisClientConfigurationBuilder applyProperties(JedisClientConfiguration
9494
Duration timeout = getProperties().getTimeout();
9595
builder.readTimeout(timeout).connectTimeout(timeout);
9696
}
97+
if (StringUtils.hasText(getProperties().getClientName())) {
98+
builder.clientName(getProperties().getClientName());
99+
}
97100
return builder;
98101
}
99102

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ private LettuceClientConfigurationBuilder applyProperties(
114114
builder.shutdownTimeout(getProperties().getLettuce().getShutdownTimeout());
115115
}
116116
}
117+
if (StringUtils.hasText(getProperties().getClientName())) {
118+
builder.clientName(getProperties().getClientName());
119+
}
117120
return builder;
118121
}
119122

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public class RedisProperties {
7070
*/
7171
private Duration timeout;
7272

73+
/**
74+
* Configure a clientName to be set with CLIENT SETNAME.
75+
*/
76+
private String clientName;
77+
7378
private Sentinel sentinel;
7479

7580
private Cluster cluster;
@@ -134,6 +139,14 @@ public Duration getTimeout() {
134139
return this.timeout;
135140
}
136141

142+
public String getClientName() {
143+
return this.clientName;
144+
}
145+
146+
public void setClientName(String clientName) {
147+
this.clientName = clientName;
148+
}
149+
137150
public Sentinel getSentinel() {
138151
return this.sentinel;
139152
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ public void testRedisConfigurationWithTimeout() {
134134
});
135135
}
136136

137+
@Test
138+
public void testRedisConfigurationWithClientName() {
139+
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.client-name:spring-boot")
140+
.run((context) -> {
141+
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
142+
assertThat(cf.getHostName()).isEqualTo("foo");
143+
assertThat(cf.getClientName()).isEqualTo("spring-boot");
144+
});
145+
}
146+
137147
@Test
138148
public void testRedisConfigurationWithSentinel() {
139149
this.contextRunner

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ void testRedisConfigurationWithTimeout() {
160160
});
161161
}
162162

163+
@Test
164+
void testRedisConfigurationWithClientName() {
165+
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.client-name:spring-boot")
166+
.run((context) -> {
167+
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
168+
assertThat(cf.getHostName()).isEqualTo("foo");
169+
assertThat(cf.getClientName()).isEqualTo("spring-boot");
170+
});
171+
}
172+
163173
@Test
164174
void testRedisConfigurationWithSentinel() {
165175
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");

0 commit comments

Comments
 (0)