-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ server: | |
spring: | ||
# 서브모듈 profile | ||
profiles: | ||
active: dev | ||
active: local | ||
group: | ||
local: | ||
- jpa | ||
|
74 changes: 73 additions & 1 deletion
74
...astructure/src/main/java/org/depromeet/spot/infrastructure/redis/EmbeddedRedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,75 @@ | ||
package org.depromeet.spot.infrastructure.redis; | ||
|
||
public class EmbeddedRedisConfig {} | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.annotation.PreDestroy; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import redis.embedded.RedisServer; | ||
|
||
@Slf4j | ||
@Configuration | ||
@Profile("local | test") | ||
@RequiredArgsConstructor | ||
public class EmbeddedRedisConfig { | ||
|
||
private final RedisProperties redisProperties; | ||
|
||
private RedisServer redisServer; | ||
|
||
@PostConstruct | ||
public void redisServer() throws IOException { | ||
int port = isRedisPortUsed() ? findAvailablePort() : redisProperties.port(); | ||
log.info("embedded redis port = {}", port); | ||
redisServer = new RedisServer(port); | ||
redisServer.start(); | ||
} | ||
|
||
@PreDestroy | ||
public void stopRedis() { | ||
if (redisServer != null) { | ||
redisServer.stop(); | ||
} | ||
} | ||
|
||
private boolean isRedisPortUsed() throws IOException { | ||
Process process = getProcessListeningOnPort(redisProperties.port()); | ||
return isRunningProcess(process); | ||
} | ||
|
||
private Process getProcessListeningOnPort(final int port) throws IOException { | ||
String command = "lsof -i :" + port + " | grep LISTEN"; | ||
String[] shell = {"/bin/sh", "-c", command}; | ||
return Runtime.getRuntime().exec(shell); | ||
} | ||
|
||
private boolean isRunningProcess(Process process) { | ||
try (BufferedReader input = | ||
new BufferedReader(new InputStreamReader(process.getInputStream()))) { | ||
String line = input.readLine(); | ||
if (line == null) { | ||
return false; | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return true; | ||
} | ||
|
||
private int findAvailablePort() throws IOException { | ||
for (int port = 10000; port <= 65535; port++) { | ||
Process process = getProcessListeningOnPort(port); | ||
if (!isRunningProcess(process)) { | ||
return port; | ||
} | ||
} | ||
throw new IllegalArgumentException("10000 ~ 65535 사이에서 사용 가능한 포트가 없습니다."); | ||
} | ||
} |