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 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"); + } +}