Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-be: Redis 로컬 환경 설정 #822

Open
wants to merge 7 commits into
base: be/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ dependencies {
implementation 'org.flywaydb:flyway-core:9.22.3'
implementation 'org.flywaydb:flyway-mysql'

// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
14 changes: 14 additions & 0 deletions backend/docker-compose.local.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제목이 local인 것에 비해 redis만 관리하고 있는 스크립트니까 redis로 파일명을 바꾸거나 따로 local용 docker-compose 파일을 만드시는게 어떨까요?

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.8'

services:
redis:
image: redis:latest
container_name: redis-local
ports:
- "6379:6379"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6379-> 6379로 포트 바인딩을 같은 포트 번호로 하는 것보다
User Port 범위(1024~49151) 내의 임의의 포트를 지정해주는 것이 어떨까요?

로컬에서 모종의 의유로 중복 사용될 수도 있을 것 같아서요

networks:
- redis_network

networks:
redis_network:
driver: bridge
38 changes: 38 additions & 0 deletions backend/src/main/java/com/cruru/RedisTestController.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test용이라면 test 패키지에 넣어두면 좋을 것 같습니다!
또한, 현재 test controller에 존재하는 메서드에서는 facade 계층을 이용하지 않고 있는데, 추후 추가될 기능을 생각해서 facade도 지금 초안 정도는 만드시는것이 좋을 것 같아요 :>

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.cruru;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis-test")
public class RedisTestController {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 테스트 클래스에서 key의 life cycle을 관리하는 테스트도 만들어보시는 것이 어떨까요?

저희 메일 인증 기능의 핵심인 것 같아서요!


private final RedisTemplate<String, String> redisTemplate;

public RedisTestController(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}

// curl -X POST "http://localhost:8080/redis-test?key=testKey&value=testValue"
@PostMapping
public ResponseEntity<String> setKey(@RequestParam String key, @RequestParam String value) {
redisTemplate.opsForValue().set(key, value);
return ResponseEntity.ok("key-value: " + key + " = " + value);
}

// http://localhost:8080/redis-test?key=testKey
@GetMapping
public ResponseEntity<String> getKey(@RequestParam String key) {
String value = redisTemplate.opsForValue().get(key);
if (value == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("key " + key + " not found");
}
return ResponseEntity.ok("value for key " + key + " is: " + value);
}
}
36 changes: 36 additions & 0 deletions backend/src/main/java/com/cruru/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.cruru.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Profile({"local", "test"})
@Configuration
public class RedisConfig {

@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private int port;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
4 changes: 4 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ spring:
file-size-threshold: 2KB
max-file-size: 25MB
max-request-size: 50MB
data:
redis:
port: 6379
host: localhost

security:
jwt:
Expand Down
56 changes: 56 additions & 0 deletions backend/src/test/java/com/cruru/RedisTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.cruru;

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.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles("test")
@SpringBootTest
class RedisTest {

@Autowired
private RedisTemplate<String, String> redisTemplate;

@Test
public void shouldConnectToRedis() {
Boolean isAvailable = redisTemplate.getConnectionFactory()
.getConnection()
.ping()
.equalsIgnoreCase("PONG");
assertThat(isAvailable).isTrue();
}

@Test
public void setAndGet() {
// given
String key = "testKey";
String value = "testValue";

// when
redisTemplate.opsForValue().set(key, value);
String actual = redisTemplate.opsForValue().get(key);

// then
assertThat(actual).isEqualTo(value);
redisTemplate.delete("testKey");
}

@Test
public void delete() {
// given
String key = "testDeleteKey";
String value = "testValue";
redisTemplate.opsForValue().set(key, value);

// when
redisTemplate.delete(key);
String actual = redisTemplate.opsForValue().get(key);

// then
assertThat(actual).isNull();
}
}
4 changes: 4 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ spring:
open-in-view: false
mail:
host: smtp.gmail.com
data:
redis:
port: 6379
host: localhost

dataloader:
enable: false
Expand Down
Loading