티스토리 뷰
참고서적: 스프링으로 하는 마이크로서비스 구축, 스프링 부트와 스프링 클라우드를 이용한 도커/쿠버네티스 머이크로서비스, 매그너스 라슨
책 설명을 완전히 이해하지 못하여 몇일동안 끙끙거리다, 나름 내 식대로 성공한거 같아서 기록해둠.
5개 프로젝트 구조
1. 전체 프로젝트를 담는 프로젝트
2. (공통) api, util 프로젝트
3. 메인서비스를 담는 프로젝트
4. (메인서비스) product, remmendation, review 프로젝트
전체적으로 구조만 보면 7개, 알맹이만 보면 5개.
1. 전체 프로젝트 microservicesFather
일단 microservicesFather 이름으로 spring boot 프로젝트 만들기.
이 프로젝트는 src 필요없음. 지우기
build.gradle도 필요없음. 지우기
2. 메인프로젝트들(3개)을 담을 프로젝트(모듈) microservices
microservicesFather에 오른쪽클릭 > New -> Module
microservices모듈 역시 src, build.gradle 필요없어서 지우기
3. 메인프로젝트 3개 추가
microservices모듈에 대고 오른쪽 클릭 > New > Module
product-service, recommendation-service, review-service 모듈 추가
4. 메인 프로젝트 3개를 결합하는 모듈 추가
microservices모듈에 대고 오른쪽 클릭 > New > Module
product-composite-service 모듈 추가
5. 공통 프로젝트 api, util 모듈 추가
microservicesFather프로젝트에 대로 오른쪽 클릭 > New > Module
api, util 모듈 추가
이렇게 큰 구조는 다 잡았다. 이제 각 모듈들을 이어보자.
root Project에 6개 모듈 include
(api, util, product, recommendation, review, product-composite)
오 intelliJ에서 모듈추가를 하면 저절로 include 소스가 만들어짐 ㄷㄷ
start.spring.io 에서 프로젝트 만들어서 했다면, 직접 작성해야 한다고 함.
rootProject.name = 'microserviesFather'
include 'microservices'
include 'microservices:product-service'
findProject(':microservices:product-service')?.name = 'product-service'
include 'microservices:recommendation-service'
findProject(':microservices:recommendation-service')?.name = 'recommendation-service'
include 'microservices:review-service'
findProject(':microservices:review-service')?.name = 'review-service'
include 'microservices:product-composite-service'
findProject(':microservices:product-composite-service')?.name = 'product-composite-service'
include 'api'
include 'util'
product-composite-service에서 메인 프로젝트 3개 결합
1. product-service: localhost:7001
2. recommendation-service: localhost:7002
3. review-service: localhost:7003
4. product-composite-service: localhost:7000
product-composite-service가 받은 요청은 3개의 프로젝트로 적절히 도착하게 될꺼임.
microservicesFather\microservices\product-composite-service에서
src\main\resources\application.properties
server.port=7000
app.product-service.host=localhost
app.product-service.port=7001
app.recommendation-service.host=localhost
app.recommendation-service.port=7002
app.review-service.host=localhost
app.review-service.port=7003
// microservicesFather\microservices\product-service\src\main\resources\application.properties
server.port=7001
logging.pattern.level=debug
logging.level.org.example=debug
// microservicesFather\microservices\recommendation-service\src\main\resources\application.properties
server.port=7002
logging.pattern.level=debug
logging.level.org.example=debug
// microservicesFather\microservices\review-service\src\main\resources\application.properties
server.port=7003
logging.pattern.level=debug
logging.level.org.example=debug
product-service 서버 켜기
(이후에 recommendation-service, review-service, product-composite-service도 똑같이 설정해주기)
1. product-service 모듈 build.gradle에 dependencies 추가
...
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-web:2.7.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.7.6'
...
}
...
2. build.gradle 수정됐으니 build 해주기
3. ProductServiceApplication.java 만들기
위치는 microserviceFather\microservices\product-service\src\main\java\org\example\microservices\core\product/ProductServiceApplication.java
package org.example.microservices.core.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("org.example")
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}
4. 4개 모듈 서버를 모두 키면
api, util은 주로 공통기능만 가져다 쓰는 용도라서 서버없이 쓸꺼임.
메인서버 3개, product-composite-service에서 api, util을 사용할 수 있도록
4개 모듈 각각 dependencies에 추가
...
dependencies {
implementation project(':api')
implementation project(':util')
...
}
...
이제 각 3개 메인 프로젝트(모듈)에 mapping, method를 추가해서
요청을 받으면 각 프로젝트가 처리할 수 있도록 해보자.
'spring(boot)' 카테고리의 다른 글
관련 기본 용어들 (0) | 2023.05.01 |
---|---|
[microservices]2.소스 추가 (0) | 2023.01.31 |
[junit]jacoco로 coverage보기 (0) | 2022.11.23 |
[security]jwt (+react) (0) | 2022.11.16 |
[java]parameter로 method 넘기기 (0) | 2022.10.30 |