Skip to content

Commit 67f67cc

Browse files
committed
Ref: 내장 Redis 설정 개선
1 parent 00343e2 commit 67f67cc

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/main/java/com/back/global/config/EmbeddedRedisConfig.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import redis.embedded.RedisServerBuilder;
99

1010
import java.io.IOException;
11+
import java.net.ServerSocket;
1112

1213
/**
1314
* Embedded Redis 설정 클래스
@@ -20,17 +21,19 @@
2021
public class EmbeddedRedisConfig {
2122

2223
private RedisServer redisServer;
24+
private int port;
2325

24-
public EmbeddedRedisConfig() throws IOException {
25-
this.redisServer = new RedisServerBuilder()
26-
.port(6379)
26+
@PostConstruct
27+
public void startRedis() throws IOException {
28+
port = findAvailablePort();
29+
redisServer = new RedisServerBuilder()
30+
.port(port)
2731
.setting("maxheap 256M")
2832
.build();
29-
}
30-
31-
@PostConstruct
32-
public void startRedis() {
3333
redisServer.start();
34+
35+
// 동적으로 찾은 포트를 Spring 환경 변수에 반영
36+
System.setProperty("spring.data.redis.port", String.valueOf(port));
3437
}
3538

3639
@PreDestroy
@@ -39,4 +42,14 @@ public void stopRedis() {
3942
redisServer.stop();
4043
}
4144
}
45+
46+
// 사용 가능한 포트를 찾는 유틸리티 메서드
47+
private int findAvailablePort() {
48+
try (ServerSocket socket = new ServerSocket(0)) {
49+
socket.setReuseAddress(true);
50+
return socket.getLocalPort();
51+
} catch (IOException e) {
52+
throw new IllegalStateException("No available port found", e);
53+
}
54+
}
4255
}

src/main/resources/application-test.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ spring:
55
username: sa
66
password:
77

8-
data:
9-
redis:
10-
host: localhost
11-
port: 6379
12-
138
jwt:
149
secret: test-jwt-secret-key-12345678901234567890
1510
access-token-expiration: ${JWT_ACCESS_TOKEN_EXPIRATION:1800} # 30분 (초 단위)

0 commit comments

Comments
 (0)