Skip to content

Commit

Permalink
fix: 테스트 용도의 Redis 분리 #61
Browse files Browse the repository at this point in the history
  • Loading branch information
mungsil committed Nov 28, 2024
1 parent 3f2f690 commit 74d4394
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'it.ozimov:embedded-redis:0.7.3'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// Spotify
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.munecting.api.global.config;

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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import redis.embedded.RedisServer;

@Configuration
@Profile("test")
public class EmbeddedRedisConfig {

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

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

private RedisServer redisServer;

@PostConstruct
public void startEmbeddedRedis() {
redisServer = new RedisServer(port);
redisServer.start();
}

@PreDestroy
public void stopEmbeddedRedis() {
if (redisServer != null) {
redisServer.stop();
}
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Profile({"prod","dev"})
public class RedisConfig {

@Value("${spring.data.redis.host}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
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;

@Configuration
@Profile({"prod","dev"})
public class RedissonConfig {

@Value("${spring.data.redis.host}")
Expand All @@ -29,4 +31,5 @@ private Config createRedissonConfig() {
config.useSingleServer().setAddress(REDISSON_HOST_PREFIX + redisHost + ":" + redisPort);
return config;
}

}
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ spring:
path: /h2-console
data:
redis:
host: 127.0.0.1
port: 6379
host: localhost
port: 6378
cloud:
aws:
s3:
Expand Down

0 comments on commit 74d4394

Please sign in to comment.