๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring Boot Maven profile ์šด์˜ & ๊ฐœ๋ฐœ DB ๋ถ„๋ฆฌ(AWS EC2)

by ์ฃผ๋ฐœ2 2021. 6. 18.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž 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

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€