๐ Spring Boot(Maven) profile ์ด์ & ๊ฐ๋ฐ DB ๋ถ๋ฆฌ
(ํ๋ฆฐ ๋ด์ฉ์ด ์์์๋ ์์ผ๋ ๋นํ์ ์ผ๋ก ๋ด์ฃผ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค..! ๐)
์๋ ํ์ธ์! ์ด๋ฒ์ ์ ๋ฆฌํ ๋ด์ฉ์ ์คํ๋ง ๋ถํธ์์ ์ด์ & ๊ฐ๋ฐ ๋ถ๋ฆฌ ๋ฐ AWS EC2์ ๋ฐฐํฌ ์ ๋๋ค.
์ค๋ฌด์์๋ ์ค์ ์๋น์ค๋๋ DB์ ๊ฐ๋ฐ์ค์ธ DB์ ์ ๋ณด๋ ๋ค๋ฅด๊ฒ ๊ด๋ฆฌ๋ฅผ ํ๋๋ฐ ์ด๋ฅผ ์ํด ์คํ๋ง ๋ถํธ์์ profile์ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐฐํฌํ ๋ ์ด์๊ณผ ๊ฐ๋ฐ์ ๋ถ๋ฆฌํด์ ๋ฐฐํฌํ ์ ์๋๋ก ์งํํด๋ณด๊ฒ ์ต๋๋ค.
๋ํ ์ ๋ ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ AWS EC2 ํ๊ฒฝ์์ ์ด์, ๋ฐฐํฌ๋ฅผ ์งํํ๊ณ ์์ต๋๋ค.
๋ก์ปฌ์์ ๊ฐ๋ฐ ํ ๋ฐฐํฌ๋ฅผ ํ๊ธฐ ์ํด GitHub์ ์ฝ๋๋ฅผ pushํ ๋ค AWS์์ pull์ ํ๊ณ ๋ฐฐํฌ๋ฅผ ์งํํ๊ฒ ๋๋๋ฐ์, ์ด๋ฌํ ๊ณผ์ ์์ ๋ฏผ๊ฐํ ์ ๋ณด๋ค(DB์ url, id, pw ๋ฑ)์ GitHub์ ์ฝ๋๋ฅผ ๊ณต๊ฐํ๊ฒ ๋๋ค๋ฉด ์ด๋ฅผ ์ ์ฉํ ์ ์์ผ๋ฏ๋ก ๊ณต๊ฐ๋ฅผ ํด์๋ ์๋ฉ๋๋ค.
๋ชฉ์ฐจ๋ ๊ฐ๋ตํ๊ฒ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- โจ๏ธ properties ํ์ผ ๋ถ๋ฆฌ
- โจ๏ธ ์ ํ๋ฆฌ์ผ์ด์ ์คํ
- โจ๏ธ ์ ํ๋ฆฌ์ผ์ด์ ์คํ์ค๋ฅ
- โจ๏ธ AWS EC2์ ์ ํ๋ฆฌ์ผ์ด์ ๋ฐฐํฌ
โจ๏ธ properties ํ์ผ ๋ถ๋ฆฌ
๊ธฐ์กด application.properties ํ์ผ์ ์ด์ & ๊ฐ๋ฐ๋ก ๋ถ๋ฆฌํ์ฌ ํ์ผ์ ์์ฑํฉ๋๋ค.
application-{name}.properties
์คํ๋ง ๋ถํธ ํ๋ก์ ํธ ๋ง๋ค๋ ์๋์ผ๋ก ์์ฑ๋๋ ๊ฒฝ๋ก์ธ resources ๋๋ ํ ๋ฆฌ ํ์์ ๊ฐ์ด ์์ฑํฉ๋๋ค.
(์ ๋ ๊ฐ๋ฐ๊ณ๋ฅผ dev, ์ด์๊ณ๋ฅผ prod ๋ผ๋ ๋ค์ด๋ฐ์ผ๋ก ํ์ผ์ ์์ฑํด์ฃผ์์ต๋๋ค.)
๊ทธ ํ ๊ฐ DB์ ์ ๋ณด๋ฅผ ๋ถ๋ฆฌํด์ฃผ์๋๋ฐ์, ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
application.properties
์ ํ์ผ์๋ ํ๋ก์ ํธ์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ค์ ๋ค์ ์์ฑํฉ๋๋ค.
application-dev.properties
dev ํ์ผ์๋ ๊ฐ๋ฐ๊ณ์ ์ ๋ณด๋ฅผ ์์ฑํฉ๋๋ค.
application-prod.properties
prod ํ์ผ์๋ ์ด์๊ณ์ ์ ๋ณด๋ฅผ ์์ฑํฉ๋๋ค.
(์ ๋ AWS RDS๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.)
โจ๏ธ ์ ํ๋ฆฌ์ผ์ด์ ์คํ
์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ ๋๋ ์คํ๋ง๋ถํธ์ ์คํ ํ๊ฒฝ์์ Active profiles์ properties์ ๋ง๊ฒ ์ค์ ์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
(IntelliJ) ์๋จ์ ์คํ ํ๊ฒฝ ๋ถ๋ถ์์ Edit Configurations(Run/Debug Configurations)๋ฅผ ํด๋ฆญํฉ๋๋ค.
Configuration > Active profiles ์์ prod๋ก ์ค์ ์ ํฉ๋๋ค.
๊ทธ ํ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ฉด ์ด์ DB๋ก ์ ์์ ์ผ๋ก ์ ์์ด ๋๋๊ฑธ ํ์ธํ ์ ์์ต๋๋ค.
โจ๏ธ ์ ํ๋ฆฌ์ผ์ด์ ์คํ์ค๋ฅ
๋ง์ฝ Active profiles์ ์๋ฌด๋ฐ ๊ฐ์ ์ง์ ํ์ง ์๊ฑฐ๋, ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ์ค์ ํ๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ ์์ ์ผ๋ก ์คํ๋์ง ์์ต๋๋ค.
์๋ฌด๋ฐ ๊ฐ์ ์ง์ ํ์ง ์์ ๊ฒฝ์ฐ
๋ค๋ฅธ ์ด๋ฆ์ ์ง์ ํ ๊ฒฝ์ฐ
โป ์ ์ค์ ์ด ๋๋๋ฉด .gitignore ํ์ผ์ ํตํด DB ์ ๋ณด๋ฅผ GitHub์ ์ฌ๋ฆฌ์ง ์๋๋ก ์ค์ ์ ํด์ผํฉ๋๋ค.
https://steady-hello.tistory.com/44
โจ๏ธ AWS EC2์ ์ ํ๋ฆฌ์ผ์ด์ ๋ฐฐํฌ
์ด์ ์ ์ ์ด์ ํ๊ฒฝ์ธ AWS EC2์์ ์ด์ DB๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐฐํฌํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
(AWS ์ ์ ๋ฐ GitHub ๊ด๋ จ๋ ์ธ๋ถ ์ฌํญ์ ์๋ตํ๋๋ก ํ๊ฒ ์ต๋๋ค.)
๋๋ต์ ์ธ ํ๋ก์ธ์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค
๋ก์ปฌ ๊ฐ๋ฐ & GitHub Push -> AWS์์ pull -> gitignoreํ ํ์ผ๋ค(prod, dev properties) ๋ณต์ฌ -> Maven Build -> ๋ฐฐํฌ
๋จผ์ application.properties ํ์ผ์ ์๋ ๋ด์ฉ์ ์ถ๊ฐํฉ๋๋ค.
spring.profiles.active=@spring.profiles.active@
๋ค์์ผ๋ก ๋ฉ์ด๋ธ์ผ๋ก ๋น๋ํ ๋ ๊ฐ๋ฐ ๋ฐ ์ด์ ์๋ฒ๋ณ๋ก ๋ฐ๋ก ๋น๋๋ฅผ ํ ์ ์๋๋ก pom.xml์ ๋ค์ ์ค์ ์ ์ถ๊ฐํฉ๋๋ค.
<!-- profile ์ถ๊ฐ: dev, prod -->
<profiles>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>
<!-- profile ์์ -->
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources-${environment}</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
properties์ ์ ๋ณด๋ฅผ ์ฝ๊ธฐ ์ํด filtering์ true๋ก ํ์ฑํ ํฉ๋๋ค(default = false)
โป Maven - pom - profiles์ ๋ํ ์ค๋ช ์ ์๋ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์ !
https://maven.apache.org/guides/introduction/introduction-to-profiles.html
Maven – Introduction to build profiles
Introduction to Build Profiles Apache Maven goes to great lengths to ensure that builds are portable. Among other things, this means allowing build configuration inside the POM, avoiding all filesystem references (in inheritance, dependencies, and other pl
maven.apache.org
๊ทธ ํ resources ํ์์ application-prod.properties ํ์ผ์ ์์ฑํฉ๋๋ค.
(ํ์ฌ AWS๋ ์ด์ ์๋ฒ์ ๋๋ค.)
๋ค์ ๋ช ๋ น์ด๋ฅผ ํตํด ๋ฉ์ด๋ธ ํจํค์ง์ ์งํํฉ๋๋ค.(์คํ ํ์ผ jar ์์ฑ)
./mvnw clean package -Pprod
-P ์ต์ ์ผ๋ก ํ๋กํ์ผ(profile)์ ์ ํํ ์ ์์ต๋๋ค.
์ฆ ์ ๋ช ๋ น์ด๋ฅผ ํตํด id๊ฐ prod์ธ profile์ ์ ํํฉ๋๋ค.
(environment = prod)
โป ํน์ ์๋์ ๊ฐ์ด ๊ถํ์ด ์์ ๊ฒฝ์ฐ(Permission denied) chmod ๋ช ๋ น์ด๋ก ๊ถํ์ ์์ ํฉ๋๋ค.
$ chmod +x mvnw
- mvnw ํ์ผ์ ๋ํด ์คํ ๊ถํ(+x) ๋ถ์ฌ
๊ทธ ํ ํจํค์ง์ ํ๋ฉด ์๋์ ๊ฐ์ด ์คํ์ด ๋๊ณ , BUILD SUCCESS ๋ผ๋ ๋ฌธ๊ตฌ๊ฐ ๋ณด์ด๋ฉด ์ ์์ ์ผ๋ก ๋น๋๊ฐ ๋์์ต๋๋ค.
์๋ ๋ช ๋ น์ด๋ฅผ ํตํด ์์ฑ๋ target ๋๋ ํ ๋ฆฌ์ .jarํ์ผ์ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐฐํฌํฉ๋๋ค.
java -jar -Dspring.profiles.active=prod jar file
java -jar -Dspring.profiles.active=prod oneline-course-0.0.1-SNAPSHOT.jar
jar ํ์ผ์ ์คํํ๋ฉด ์์ ๊ฐ์ด ์ ์์ ์ผ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋ฐฐํฌ๊ฐ ๋ฉ๋๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฐฐํฌํ๊ณ URL๋ก ์ ์์ ํด๋ณด๋ฉด(AWS EC2์ ํ๋ ฅ์ IP) ์ ์์ ์ผ๋ก ์ ์์ด ๋๋๊ฑธ ํ์ธํ ์ ์์ต๋๋ค. ๐
References
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] - @JsonProperty, @JsonNaming (2) | 2021.09.02 |
---|---|
Spring AOP - (1) ํ๋ก์ ํจํด, ๋ฐ์ฝ๋ ์ดํฐ ํจํด (0) | 2021.08.27 |
JUnit - @ParameterizedTest, @ValueSource, @CsvSource, @MethodSource ์ด๋ ธํ ์ด์ (4) | 2021.06.08 |
[Spring] ๊ฐ์ฒด๋ณต์ฌ BeanUtils.copyProperties() & ์๋ ์๋ฆฌ (0) | 2021.02.20 |
[Spring] Maven ๋ฉํฐ๋ชจ๋ ํ๋ก์ ํธ (0) | 2021.02.09 |
๋๊ธ