๐ ๋ฉํฐ ๋ชจ๋์ ๋์ ํ ์ด์
์ ์ด๋ฏธ์ง๋ ์งํ์ค์ธ ํ์ฐ์ค ํ๋ก์ ํธ์์ ๋ฉํฐ ๋ชจ๋์ ์ ์ฉํ๊ธฐ ์ ์ ํจํค์ง ๊ตฌ์กฐ์ ๋๋ค.
๊ธฐ์กด์๋ ์๋น์ค์ ๋ชจ๋ ์ฝ๋๋ค์ด ๋จ์ผ ๋ชจ๋์ ํฌํจ๋ ์ํ์์ต๋๋ค.
ํ์ฐ์ค ํ๋ก์ ํธ๋ ํธ์ ์๋ฆผ ๊ธฐ๋ฅ์ ํฌํจํ๊ณ ์๋๋ฐ, ํธ์ ์๋ฆผ์ ์ ์กํ๋ ์๋ฒ๋ฅผ ๋ถ๋ฆฌํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค.
ํ์ฐ์ค ์๋ฒ → ํ์ฐ์ค API ์๋ฒ, ํ์ฐ์ค ์๋ฆผ ์๋ฒ
์ด ๋, API ์๋ฒ์ ์๋ฆผ ์๋ฒ ๋ชจ๋ domain
ํจํค์ง์ ์ฝ๋, external.client
ํจํค์ง์ ์ฝ๋, common
ํจํค์ง์ ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์ฌ์ฌ์ฉํด์ผ ํ์ต๋๋ค. ํ์ง๋ง ์ ์ง ๋ณด์ ์ธก๋ฉด์์ ๋ฐ๋ผ๋ดค์ ๋, ์๋ฒ๊ฐ ๋๋ก ๋ถ๋ฆฌ๋๋ฉด์ ๊ณตํต๋ ํจํค์ง์ ๋ด์ฉ์ด ์์ ๋๋ฉด ๋งค๋ฒ API ์๋ฒ์ ์๋ฆผ ์๋ฒ์ ์ฝ๋๋ฅผ ํจ๊ป ์์ ํด์ค์ผ ํ๋ ๋ถํธํจ์ด ๋ฐ์ํ ์ ์์์ต๋๋ค.
์ด๋ฌํ ๋ถํธํจ์ ํด์ํ๊ธฐ ์ํ ๋ฐฉ์์ผ๋ก ๋ฉํฐ ๋ชจ๋ ๋์ ์ ๊ฒฐ์ ํ์ต๋๋ค. ๐คฉ
๐ค ๋ฉํฐ ๋ชจ๋์ด๋?
๋ฉํฐ ๋ชจ๋์ ํ๋์ ์์คํ ์ด๋ ํ๋ก์ ํธ๋ฅผ ์ฌ๋ฌ ๊ฐ์ ๋ ๋ฆฝ์ ์ธ ๋ชจ๋๋ก ๋ถ๋ฆฌํ์ฌ ๊ตฌ์ฑํ๋ ๋ฐฉ์์ ๋๋ค.
๋ชจ๋ํ๋ ๊ตฌ์กฐ๋ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ฌ์ฃผ๋๋ฐ, ๋น์ทํ ๊ธฐ๋ฅ์ ๊ฐ์ง ๋ชจ๋์ ๋ค๋ฅธ ํ๋ก์ ํธ์์๋ ์ฌ์ฌ์ฉํด์ ๊ฐ๋ฐ ์๊ฐ๊ณผ ๋ ธ๋ ฅ์ ์ ์ฝํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค.
์ ์ด๋ฏธ์ง๋ ๋จ์ผ ๋ชจ๋๋ก ๊ตฌ์ฑ๋์๋ ํ์ฐ์ค ํ๋ก์ ํธ์ ๋ฉํฐ ๋ชจ๋ ์ ์ฉ์ ์๋ฃํ ๋ชจ์ต์ ๋๋ค.
์ ์ฒด ์ฝ๋๋ ๊นํ๋ธ์์ ํ์ธํ ์ ์์ต๋๋ค.
API ์๋ฒ(hous-api
)์ ์๋ฆผ ์๋ฒ(hous-notification
)์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋ถ๋ถ์ hous-common
, hous-core
, hous-external
๋ชจ๋๋ก ๊ตฌ์ฑํ์ต๋๋ค.
์ด๋ ๊ฒ ๊ตฌ์ฑ์ ํ ๋๋ถ์, ๊ณตํต๋ ๋ถ๋ถ์์ ์์ ์ฌํญ์ด ๋ฐ์ํด๋ ๋ถ๋ฆฌํ ๋ชจ๋์์ ํ ๋ฒ๋ง ์์ ์ ํ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ์ ์ง ๋ณด์ ์ธก๋ฉด์์ ๋ ๊ฐํธํด์ก์ต๋๋ค.
๐ป ๋ฉํฐ ๋ชจ๋ ์ ์ฉ ๋ฐฉ๋ฒ
์ด์ ๋ฉํฐ ๋ชจ๋์ ์ ์ฉํ๊ธฐ ์ํ ๋ฐฉ๋ฒ๋ค์ ์ดํด๋ณด๊ฒ ์ต๋๋ค!
๐ฎ ์๋ก์ด ๋ชจ๋ ์ถ๊ฐ
๋จผ์ ์๋ก์ด ๋ชจ๋์ ์ถ๊ฐํ ๋๋ ์ต์๋จ ํ๋ก์ ํธ์์ ๋ง์ฐ์ค ์ฐํด๋ฆญ์ ํ๊ณ New > Module ์ ์ ํํฉ๋๋ค.
์ดํ ๋ชจ๋ ์ด๋ฆ์ ์ง์ ํ๊ณ ์์ฑํ๋ฉด ์๋ก์ด ๋ชจ๋์ด ๋ง๋ค์ด์ง๋๋ค.
๋ชจ๋์ด ์์ฑ๋๋ฉด settings.gradle ํ์ผ์ ์๋์ผ๋ก ์๋ก์ด ๋ชจ๋์ด include
๋ฉ๋๋ค.
์ด๋ ์๋ก์ด ํด๋๋ฅผ ์์ฑํ๊ณ ์ง์ include
ํด์ค๋ ๋ชจ๋๋ก ์ธ์๋๋ค๋ ์๋ฏธ์
๋๋ค.
๐ฎ build.gradle ์ค์
๋ค์์ build.gradle ์ ์ค์ ํ ์ฐจ๋ก์ ๋๋ค.
์ฐ์ ์ต์๋จ ํ๋ก์ ํธ์ build.gradle ํ์ผ์ ๋๋ค.
// server/build.gradle
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id "org.springframework.boot" version "2.7.1"
id "io.spring.dependency-management" version "1.0.11.RELEASE"
id "java"
}
// ํ์ ๋ชจ๋ ํ๋ก์ ํธ ๊ณตํต ์ธํ
subprojects {
apply plugin: "org.springframework.boot"
apply plugin: "io.spring.dependency-management"
apply plugin: "java"
group = "hous"
version = "1.0.7-SNAPSHOT"
sourceCompatibility = "11"
repositories {
mavenCentral()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
dependencies {
compileOnly "org.projectlombok:lombok"
annotationProcessor "org.projectlombok:lombok"
testCompileOnly("org.projectlombok:lombok")
testAnnotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude group: "org.junit.vintage", module: "junit-vintage-engine"
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.5"
}
}
tasks.named("test") {
useJUnitPlatform()
}
}
๋จ์ผ ๋ชจ๋ ํ๋ก์ ํธ์ build.gradle ํ์ผ๊ณผ ๋ค๋ฅธ ๋ถ๋ถ์ ์ดํด๋ณด๋ฉด subprojects
๊ฐ ์์ต๋๋ค.
subprojects
์๋ setting.gradle ์ include
๋ ๋ชจ๋ ํ๋ก์ ํธ์ ๊ณตํต์ ์ผ๋ก ์ ์ฉํ ์ค์ ์ ์์ฑํ๋ฉด ๋ฉ๋๋ค.
์ด์ ํ์ ํ๋ก์ ํธ๋ค์ build.gradle ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๋จผ์ ๊ณตํต๋ ๋ถ๋ถ์ธ hous-common
, hous-core
, hous-external
์ build.gradle ํ์ผ์
๋๋ค.
// server/hous-common/build.gradle
bootJar { enabled = false }
jar { enabled = true }
dependencies {
...
}
// server/hous-core/build.gradle
bootJar { enabled = false }
jar { enabled = true }
dependencies {
implementation project(":hous-common")
...
}
// server/hous-external/build.gradle
bootJar { enabled = false }
jar { enabled = true }
dependencies {
implementation project(":hous-common")
...
}
๋ค์์ API ์๋ฒ(hous-api
)์ ์๋ฆผ ์๋ฒ(hous-notification
)์ build.gradle ํ์ผ์
๋๋ค.
// server/hous-api/build.gradle
dependencies {
implementation project(":hous-core")
implementation project(":hous-common")
implementation project(":hous-external")
...
}
// server/hous-notification/build.gradle
dependencies {
implementation project(":hous-core")
implementation project(":hous-common")
implementation project(":hous-external")
...
}
๊ณตํต๋ ๋ชจ๋์ build.gradle
๋ด์ฉ์ ๋ณด๋ฉด ์๋์ ๊ฐ์ ๋ถ๋ถ์ด ์์ต๋๋ค.
bootJar { enabled = false }
: Spring Boot๋ ๊ธฐ๋ณธ์ ์ผ๋กbootJar
ํ์คํฌ๋ฅผ ์ฌ์ฉํ์ฌ ์คํ ๊ฐ๋ฅํ JAR ํ์ผ์ ์์ฑํฉ๋๋ค. ์ด ์ต์ ์false
๋ก ์ค์ ํ๋ฉด, ๋น๋ ํ๋ก์ธ์ค ์ค์ ์คํ ๊ฐ๋ฅํ JAR ํ์ผ์ด ์์ฑ๋์ง ์์ต๋๋ค.
jar { enabled = true }
: ์ผ๋ฐ์ ์ธ Java ํ๋ก์ ํธ์์ ์์ฑ๋๋ JAR ํ์ผ์ ํ์ฑํํ๋ ์ค์ ์ ๋๋ค. ์ด ์ต์ ์true
๋ก ์ค์ ํ๋ฉด, ๋น๋ ํ๋ก์ธ์ค ์ค์ ์ผ๋ฐ์ ์ธ JAR ํ์ผ์ด ์์ฑ๋ฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ด JAR ํ์ผ์ ํ๋ก์ ํธ์ ์ปดํ์ผ๋ ํด๋์ค ํ์ผ ๋ฐ ๋ฆฌ์์ค ํ์ผ์ ํฌํจํฉ๋๋ค.
๊ณตํต๋ ๋ถ๋ถ์ธ hous-common
, hous-core
, hous-external
๋ชจ๋๋ค์ ๊ฐ๋ณ์ ์ผ๋ก ์คํ๋์ง ์๊ณ , API ์๋ฒ์ ์๋ฆผ ์๋ฒ์ ํฌํจ๋๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ ์ต์
์ ์ถ๊ฐํฉ๋๋ค.
๋ง์ง๋ง์ผ๋ก ๋ชจ๋๊ฐ์ ์์กด์ฑ์ ์ค์ ํ๋ฉด ๋ฉ๋๋ค.
implementation project(":some-module")
: ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ์ค์ ํ๋ ๋ถ๋ถ์ ๋๋ค. ์ด ์ค์ ์ ํ์ฌ ํ๋ก์ ํธ๊ฐsome-module
ํ๋ก์ ํธ์ ์์กดํ๋ค๋ ๊ฒ์ ๋ํ๋ ๋๋ค.
โ๏ธ ์ฃผ์ํ ์
๋ชจ๋๊ฐ์ ์์กด์ฑ์ ์ค์ ํ ๋ ์ํ์ฐธ์กฐ๊ฐ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ์ฃผ์ํด์ผ ํฉ๋๋ค.
์๋ฅผ ๋ค์ด,
hous-core ๋ชจ๋์์ hous-common ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๊ณ ,
hous-common ๋ชจ๋์์ hous-core ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ์ํ์ฐธ์กฐ๊ฐ ์ผ์ด๋๋ ์ํฉ์ ๋๋ค!
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋ฉํฐ ๋ชจ๋์ ์ค๊ณํ ๋, ๊ฐ ๋ชจ๋์ ์ญํ ์ ๋ช ํํ ํ๊ณ ์์กด์ฑ ๊ตฌ์กฐ๋ฅผ ์ฃผ์ํด์ ์ค๊ณํด์ผ ํฉ๋๋ค.
๐ฎ Component Scan ํจํค์ง ๋ฑ๋ก
ํ์ฌ API ์๋ฒ์, ์๋ฆผ ์๋ฒ์์๋ hous-common
, hous-core
, hous-external
๋ชจ๋์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ํ์ง๋ง, ๋ณ๋ค๋ฅธ ์ค์ ์ ํ์ง ์๋๋ค๋ฉด Spring ์ ๋ค๋ฅธ ๋ชจ๋์ ์๋ ํด๋์ค๋ค์ Bean ์ผ๋ก ๋ฑ๋กํ ์ ์์ต๋๋ค.
@SpringBootApplication(scanBasePackages = {"hous.core", "hous.common", "hous.external", "hous.api"})
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
์์ ๊ฐ์ด scanBasePackages
์ ํจํค์ง ์ด๋ฆ๋ค์ ์
๋ ฅํด์ค์ Component Scan ์์ ์ค์บํ ํจํค์ง๋ฅผ ์ง์ ํด์ผ Spring ์ด ์ ์์ ์ผ๋ก ๋ชจ๋ Bean ๋ค์ ๋ฑ๋กํ ์ ์๊ฒ ๋ฉ๋๋ค.
๐ป ๋ฉํฐ ๋ชจ๋ ๋น๋ ๋ฐฉ๋ฒ
API ์๋ฒ์ ์๋ฆผ ์๋ฒ๋ฅผ ๋น๋ํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
# API ์๋ฒ
./gradlew hous-api:bootJar
# ์๋ฆผ ์๋ฒ
./gradlew hous-notification:bootJar
'โ๏ธ ๋ฐฑ์๋: Backend' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] Slack API ํ์ฉํ์ฌ ์๋ฌ ๋ชจ๋ํฐ๋งํ๊ธฐ (0) | 2023.08.03 |
---|---|
[Spring] JPA ์์์ฑ ์ปจํ ์คํธ ์ดํดํ๊ธฐ (0) | 2023.06.27 |
[Spring] ํผ๋๋๋ JPA ๊ฐ๋ ์ ๋ฆฌ (Hibernate, Spring Data JPA) (0) | 2023.06.27 |
[Spring] JPA ๋น๊ด์ ๋ฝ ์ ์ฉ์ผ๋ก ๋์์ฑ ๋ฌธ์ ํด๊ฒฐํ๊ธฐ (0) | 2023.06.15 |
[Spring] MySQL๊ณผ mongoDB ๋ฐ์ดํฐ๋ฒ ์ด์ค 2๊ฐ ์ฐ๋ํ๊ธฐ (0) | 2023.05.10 |
Spring ๋ฌดํ์คํฌ๋กค ๊ตฌํ (2) - ์คํ์ ๊ธฐ๋ฐ ํ์ด์ง๋ค์ด์ (0) | 2023.01.01 |
Spring ๋ฌดํ์คํฌ๋กค ๊ตฌํ (1) - ์ปค์ ๊ธฐ๋ฐ ํ์ด์ง๋ค์ด์ (0) | 2023.01.01 |