반응형
프로젝트 개요
AWS 클라우드 환경을 기반으로 하는 느슨하게 연결된 (loosely coupled) 어플리케이션 아키텍처에 대한 이해
프로젝트 요구사항 및 시나리오
프로젝트 명 : <자동 재고 확보 시스템> 을 위한 MSA 구성
Day 3
목표
Factory-API 문서
Factory-API Document
Method
- POST
Path
- /api/manufactures
Request Body Schema : application/json
{
MessageGroupId : string(메시지 그룹 아이디) //"stock-arrival-group",
MessageAttributeProductId : string(추가 생산이 필요한 제품 아이디),
MessageAttributeProductCnt : string(추가 생산 요청 수량),
MessageAttributeFactoryId : string(추가 생산을 요청할 공장 아이디),
MessageAttributeRequester : string(추가 생산 요청 담당자)
CallbackUrl : string(생산 내역으로 데이터베이스에 재고를 추가할 서버의 주소)
}
Step 4 : 데이터베이스의 재고를 증가시키는 Lambda 함수 생성
#1. 데이터베이스의 재고를 증가시키는 Lambda 함수 (stock-increase-lambda)를 배포
const serverless = require("serverless-http");
const express = require("express");
const app = express();
app.use(express.json())
const {
connectDb,
queries: { getProduct, increaseStock }
} = require('./database')
app.post("/product/donut", connectDb, async (req, res, next) => {
const [ result ] = await req.conn.query(
getProduct('CP-502101')
)
if (result.length > 0) {
const product = result[0]
const incremental = req.body.MessageAttributeProductCnt || 0
await req.conn.query(increaseStock(product.product_id, incremental))
return res.status(200).json({ message: `입고 완료! 남은 재고: ${product.stock + incremental}`});
} else {
return res.status(400).json({ message: "상품 없음" });
}
});
app.use((req, res, next) => {
return res.status(404).json({
error: "Not Found",
});
});
module.exports.handler = serverless(app);
module.exports.app = app;
#2. stock_lambda 에서 레거시 시스템 (Factory API) 에 제품 생산 요청
- Factory-API 문서를 활용한 코드작성
- npm install axios 을 통한 axios 라이브러리 (stock-increase-lambda 에서)
=> axios 이란? promise 기반 비동기 방식의 HTTP 요청 처리
- const axios = require('axios').default 을 소스코드 맨 위에 작성
const axios = require('axios').default
const Stock_Empty = async (event) => {
let newevent = JSON.parse(event.Records[0].body)
console.log(event);
console.log("event : ", newevent);
axios.post('<DB URL>', {
"MessageGroupId": 'stock-arrival-group',
"MessageAttributeProductId": <ProductId>,
"MessageAttributeProductCnt": 3,
"MessageAttributeFactoryId": <FactoryId>,
"MessageAttributeRequester": <Requester>,
"CallbackUrl": <increase lambda api gateway>
})
.then(function (response) {
console.log("성공");
})
.catch(function (error) {
console.log(error);
});
}
module.exports = {
Stock_Empty,
};
- stock-increase-lambda => handler.js 수정
const incremental = req.body.<재고 증가 변수 입력> || 0
#3. curl 명령을 통한 재고 생산 요청
- 재고 소진 후 재요청 시 재고 생산요청 됨
- 생산 완료 확인 후 재요청 하여 구매 확인
#4. 추가 시나리오에 대한 아키텍처 구성 추가
- 재고가 없는 상황에서도 광고가 계속 진행되어 광고 중단 요청을 광고 담당자에게 보내야 한다
- Queue를 사용하여야 하며, AWS SES 서비스를 이용해 이메일을 전송하여야 한다.
구성 내용
- Sales Api 에 연결된 SNS 를 통해 광고SQS로 전달이 되고 SES로 트리거 되며 느슨한 결합을 이룬다.
(광고 담당자에게 e-mail 전달)
- DLQ를 추가하여 메시지가 소비되지 않았을때 보관할 수 있도록 한다.
반응형
'DevOps > 프로젝트' 카테고리의 다른 글
Project 3 : 마이크로서비스 DAY-2 (자동 재고 확보 시스템 을 위한 MSA 구성) (0) | 2023.02.21 |
---|---|
Project 3 : 마이크로서비스 DAY-1 (튜토리얼) (0) | 2023.02.18 |