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

[fix/NO_JIRA] local & test 환경에서 embedded redis를 이용해 redisson 쓰도록 수정 #172

Merged
merged 5 commits into from
Aug 27, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,6 @@ application-jwt.yml
application-oauth.yml
application-sentry.yml
application-aws.yaml

# 민성 레디스 바이너리 파일
redis-server-7.2.3-mac-arm64
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
package org.depromeet.spot.infrastructure.redis;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Objects;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;

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 static final String REDISSON_HOST_PREFIX = "redis://localhost:";
private static final int REDIS_DEFAULT_PORT = 6379;

private RedisServer redisServer;
private final int embeddedRedisPort;

public EmbeddedRedisConfig() {
this.embeddedRedisPort =
isPortInUse(REDIS_DEFAULT_PORT) ? findAvailablePort() : REDIS_DEFAULT_PORT;
log.info("embedded redis port = {}", embeddedRedisPort);
}

@PostConstruct
public void redisServer() {
int port = isRedisPortAvailable() ? findAvailablePort() : redisProperties.port();
log.info("embedded redis port = {}", port);
redisServer = new RedisServer(port);
redisServer.start();
// redisServer = new RedisServer(embeddedRedisPort);
if (isArmMac()) {
redisServer = new RedisServer(getRedisFileForArcMac(), embeddedRedisPort);
} else {
redisServer =
RedisServer.builder()
.port(embeddedRedisPort)
.setting("maxmemory 128M") // maxheap 128M
.build();
}
try {
redisServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}

@PreDestroy
Expand All @@ -39,11 +63,33 @@ public void stopRedis() {
}
}

private boolean isRedisPortAvailable() {
return isAvailablePort(redisProperties.port());
@Bean
public RedissonClient redissonClient() {
Config redissonConfig = new Config();
redissonConfig.useSingleServer().setAddress(REDISSON_HOST_PREFIX + embeddedRedisPort);
return Redisson.create(redissonConfig);
}

/**
* 현재 시스템이 ARM 아키텍처를 사용하는 MAC인지 확인 System.getProperty("os.arch") : JVM이 실행되는 시스템 아키텍처 반환
* System.getProperty("os.name") : 시스템 이름 반환
*/
private boolean isArmMac() {
return Objects.equals(System.getProperty("os.arch"), "aarch64")
&& Objects.equals(System.getProperty("os.name"), "Mac OS X");
}

/** ARM 아키텍처를 사용하는 Mac에서 실행할 수 있는 Redis 바이너리 파일을 반환 */
private File getRedisFileForArcMac() {
try {
return new ClassPathResource("binary/redis/redis-server-7.2.3-mac-arm64").getFile();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private boolean isAvailablePort(final int port) {
private boolean isPortInUse(final int port) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("localhost", port), 200);
return true;
Expand All @@ -54,7 +100,7 @@ private boolean isAvailablePort(final int port) {

private int findAvailablePort() {
for (int port = 10000; port <= 65535; port++) {
if (!isAvailablePort(port)) {
if (!isPortInUse(port)) {
return port;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import lombok.RequiredArgsConstructor;

@Configuration
@Profile("dev | prod")
@RequiredArgsConstructor
public class RedissonConfig {

Expand Down
Loading