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

Setting: 레디스 프로덕션 설정을 분리한다. #108

Merged
merged 3 commits into from
Aug 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion backend-config
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(TicketingException.class)
public ResponseEntity<ErrorResponse<Void>> handleTicketingException(TicketingException e) {
ErrorCode errorCode = e.getErrorCode();
log.warn("예외 발생. 메세지={}", e.getMessage(), e);
return ResponseEntity.status(errorCode.getHttpStatusValue())
.body(ErrorResponse.of(errorCode));
}
Expand All @@ -20,6 +25,7 @@ public ResponseEntity<ErrorResponse<Void>> handleTicketingException(TicketingExc
public ResponseEntity<ErrorResponse<List<ValidationErrorDetail>>>
handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
ErrorResponse<List<ValidationErrorDetail>> errorResponse = ErrorResponse.of(e);
log.info("API 요청 데이터 오류. 메세지={}", e.getMessage());
return ResponseEntity.badRequest().body(errorResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
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.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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Profile({"local", "default"})
@Configuration
public class LettuceConfig {
public class LocalRedisConfig {

@Bean
public LettuceConnectionFactory lettuceConnectionFactory(
private final int port;
private final String host;

public LocalRedisConfig(
@Value("${spring.data.redis.port}") int port,
@Value("${spring.data.redis.host}") String host) {
return new LettuceConnectionFactory(host, port);
this.port = port;
this.host = host;
}

@Bean
public RedissonClient redissonClient() {
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer();
serverConfig.setAddress("redis://" + host + ":" + port);
return Redisson.create(config);
}

@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPort(port);
return new LettuceConnectionFactory(config);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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.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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

@Profile("prod")
@Configuration
public class ProductionRedisConfig {

private final int port;
private final String host;
private final String password;

public ProductionRedisConfig(
@Value("${spring.data.redis.port}") int port,
@Value("${spring.data.redis.host}") String host,
@Value("${spring.data.redis.password}") String password) {
this.port = port;
this.host = host;
this.password = password;
}

@Bean
public RedissonClient redissonClient() {
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer();
serverConfig.setAddress("redis://" + host + ":" + port);
serverConfig.setPassword(password);
return Redisson.create(config);
}

@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPort(port);
config.setPassword(password);
return new LettuceConnectionFactory(config);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
import com.thirdparty.ticketing.support.TestContainerStarter;

@SpringBootTest
class RedissonConfigTest extends TestContainerStarter {
class ProductionRedisConfigTest extends TestContainerStarter {

@Autowired private RedissonClient redissonClient;

@Autowired private StringRedisTemplate redissonRedisTemplate;
@Autowired private StringRedisTemplate redisTemplate;

@Test
void testRedissonClient() {
assertThat(redissonClient).isNotNull();
}

@Test
void testRedissonRedisTemplate() {
assertThat(redissonRedisTemplate).isNotNull();
redissonRedisTemplate.opsForValue().set("testKey", "testValue");
String value = redissonRedisTemplate.opsForValue().get("testKey");
void testLettuceRedisTemplate() {
assertThat(redisTemplate).isNotNull();
redisTemplate.opsForValue().set("testKey", "testValue");
String value = redisTemplate.opsForValue().get("testKey");
assertThat(value).isEqualTo("testValue");
}
}
Loading