RESTful API ๊ฐ€์ด๋“œ

HTTP REST API์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž

REST์˜ ๊ธฐ๋ณธ ์š”์†Œ

์˜ˆ์ œ๋ฅผ ํ•˜๋‚˜ ๋“ค์–ด๋ณด์ž

POST /api/v1/users

{
  "users":{
    "name":"andrew"
  }
}

POST ๋ฐฉ์‹์œผ๋กœ api/v1/users ์š”์ฒญํ•˜๊ณ  ๋ฐ”๋””๋กœ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธด๋‹ค.

HTTP ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์˜๋ฏธ Idempotent
POST create No
GET select Yes
PUT update Yes
DELETE delete Yes

HTTP ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฐ๊ฐ ํ–‰์œ„๋ฅผ ์ •์˜ํ•œ๋‹ค. idempotent๋Š” ๊ฐ™์€ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ Idempotent ํ•˜๋‹ค๊ณ  ๋งํ•œ๋‹ค. POST๋Š” ๋งค๋ฒˆ ๋ฐ”๋””์ •๋ณด๋กœ ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— Idempotent ํ•˜์ง€ ์•Š๊ณ , ๋งค๋ฒˆ ์กฐํšŒ๋ฅผ ํ•˜์ง€๋งŒ count++ ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๋กœ์ง์ด ์ถ”๊ฐ€๋œ API๋ผ๋ฉด ์ด ๊ฒฝ์šฐ๋„ Idempotentํ•˜์ง€ ์•Š๋‹ค๊ณ  ํ•œ๋‹ค.

Idempotent์„ค๋ช…์€ ์™œ? (Stateless)

REST๋Š” ๊ฐ ๊ฐœ๋ณ„ API๋ฅผ ์ƒํƒœ ์—†์ด ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. ์ƒํƒœ๋ฅผ HttpSession์™€ ๊ฐ™์€ ์ปจํ…์ŠคํŠธ ์ €์žฅ์†Œ์— ์ƒํƒœ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š์Œ์„ ์˜๋ฏธํ•œ๋‹ค.

URL ๋™์‚ฌ ๋ณด๋‹ค ๋ช…์‚ฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

REST API๋Š” ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•ด์„œ ํ–‰๋™์„ ์ •์˜ํ•จ

GET /teams/{id}/groups/{groupId}
// ์ž˜๋ชป๋œ ์˜ˆ์ œ
GET /getTeams/
POST /setTeam/{id}
ย  POST GET PUT DELETE
/teams ์ƒˆ๋กœ์šด ํŒ€์„ ๋“ฑ๋กํ•จ ํŒ€ ๋ชฉ๋ก์„ ๋ฆฌํ„ด ํŒ€ ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ(Bulk) ๋ชจ๋“  ํŒ€ ์ •๋ณด๋ฅผ ์‚ญ์ œ
/teams/1 x 1๋ฒˆ ํŒ€์„ ๋ฆฌํ„ด 1๋ฒˆ ํŒ€ ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ 1๋ฒˆ ํŒ€์„ ์‚ญ์ œ

๋ฆฌ์†Œ์Šค๊ฐ„์˜ ๊ด€๊ณ„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

์œ„์—์„œ๋„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ๋“ค์—ˆ์ง€๋งŒ /resource/{identifier}/other-related-resources ํ˜•ํƒœ๋กœ ์ •์˜ํ•œ๋‹ค.

GET /teams/3/groups/4

3๋ฒˆ ํŒ€์˜ 4๋ฒˆ ๊ทธ๋ฃน์„ ์กฐํšŒํ•˜๋Š” API

๊ฒ€์ƒ‰

์ผ๋ฐ˜์ ์œผ๋กœ HTTP GET์€ QueryString์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ฒ€์ƒ‰์กฐ๊ฑด์ด ๋‹ค๋ฅธ Query String๊ณผ ์„ž์ผ ์ˆ˜ ์žˆ๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ ๊ฒ€์ƒ‰์กฐ๊ฑด์€ URL Encoder๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋‚˜์˜ ๊ฒ€์ƒ‰์กฐ๊ฑด์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

// ์—ฌ๋Ÿฌ๊ฐœ์˜ QueryString์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰์กฐ๊ฑด๊ณผ ํŽ˜์ด์ง• ์กฐ๊ฑด์ด ํ—ท๊ฐˆ๋ฆผ (X)
/users?name=andrew&region=seoul&offset=20&limi=10

// ๊ฒ€์ƒ‰์กฐ๊ฑด์€ ํ•˜๋‚˜์˜ QueryString์—์„œ ๋๋‚ด๊ณ , ๋‚˜๋จธ์ง€๋Š” ํŽ˜์ด์ง• ์กฐ๊ฑด (O)
/users?q=name%3Dandrew,region%3Dseoul&offset=20&limit=10

์ „์—ญ ๊ฒ€์ƒ‰๊ณผ ๋ฆฌ์†Œ์Šค ๊ฒ€์ƒ‰

์ „์—ญ ๊ฒ€์ƒ‰์€ /search?q=name%3Dandrew

๋ฆฌ์†Œ์Šค๊ฒ€์ƒ‰์€ /users?q=name%3Dandrew

๋‹จ์ผ API URL

API ์„œ๋ฒ„๊ฐ€ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋ถ„๋ฆฌ๋œ ๊ฒฝ์šฐ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•˜๊ณ  ์žˆ์„ ๋•Œ, account.apiserver.com, appapiserver.com ๊ณผ ๊ฐ™์„ ๋•Œ HAProxy๋‚˜ nginx reverse proxy๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

HAProxy ์„ค์ •์—์„œ

api.apiserver.com/account ๋Š” account.apiserver.com๋กœ ๋ผ์šฐํŒ… ํ•˜๊ณ 
api.apiserver.com/app ๋Š” app.apiserver.com์œผ๋กœ ๋ผ์šฐํŒ…ํ•˜๋„๋ก ๊ตฌํ˜„

์—ฐ๊ด€ ํฌ์ŠคํŠธ