From 43957731b114168bd6cd955fc303ab519d83d777 Mon Sep 17 00:00:00 2001 From: mirageoasis Date: Thu, 15 Aug 2024 01:39:07 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20redis=20lettuce=20=EC=99=80=20redis?= =?UTF-8?q?son=20=EB=91=98=EB=8B=A4=20=EC=84=A4=EC=A0=95=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++ .../global/config/LettuceConfig.java | 33 +++++++++++++++++ .../global/config/RedissonConfig.java | 36 +++++++++++++++++++ .../global/config/LettuceConfigTest.java | 26 ++++++++++++++ .../global/config/RedissonConfigTest.java | 34 ++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 src/main/java/com/thirdparty/ticketing/global/config/LettuceConfig.java create mode 100644 src/main/java/com/thirdparty/ticketing/global/config/RedissonConfig.java create mode 100644 src/test/java/com/thirdparty/ticketing/global/config/LettuceConfigTest.java create mode 100644 src/test/java/com/thirdparty/ticketing/global/config/RedissonConfigTest.java diff --git a/build.gradle b/build.gradle index 80c69468..9e09d86e 100644 --- a/build.gradle +++ b/build.gradle @@ -35,7 +35,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + // Redisson + implementation 'org.redisson:redisson-spring-boot-starter:3.17.6' //jwt implementation 'io.jsonwebtoken:jjwt-api:0.12.3' runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3' diff --git a/src/main/java/com/thirdparty/ticketing/global/config/LettuceConfig.java b/src/main/java/com/thirdparty/ticketing/global/config/LettuceConfig.java new file mode 100644 index 00000000..5f3682e5 --- /dev/null +++ b/src/main/java/com/thirdparty/ticketing/global/config/LettuceConfig.java @@ -0,0 +1,33 @@ +package com.thirdparty.ticketing.global.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import lombok.Setter; + +@Configuration +@ConfigurationProperties(prefix = "spring.data.redis.lettuce") +@Setter +public class LettuceConfig { + private String host; + private Integer port; + + @Bean + public LettuceConnectionFactory lettuceConnectionFactory() { + return new LettuceConnectionFactory(); + } + + @Bean + public StringRedisTemplate lettuceRedisTemplate( + LettuceConnectionFactory lettuceConnectionFactory) { + StringRedisTemplate redisTemplate = new StringRedisTemplate(); + redisTemplate.setConnectionFactory(lettuceConnectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + return redisTemplate; + } +} diff --git a/src/main/java/com/thirdparty/ticketing/global/config/RedissonConfig.java b/src/main/java/com/thirdparty/ticketing/global/config/RedissonConfig.java new file mode 100644 index 00000000..7921301c --- /dev/null +++ b/src/main/java/com/thirdparty/ticketing/global/config/RedissonConfig.java @@ -0,0 +1,36 @@ +package com.thirdparty.ticketing.global.config; + +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; +import org.redisson.config.SingleServerConfig; +import org.redisson.spring.data.connection.RedissonConnectionFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.StringRedisTemplate; + +import lombok.Setter; + +@Configuration +@ConfigurationProperties(prefix = "spring.data.redis.redisson") +@Setter +public class RedissonConfig { + private String host; + private Integer port; + + @Bean + public RedissonClient redissonClient() { + Config config = new Config(); + SingleServerConfig serverConfig = config.useSingleServer(); + serverConfig.setAddress("redis://" + host + ":" + port); + return Redisson.create(config); + } + + @Bean + public StringRedisTemplate redissonRedisTemplate(RedissonClient redissonClient) { + StringRedisTemplate redisTemplate = new StringRedisTemplate(); + redisTemplate.setConnectionFactory(new RedissonConnectionFactory(redissonClient)); + return redisTemplate; + } +} diff --git a/src/test/java/com/thirdparty/ticketing/global/config/LettuceConfigTest.java b/src/test/java/com/thirdparty/ticketing/global/config/LettuceConfigTest.java new file mode 100644 index 00000000..380d4b0b --- /dev/null +++ b/src/test/java/com/thirdparty/ticketing/global/config/LettuceConfigTest.java @@ -0,0 +1,26 @@ +package com.thirdparty.ticketing.global.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.StringRedisTemplate; + +@SpringBootTest( + properties = { + "spring.data.redis.lettuce.host=localhost", + "spring.data.redis.lettuce.port=6379" + }) +class LettuceConfigTest { + + @Autowired private StringRedisTemplate lettuceRedisTemplate; + + @Test + void testLettuceRedisTemplate() { + assertThat(lettuceRedisTemplate).isNotNull(); + lettuceRedisTemplate.opsForValue().set("testKey", "testValue"); + String value = lettuceRedisTemplate.opsForValue().get("testKey"); + assertThat(value).isEqualTo("testValue"); + } +} diff --git a/src/test/java/com/thirdparty/ticketing/global/config/RedissonConfigTest.java b/src/test/java/com/thirdparty/ticketing/global/config/RedissonConfigTest.java new file mode 100644 index 00000000..0e555688 --- /dev/null +++ b/src/test/java/com/thirdparty/ticketing/global/config/RedissonConfigTest.java @@ -0,0 +1,34 @@ +package com.thirdparty.ticketing.global.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.redisson.api.RedissonClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.StringRedisTemplate; + +@SpringBootTest( + properties = { + "spring.data.redis.redisson.host=localhost", + "spring.data.redis.redisson.port=6379" + }) +class RedissonConfigTest { + + @Autowired private RedissonClient redissonClient; + + @Autowired private StringRedisTemplate redissonRedisTemplate; + + @Test + void testRedissonClient() { + assertThat(redissonClient).isNotNull(); + } + + @Test + void testRedissonRedisTemplate() { + assertThat(redissonRedisTemplate).isNotNull(); + redissonRedisTemplate.opsForValue().set("testKey", "testValue"); + String value = redissonRedisTemplate.opsForValue().get("testKey"); + assertThat(value).isEqualTo("testValue"); + } +} From 32f263d19a44350f8cb110773cb1bdd829402307 Mon Sep 17 00:00:00 2001 From: mirageoasis Date: Thu, 15 Aug 2024 01:40:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20redis=20=EC=84=A4=EC=A0=95=EC=97=90?= =?UTF-8?q?=20=EC=95=8C=EB=A7=9E=EC=9D=80=20yml=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend-config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend-config b/backend-config index 9f2dcb00..44d0d8d9 160000 --- a/backend-config +++ b/backend-config @@ -1 +1 @@ -Subproject commit 9f2dcb00564df24fa3d6591e2dfecde2aff79735 +Subproject commit 44d0d8d9302141589cae777b567a3eab6fd9a68f