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

프로파일 분리 완료 #63

Merged
merged 12 commits into from
Nov 29, 2024
Merged
22 changes: 15 additions & 7 deletions .github/workflows/develop-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ jobs:
distribution: 'temurin'

# 환경 변수 파일 생성
- name: Set environment values for main application
- name: Set environment values for common
run: |
cd ./src/main/resources
touch ./env.properties
echo "${{ secrets.ENV }}" > ./env.properties
touch ./env-common.properties
echo "${{ secrets.ENV_COMMON }}" > ./env-common.properties
shell: bash

- name: Set environment values for prod
run: |
cd ./src/main/resources
touch ./env-prod.properties
echo "${{ secrets.ENV_PROD }}" > ./env-prod.properties
shell: bash

- name: Set environment values for test
run: |
cd ./src/test/resources
touch ./env.properties
echo "${{ secrets.TEST_ENV }}" > ./env.properties
cd ./src/main/resources
touch ./env-test.properties
echo "${{ secrets.TEST_ENV }}" > ./env-test.properties
shell: bash

# Gradle에 권한 부여
Expand All @@ -49,7 +56,8 @@ jobs:
- name: Make zip file
run: |
mkdir develop-deploy
cp ./src/main/resources/env.properties ./develop-deploy/.env
cp ./src/main/resources/env-common.properties ./develop-deploy/.env.common
cp ./src/main/resources/env-prod.properties ./develop-deploy/.env.prod
cp ./docker-compose.blue.yml ./develop-deploy/
cp ./docker-compose.green.yml ./develop-deploy/
cp ./appspec.yml ./develop-deploy/
Expand Down
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ java {
}

configurations {
all {
exclude group: "commons-logging", module: "commons-logging"
exclude group: "org.slf4j", module: "slf4j-simple"
}
compileOnly {
extendsFrom annotationProcessor
}
Expand Down Expand Up @@ -53,6 +57,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
5 changes: 5 additions & 0 deletions docker-compose.blue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ services:
ports:
- "8081:8080"
container_name: spring-blue
environment:
- SPRING_PROFILES_ACTIVE=prod
env_file:
- .env.common
- .env.prod
networks:
- munecting-network

Expand Down
5 changes: 5 additions & 0 deletions docker-compose.green.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ services:
ports:
- "8082:8080"
container_name: spring-green
environment:
- SPRING_PROFILES_ACTIVE=prod
env_file:
- .env.common
- .env.prod
networks:
- munecting-network

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/munecting/api/global/config/DevConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.munecting.api.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;

@Configuration
@Profile("dev")
@PropertySource(value = {"classpath:env-common.properties", "classpath:env-dev.properties"})
public class DevConfig {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 파일은 테스트용 redis의 환경변수 값을 주입하기 위한 클래스인가요?
만약 그렇다면 아래쪽의 RedisConfig와 차이점이 무엇인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 파일은 말씀해주신 테스트용 redis의 환경변수 값을 주입하기 위한 목적도 가지고 있지만, 실제 레디스 서버 대신 EmbeddedRedis를 사용하기 위함이기도 합니다.

두 Config는 어느 환경에 위치한 레디스를 사용해야하는가에 따라 나누었는데요, 아시다시피 개발 환경과 배포 환경에서는 각각 로컬 또는 EC2 컨테이너로 존재하는 실제 Redis 서버를 사용하고 있어요.
따라서 RedisConfig는 외부 Redis 서버를 사용하기 위한 설정을 수행하고 있습니다.

반면 테스트 환경에서는 실제 Redis 서버 대신 내장된 레디스 서버(EmbeddedRedis)를 사용하려고 합니다.
그러므로 EmbeddedRedis를 사용하기 위한 설정을 따로 분리하였습니다.

  • 이해를 돕기 위한 추가 설명
    @PostConstruct
    public void startEmbeddedRedis() {
        redisServer = new RedisServer(port);
        redisServer.start();
    }
    
    RedisServer를 사용하기 위해 redis.embedded.RedisServer를 임포트하고 있는 것을 확인하실 수 있어요!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이해했습니다! 답변 감사합니다

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
@@ -1,12 +1,12 @@
package com.munecting.api.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({
@PropertySource("classpath:env.properties")
})
public class PropertyConfig {
@Profile("prod")
@PropertySource(value = {"classpath:env-common.properties","classpath:env-prod.properties"})
public class ProdConfig {
}
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;
}

}
12 changes: 12 additions & 0 deletions src/main/java/com/munecting/api/global/config/TestConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.munecting.api.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@Profile("test")
@PropertySource(value = {"classpath:env-common.properties", "classpath:env-test.properties"})
public class TestConfig {
}
110 changes: 87 additions & 23 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ server:
spring :
application:
name: munectingV4
profiles:
active: dev
mvc:
path match:
matching-strategy: ant_path_matcher
Expand All @@ -17,32 +19,16 @@ spring :
format_sql: true
jdbc:
timezone: Asia/Seoul

datasource:
url: jdbc:${DATASOURCE}://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: org.postgresql.Driver
hikari:
initialization-fail-timeout: 3600

data:
redis:
host: ${REDIS_HOST}
port: 6379
# password: 1234


security:
cors:
allowed-origins: ${CORS_ALLOWED_ORIGINS:*}
allowed-methods: ${CORS_ALLOWED_METHODS:GET,POST,PUT,PATCH,DELETE}
allowed-headers: ${CORS_ALLOWED_HEADERS:*}
allowed-origins: "*"
allowed-methods: GET,POST,PUT,PATCH,DELETE
allowed-headers: "*"
path-pattern: "/**"

auth:
header: ${AUTH_HEADER:Authorization}
prefix: ${AUTH_PREFIX:Bearer }
header: Authorization
prefix: Bearer

oauth:
kakao:
Expand All @@ -62,14 +48,12 @@ spring :
maxRequestSize: 10MB # 한 번에 최대 업로드 가능 용량
resolve-lazily: true


cloud:
aws:
s3:
bucket: munecting
path:
profile-image: profileImage

region:
static: ap-northeast-2
stack:
Expand All @@ -91,3 +75,83 @@ jwt:

kakao-map:
api-key: ${KAKAO_MAP_API_KEY}

---
spring:
config:
activate:
on-profile: prod

datasource:
url: jdbc:${DATASOURCE}://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: org.postgresql.Driver
hikari:
initialization-fail-timeout: 3600
jpa:
hibernate:
ddl-auto: validate
data:
redis:
host: ${REDIS_HOST}
port: 6379

---
spring:
config:
activate:
on-profile: dev
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/munecting
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
hikari:
initialization-fail-timeout: 3600
jpa:
hibernate:
ddl-auto: update
data:
redis:
host: 127.0.0.1
port: 6379
cloud:
aws:
s3:
bucket: munecting-dev
path:
profile-image: profileImage
---
spring:
config:
activate:
on-profile: test
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:~/munecting
username: sa
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
show_sql: true
format_sql: true
h2:
console:
enabled: true
path: /h2-console
data:
redis:
host: localhost
port: 6378
cloud:
aws:
s3:
bucket: munecting-test
path:
profile-image: profileImage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

package com.munecting.api;

import org.junit.jupiter.api.Test;
Expand All @@ -14,4 +14,4 @@ void contextLoads() {
}

}
*/

Loading