๐ ์ค๋ ๋ฐฐ์ด ๋ด์ฉ!
- ์ ํ๋ฆฌ์ผ์ด์ ์์ค ์ฝ๋๋ฅผ ๋น๋
- ์ ํ๋ฆฌ์ผ์ด์ ์คํ
- ํ๋กํ์ผ ์ ์ฉ
- ์ ํ๋ฆฌ์ผ์ด์ ๋ฐฐํฌ
โ๏ธ Spring Boot ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์์ค ์ฝ๋๋ฅผ ๋น๋ํ๋ ๋ฐฉ๋ฒ
โ IntelliJ IDE๋ฅผ ์ด์ฉํ ๋น๋
- Spring Boot์ Gradle ๋น๋ ํด์ ์ด์ฉํ์ฌ Gradle task ๋ช
๋ น์ผ๋ก ๋น๋ํ๋ ๋ฐฉ๋ฒ
- ์ฐ์ธก ์๋จ์
[Gradle]
์๋์ฐ ํญ ํด๋ฆญ ํ๋ก์ ํธ ๋ช > Tasks > build
์:bootJar
or:build task
๋๋ธ ํด๋ฆญ
- ์ฐ์ธก ์๋จ์
build/libs
๋๋ ํ ๋ฆฌ์ ์๊ธด Jar ํ์ผ๋ก ์ ํ๋ฆฌ์ผ์ด์ ์คํ ๊ฐ๋ฅ
๐ก :build vs :bootJar
- :build ํ์คํฌ ์คํํ๋ฉด
โ Gradle์์ ๋น๋์ ๊ด๋ จ๋ ๋ชจ๋ task๋ค์ ์คํ์ํด
โ ์คํ ๊ฐ๋ฅํ Jar ํ์ผ ์ด์ธ์ plain Jar ํ์ผ ํ๋๋ฅผ ๋ ์์ฑ
โ - :bootJar ํ์คํฌ ์คํํ๋ฉด
โ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํ ๊ฐ๋ฅํ Jar(Executable Jar)ํ์ผ์ ์์ฑํ๊ธฐ ์ํ task๋ง ์คํ์ํด
โ ๋จ์ํ Executable Jar ํ์ผ๋ง ํ์ํ๋ค๋ฉด :bootJar task๋ฅผ ์คํ
โ Gradle Task๋ฅผ ์ด์ฉํ ๋น๋
1. Windows ํฐ๋ฏธ๋์ด๋ Git Bash๋ก ํ๋ก์ ํธ๊ฐ ์์นํด ์๋ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ก ์ด๋
Ex. cd desktop/projects/be-template-build
2. ๋ช
๋ น์ ์
๋ ฅํ์ฌ ๋น๋ ์งํ
- Windows ํฐ๋ฏธ๋์ ๊ฒฝ์ฐ, ์๋ ๋ช
๋ น ์
๋ ฅํด์ ์ ํ๋ฆฌ์ผ์ด์
๋น๋ ์งํ
โ.\gradlew bootJar
- Git Bash์ ๊ฒฝ์ฐ
โ$ ./gradlew build
( ์๋๋ Git Bash๋ก ๋น๋ํ ๊ฒฝ์ฐ์ )
3. ๋น๋๊ฐ ์ ์์ ์ผ๋ก ์ข
๋ฃ๋๋ฉด build/libs
๋๋ ํ ๋ฆฌ์ Jar ํ์ผ ์์ฑ๋จ
โ๏ธ ์ ํ๋ฆฌ์ผ์ด์ ์คํ
- Windows ํฐ๋ฏธ๋์ด๋ Git Bash๋ก ์์ฑ๋ Jar ํ์ผ์ด ์๋ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ก ์ด๋
Ex.cd desktop/projects/be-template-build/build/libs
(build/libs
๊ฐ jar ํ์ผ์ด ์์ฑ๋ ๋๋ ํ ๋ฆฌ์ ! ) java -jar Jar ํ์ผ๋ช .jar
๋ช ๋ น ์ ๋ ฅํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ์๋ฒ ํ๊ฒฝ์์ ์คํ !
Ex.java -jar section3-week3-template-build-0.0.1-SNAPSHOT.jar
( ์๋๋ Git Bash๋ก ์คํํ ๊ฒฝ์ฐ์ )
โ intellij์์ ์ ํ๋ฆฌ์ผ์ด์
์ด ์คํ์ค์ด๋ผ๋ฉด ๊ฐ์ port ๋ฒํธ๋ฅผ ์ฌ์ฉํ๊ณ ์์ด์
์๋์ ๊ฐ์ด ์คํ์ ์คํจํ๋ intellij์ ์คํ์ ๋ฉ์ถ ํ Git Bash์์ ์คํํ ๊ฒ !!
๐ก But, ํ์ฌ ์ฐ๋ฆฌ๊ฐ ๋ง๋ ํ๋ก์ ํธ๋ค์ ๋ก์ปฌ ํ๊ฒฝ์์ ์ธ๋ฉ๋ชจ๋ฆฌ DB์ธ H2 ์ฌ์ฉ์ค !
โ
์๋ฒ ํ๊ฒฝ์์ ๋ฐฐํฌ / ์ด์ ํ๋ค๋ฉด ์ธ๋ฉ๋ชจ๋ฆฌ DB๋ก ํ๋ฉด ์๋จ
( ์ฌ์์ ํ ๊ฒฝ์ฐ ๋ฐ์ดํฐ ๋ชจ๋ ์ด๊ธฐํ )
โ
๋ก์ปฌ ํ๊ฒฝ์์ ์ฌ์ฉ์์ ์๋ฒ์ฉ jar ํ์ผ ๋น๋ ์ application.yml ํ์ผ์ DB ์ ๋ณด๋ฅผ ๊ณ์ ๋ฐ๊ฟ๊ฐ๋ฉฐ ๋น๋ํ๋ ๊ฒ์ ์์ฃผ ๋นํจ์จ์
โ
โ ํ๋กํ์ผ ๊ธฐ๋ฅ ์ฌ์ฉ
[IntelliJ IDE ์ด์ธ์ ๋ฐฉ๋ฒ์ผ๋ก Spring Boot ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์
์ ์คํํ๋ ๋ฐฉ๋ฒ ์ฐธ๊ณ ]
https://docs.spring.io/spring-boot/docs/current/reference/html/cli.html#cli.using-the-cli.run
https://spring.io/guides/gs/spring-boot/
โ๏ธ ํ๋กํ์ผ ์ ์ฉํ๊ธฐ
- ๋ก์ปฌ ํ๊ฒฝ์์ ๊ฐ๋ฐํ ๋์ ์๋ฒ์์ ๋ฐฐํฌ / ์ด์ํ ๊ฒฝ์ฐ์ DB๋ฅผ ๋ค๋ฅด๊ฒ ํ๊ธฐ ์ํด ํ๋กํ์ผ์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ ์ฉ
- IntelliJ IDE ๊ฐ์ ๊ฐ๋ฐ ํ๊ฒฝ์์ ํ
์คํธ๋ฅผ ์ํด ํ๋กํ์ผ์ ๋ณ๊ฒฝํด์ผ ํ ๊ฒฝ์ฐ๋ ์์ง๋ง
ํ๋กํ์ผ ๊ธฐ๋ฅ์ ์ฃผ๋ก ๋น๋๋ ์คํ ํ์ผ์ ์ด๋ ํ๊ฒฝ์์ ์คํํ ์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ ๋ ์ฌ์ฉ
โ ๋น๋ ํ ์์ฑ๋๋ ์ ํ๋ฆฌ์ผ์ด์ ์คํ ํ์ผ์ ๋ํ ์คํ ํ๊ฒฝ์ ์์ฝ๊ฒ ์ง์ ๊ฐ๋ฅ
[์ฐธ๊ณ ]
https://www.baeldung.com/spring-profiles
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles
โ IntelliJ IDE์์ ํ๋กํ์ผ ์ ์ฉ
1. ์๋์ ๊ฐ์ด application.yml ํ์ผ ๋๊ฐ๋ฅผ ๋ ๋ง๋ค๊ธฐ
application.yml
โ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํ ํ๊ฒฝ์ ์๊ด์์ด ๊ณตํต์ผ๋ก ์ฌ์ฉํ๋ ์ ๋ณด๋ค ์ค์ application-local.yml
โ ๋ก์ปฌ ํ๊ฒฝ์์ ์ฌ์ฉํ๋ ์ ๋ณด๋ค ์ค์
( ์๋application.yml
์ ์๋ ์ ๋ณด๋ค์ ๋ถ์ฌ๋ฃ๊ธฐ )application-server.yml
โ ์๋ฒ ํ๊ฒฝ์์ ์ฌ์ฉํ๋ ์ ๋ณด๋ค ์ค์
( ํ๋กํ์ผ์ ์ง์ ํ๋ ๊ฐ์ฅ ์์ฌ์ด ๋ฐฉ๋ฒ์ ์์ฒ๋ผ -
๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋กํ์ผ๋ช
์ yml ํ์ผ ์ด๋ฆ์์ ํฌํจํ๋ ๊ฒ ! )
2. Intellij IDEA ์์ ์ ํ๋ฆฌ์ผ์ด์
์คํ ํ์ผ์ด ์์นํ ์
๋ ํธ ๋ฐ์ค๋ฅผ ํด๋ฆญํ ํ, [Edit Configurations]
ํด๋ฆญ
3. ํ์ฑํํ ํ๋กํ์ผ(Active Profile)์ local
๋ก ์ง์ ํ๋ค๋ฉด,--spring.profiles.active=local
์
๋ ฅํ์ฌ ์ค์
( ์ฌ๊ธฐ์ local
์ application-local.yml
ํ์ผ๋ช
์์์ local
์ ๊ฐ๋ฆฌํด )
( ๊ทธ๋ฅ local
์ด๋ผ๊ณ ๋ง ์์ฑํด๋ ๊ฐ๋ฅ )
4. ์ ํ๋ฆฌ์ผ์ด์
์คํ ํ ๋ก๊ทธ๋ฅผ ๋ณด๋ฉด, local
ํ๋กํ์ผ์ด ํ์ฑํ ๋ ๊ฒ์ ํ์ธํ ์ ์์
โ๏ธ @Profile ์ ๋ํ ์ด์ ์ ์ถ๊ฐํด ํ๋กํ์ผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์
- ํด๋์ค ๋ ๋ฒจ์ ์ถ๊ฐ
โ @Profile("local")
โ ๋ก์ปฌ์ผ ๋๋ง ์๋ํ๊ฒ
โ @Profile(value = {"local", "server"})
โ ๋ก์ปฌ์ด๊ฑฐ๋ ์๋ฒ์ผ ๋๋ง ์๋ํ๊ฒ
โ
( ์์ ๊ฐ์ด ์ฌ์ฉ )
โ ๋น๋๋ ์คํ ํ์ผ์ ํ๋กํ์ผ ์ ์ฉ
- Windows ํฐ๋ฏธ๋์ด๋ Git Bash๋ก Jar ํ์ผ์ด ์๋ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก์์
Jar ์ ์คํ์์ผฐ๋java -jar Jar ํ์ผ๋ช .jar
๋ช ๋ น์ Intellij IDEA ์์ ํ๋กํ์ผ ์ ์ฉํ๋ ๊ฒ๊ณผ ๋์ผํ๊ฒ--spring.profiles.active=local
์ค์ ์ถ๊ฐํด์ฃผ๋ฉด ๋จ
โ
Ex.java -jar section3-week3-template-build-0.0.1-SNAPSHOT.jar --spring.profiles.active=local
( ์๋๋ Git Bash๋ก ์คํํ ๊ฒฝ์ฐ์ )
โ๏ธ ์ ํ๋ฆฌ์ผ์ด์ ๋ฐฐํฌ
( Spring Boot ๊ธฐ๋ฐ์ Executable Jar ํ์ผ์ ์๋ฒ์ ๋ฐฐํฌํ๋ ๋ฐฉ๋ฒ )
โ ์ ํต์ ์ธ ๋ฐฐํฌ ๋ฐฉ๋ฒ
- ํ์ค ์ ๋์ค ํด์ ์ด์ฉํด์ ์๋ฒ๋ก ๊ฐ๋จํ ์ ์ก
( ์๋ฒ๋ก ์ ์ก๋ Jar ํ์ผ์ JVM์ด ์ค์น๋ ํ๊ฒฝ์ด๋ผ๋ฉด ์ด๋์๋ ์์ฝ๊ฒ ์คํ ๊ฐ๋ฅ )
Ex.scp
/sftp
โ
[scp ์ฐธ๊ณ ]
โ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ ์ํ ๋ฐฐํฌ ๋ฐฉ๋ฒ
PaaS
(Platform as a Service)- Cloud Foundry, Heroku
โ PaaS ์ ๊ณต ๊ธฐ์ ์ธ Cloud Foundry์์ ์ ๊ณตํ๋ cf command line ํด ์ฌ์ฉ
- Cloud Foundry, Heroku
Ex. $ cf push acloudyspringtime -p target/app-0.0.1-SNAPSHOT.jar
IaaS
(Infrastructure as a Service)- AWS Elastic Beanstalk, AWS Container Registry, AWS Code Deploy ๊ฐ์ ์๋น์ค ์ด์ฉ
- Azure Spring Cloud, Azure App Service ์์ Spring Boot ๊ธฐ๋ฐ์ Executable Jar ํ์ผ ๋ฐฐํฌ ๊ธฐ๋ฅ์ ์ ๊ณต
( Microsoft์ ํด๋ผ์ฐ๋ ์๋น์ค์ธ Azure ) - Google Cloud ์ฌ์ฉ
โ Executable Jar ํ์ผ ๋ฐฐํฌ๋ฅผ ์ํ ์ฌ๋ฌ๊ฐ์ง ์ต์ ์ ์ ๊ณต
CI / CD ํ๋ซํผ
์ ์ฌ์ฉํ ๋ฐฐํฌ- CI / CD ํ๋ซํผ ์ด์ฉํ์ฌ AWS๋ Azure ๊ฐ์ ํด๋ผ์ฐ๋ ์๋น์ค์ Executable Jar ํ์ผ์ ์๋ ๋ฐฐํฌํ๋๋ก ๊ตฌ์ฑ ๊ฐ๋ฅ
Ex. Github Actions / Circle CI
-
- ๋ฐฐํฌ ์๊ฐ ๋จ์ถ / ์ค์ ์ค๋ฌด์์๋ ๊ต์ฅํ ๋ง์ด ์ฐ์ด๋ ๊ธฐ์
๐ ์ค์ต
- projects - be-reference-profile-examples
- git - be-homework-build
์ค๋ ์ค์ต ๊ณผ์ ๋ application-server.yml
ํ์ผ์ MySQL๊ณผ ์ฐ๋ํ๊ธฐ ์๋ค !
๋จผ์ , build.gradle
ํด๋์ ์๋์ ๊ฐ์ ์์กด์ฑ์ ์ถ๊ฐํด์ฃผ๊ณ
implementation 'mysql:mysql-connector-java'
application-server.yml
ํ์ผ์
spring:
jpa:
hibernate:
ddl-auto: create-drop # (1) ์คํค๋ง ์๋ ์์ฑ
show-sql: true # (2) SQL ์ฟผ๋ฆฌ ์ถ๋ ฅ
properties:
hibernate:
format_sql: true # (3) SQL pretty print
datasource: // ์ฌ๊ธฐ์๋ถํฐ ์ถ๊ฐํ ๋ถ๋ถ
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/coffee_order?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&serverTimezone=Asia/Seoul
username: root
password: 1234
์์ ๊ฐ์ด ์ถ๊ฐํด์ฃผ๊ณ
Edit Configurations์์ --spring.profiles.active=server
์ด๋ ๊ฒ ์ค์ ํด์ฃผ๋ฉด
์๋ฒ๋ก mysql์ด ์ฐ๋๋๋ค !
[๊ฐ๋ฐ ํ๊ฒฝ์ ๋ฐ๋ฅธ application.yml ์ค์ ์ฐธ๊ณ ]
๐ ๋๋์
์ค๋์ ์ฌ์ด๊ฐ๋ ์ฑํฐ์๋ค!
๊ตฌ๊ธ์ ์ ๋ณด๋ ๋ง์๊ณ ๊ฐ๋จํ๊ฒ ๋ฐ๋ผํ๊ธฐ๋ง ๋๋ ๋ถ๋ถ์ด์๋ค.
๋ฒ์จ ๋ด์ผ์ด ๋ ๋ชจ์ ๋ฉด์ ใ
( ๋ณธ ๊ฒ์๋ฌผ์ 2022/11/15์ ์์ฑํ ๊ธ์ ์ฎ๊ธด ๊ธ์ ๋๋ค. ์๋ฌธ์ ์๊ธฐ์ ์์! )
'โข CodeStates BootCamp > Section 3' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ [Section3] ์ด์ ๊ฐ๋ ํ๊ณ ๐ฅ (0) | 2023.04.11 |
---|---|
๐ [Section3] 12. [ Spring MVC ] API ๋ฌธ์ํ (0) | 2023.04.11 |
๐ [Section3] 11. [ Spring MVC ] ํ ์คํ 2 (1) | 2023.04.11 |
๐ [Section3] 10. [ Spring MVC ] ํ ์คํ 1 (0) | 2023.04.11 |
๐ [Section3] 9. [ Spring MVC ] ํธ๋์ญ์ (0) | 2023.04.11 |