Skip to content

zkfmapf123/api-gateway-pubsub-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Gateway Pub/Sub Pattern

Desc

  • 기존 Server to Server 통신을 Gateway + Queue 통신으로 수정
  • 통신 방식을 수정함으로써, Server 간의 Coupling을 낮춰 안정적인 데이터 송/수신 보장

ASIS

asis

  • Call Server To Server

  • Sub Server가 죽으면 데이터 유실 + 503 (Server Unavailable)

  • Pub Server Host : pub.leedonggyu.com

  • Sub Server Host : sub.leeodnggyu.com

TOBE

tobe

  • Call Pub Server -> Proxy Gateway -> Messaging Queue -> Sub Server

  • Sub Server가 죽어도 SQS에서 Queueing 동작하도록 구성

  • Pub Server Host : pub.leedonggyu.com

  • GW Server Host : sub-gw.leedonggyu.com

  • Sub Server Host : sub.leedonggyu.com

In Code

// pub code
app.get("/pub", async (req,res) => {
    let statusCode = 200
    
    try{
        const result = await axios.post(SUB_HOST, {
            body : {
                id : uuidv4(),
                name : "leedonggyu",
                age : 31,
                job : "devops"
            }
        })

        console.log(`Success >> ${result.status}`)

    }catch(e){
        console.error(e)
        statusCode = 500
        // ignore
    }

    return res.status(statusCode).json({})
})
// sub code
app.post("/sub",async (req,res) =>{
   
    console.log("body : ",  JSON.stringify(req))

    const {id} = req
    try{
        
        // S3 에 저장
        const command=  new PutObjectCommand({
            Bucket  : "donggyu-test-poc",
            Key : id,
            Body : JSON.stringify(req.body)
        })

        const result = await client.send(command)
        console.log("result >> ", result)
    }catch(e) {
        console.error(e)
        // ignore
    }

    return res.status(200).json({})
})

API-Gateway + SQS

IAM Role 만들기

role-1 role-2

API Gateway + SQS 연동하기

gw-1 gw-2 gw-3 gw-4 gw-5 gw-6

API Gateway 사용자 지정 도메인

gw-7

  • API Mapping 에서 Stage 별로 따로 Domain 구성해야 함

EventBridge Pipe 설정

eda-1 eda-2

Result

result

Reference

    curl -X POST https://sub-gw.leedonggyu.com/events/sub \
    -H "Content-Type: application/json" \
    -d '{"id": 10, "age" : 31, "name": "leedonggyu", "job": "devops"}'

...

  • Thanks to my "K"

About

api-gateway + SQS Pub/Sub Pattern

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published