본문 바로가기

Cloud/MSA

MSA 교육 정리(2)

[1] 
- 이벤트 스토밍 결과 기반의 Spring-boot 코드의 매핑 관계

Bounded Context --> 단위 마이크로서비스
Event --> Event POJO
Command --> RestRepository
Aggregate --> Entity (Aggregate Root)
Policy --> PolicyHandler 내에 kafka event listener

- Read Model(초록): , Issue(보라): 일종의 comment로 반영 안되면 기울이기, 되면 원래대로

Read Model(초록)에 CQRS(Command Query Responsibility Segregation)

create, update, delete When~ 에 SET, WHERE 세팅

- Correlation Key
- ACL(Anti Corruption Layer)

[2] 구현기술 - 129p 참조

[3] httpie(나중에 해보려면 깔아야함) -> postman과 사용 이유 같음
- Repository 패턴 - rest api를 자동으로 만들어줌(Rest Repositories 라이브러리)
public interface ProductRepository extends CrudRepository<Product, Long> {}
-> products api 생성(소문자로 바뀌고 복수형)

[4]
- 이벤트는 Aggregate 내의 상태 변화에 의해서 발생
- 카프카에 쏘기전 직렬화(json으로 변환)해줘야함

*** Kafka ***

streams은 메세지의 흐름으로 input 메세지와 output 메세지가 존재
Processor 방식은 input 과 output 을 모두 사용하는 방식
stream 을 kafka 와 연결하기 위하여 application.yaml 파일에 아래 설정을 추가
kafka brokers로 localhost:9092 를 사용한다는 의미입니다. 카프카 설치시 기본 포트가 9092
bindings.input 과 bindings.output 은 기본 채널입니다. 만약 채널명을 변경 하고 싶으면 Processor 를 새로 만들어야 합니다.
https://github.com/event-storming/products/blob/master/src/main/java/com/example/template/config/kafka/KafkaProcessor.java
destination 은 목적지라는 뜻인데, kafka 에서는 topic 이름
group도 아주아주 중요 추후 설명
즉, 해당 설정은 shop 이라는 토픽에 메세지를 주고 받겠다는 의미입니다.
환경정보 파일인 resouces/applications.properties를 application.yml로 변경 후, 아래 내용을 추가합니다.

spring:
  cloud:
stream:
  kafka:
binder:
  brokers: localhost:9092
  bindings:
input:
  group: product
  destination: shop
  contentType: application/json
output:
  destination: shop
  contentType: application/json   
  
  
[5], [6] 
이론 교육

- 스프링의 경우 yaml파일에 '---'
- brokers는 kafka 서버
- profiles: docker
- server: server:          
port: 8081 port: 8082 ... overriding 해서 포트 안겹치게함
- 터미널에 assigned partitions 나오면 카프카 잘 연결된 것

- Command 타입 두가지
1. RestRepository - 라이브러리 사용해서 rest api 쓰는 방식
2. Controller - 알고있는 컨트롤러 방식

[7] API Gateway

gateway:
  routes:
- id: order
  uri: http://localhost:8081
  predicates:
- Path=/orders/**
- id: delivery
  uri: http://localhost:8082
  predicates:
- Path=/deliveries/** 

yaml은 쿠버네티스가 좋아하는 간식!

- HATEOAS: rest api중 상속도가 가장 높음

- vscode > labs > kafka consumer
topic만 써주면 됨(yaml 파일의 destination)
shopmall(얘가 토픽) --from-beginning -> 쌓인 모든 메세지 가져옴

728x90
반응형