From f877caf118a1ff459592bb56a0dc76caef4ba2f7 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 27 Jun 2019 10:55:29 +0300 Subject: [PATCH] Add 'client-name' property to specify a Redis client name See gh-17330 --- .../data/redis/JedisConnectionConfiguration.java | 3 +++ .../data/redis/LettuceConnectionConfiguration.java | 3 +++ .../autoconfigure/data/redis/RedisProperties.java | 13 +++++++++++++ .../redis/RedisAutoConfigurationJedisTests.java | 10 ++++++++++ .../data/redis/RedisAutoConfigurationTests.java | 10 ++++++++++ 5 files changed, 39 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java index 8a02384d66..301ab40b42 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java @@ -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; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java index ce6642dc6e..4ae272246e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java @@ -114,6 +114,9 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration { builder.shutdownTimeout(getProperties().getLettuce().getShutdownTimeout()); } } + if (StringUtils.hasText(getProperties().getClientName())) { + builder.clientName(getProperties().getClientName()); + } return builder; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java index 58e4093751..410f8c372c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java @@ -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; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java index 310ad8875f..d012fb660e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java @@ -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 diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java index 1aaf4008aa..68c4ffb082 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java @@ -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 sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");