Search
๐ŸŸข

General

1. Message Queue

๊ฐ€. ๊ธฐ๋ณธ ๊ฐœ๋…

1) ๊ฐœ๋…

โ€ข
์ •์˜: ๋น„๋™๊ธฐ ํ†ต์‹  ์‹œ์Šคํ…œ์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ์‘์šฉํ”„๋กœ๊ทธ๋žจ ๊ฐ„ ๋ฐ์ดํ„ฐ ์†ก์ˆ˜์‹ ์„ ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•จ
โ€ข
ํŠน์ง•
โ†’ ๋น„๋™๊ธฐ ํ†ต์‹ ์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋„์ฐฉ์ง€์˜ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์ด ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•  ์ƒํ™ฉ์ด ์•„๋‹ ๊ฒฝ์šฐ, ์ž„์‹œ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๊ด€ํ•จ
โ†’ ๋ฐ์ดํ„ฐ ์ž„์‹œ ์ €์žฅ์†Œ์˜ ๊ฒฝ์šฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜ ๋ฐฉ์‹๊ณผ ํŒŒ์ผ ๊ธฐ๋ฐ˜ ๋ฐฉ์‹์ด ์กด์žฌํ•จ

2) ๋™์ž‘ ํ๋ฆ„

โ€ข
Producer๊ฐ€ ๋ถ„์‚ฐ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌํ•  ์ž‘์—…(tasks) ์ƒ์„ฑ
โ†’ Producer: Message Writer
โ€ข
Broker๊ฐ€ ๊ฐ๊ฐ์˜ ์ž‘์—…(task)์— ๋Œ€ํ•ด consumer์—๊ฒŒ ๋ถ„์‚ฐ ์ „๋‹ฌ
โ†’ Broker: Message Broker
โ€ข
Consumer๊ฐ€ ์ „๋‹ฌ ๋ฐ›์€ ์ž‘์—…์„ ์ฒ˜๋ฆฌ
โ†’ Consumer: Message Reader
โ€ข
Result Backend๊ฐ€ ์ฒ˜๋ฆฌ๋œ ์ž‘์—…์„ ์ €์žฅ

๋‚˜. ์ฃผ์š” ์ปดํฌ๋„ŒํŠธ

1) Broker

โ€ข
Broker: Message Broker
โ€ข
์ƒ์„ฑ๋œ task์— ๋Œ€ํ•ด task queue๋กœ ๊ด€๋ฆฌํ•จ
โ€ข
task๋ฅผ ์ ์ ˆํ•œ ๊ฒฝ๋กœ์— ๋”ฐ๋ผ Consumer ๋‚ด worker์—๊ฒŒ ์ „๋‹ฌ
โ€ข
ex) RabbitMQ, Redis ๋“ฑ์ด ์žˆ์Œ

2) Consumer

โ€ข
Consumer: Message Reader
โ€ข
ํ•˜๋‚˜ ์ด์ƒ์˜ worker๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ

3) Result Backend

โ€ข
์ฒ˜๋ฆฌ๋œ ์ž‘์—…์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๊ด€๋ จ ์„ค์ • ํ•„์š”
โ†’ ์ฒ˜๋ฆฌ๋œ ์ž‘์—…์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ๊ธฐ๋ณธ ์„ธํŒ…์ด ์•„๋‹˜

2. RabbitMQ

๊ฐ€. ๊ฐœ๋… ์ •๋ฆฌ

1) ๊ธฐ๋ณธ ์šฉ์–ด

โ€ข
Queue: ๋ฒ„ํผ์˜ ์ผ์ข…์œผ๋กœ Broker์˜ ๋ฉ”์‹œ์ง€ ์ €์žฅ์†Œ
โ€ข
Exchange: ๋ฉ”์‹œ์ง€๋ฅผ ํŠน์ • ํ๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์ค‘๊ฐœ์ž
โ†’ Exchange๋Š” Virtual Host๋ณ„๋กœ ์ •์˜๋จ
โ†’ bindings, routing keys, header attributes๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฉ”์‹œ์ง€ ์ค‘๊ฐœํ•จ
โ€ข
Routing Key: Message ์ฃผ์†Œ
โ†’ Exchange์—์„œ Routing Key๋ฅผ ์ฝ์–ด์„œ ์–ด๋–ค Queue๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ์ง€๋ฅผ ๊ฒฐ์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•จ
โ€ข
Virtual Host: ๋ณต์ˆ˜์˜ virtual host๋ฅผ ๋‘ ์œผ๋กœ์จ ํ•˜๋‚˜์˜ RabbitMQ Instance์—์„œ virtual host๋ณ„๋กœ Queue์™€ Exchange์— ๋Œ€ํ•œ ์ ‘๊ทผ๊ถŒํ•œ์„ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
โ€ข
binding: ํŠน์ • queue์™€ exchange์˜ ์—ฐ๊ฒฐ ์„ค์ •
โ€ข
Acknowledgements(or Ack): consumer๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ž˜ ๋ฐ›์•˜๋‹ค๋Š” ํ‘œ์‹œ๋กœ Broker์—๊ฒŒ ๋ณด๋‚ด๋Š” ๋‹ต์žฅ
โ€ข
AMQP(Advanced Message Queuing Protocol): ์‹œ์Šคํ…œ ๊ฐ„ ๋ฉ”์‹œ์ง€ ์†ก์ˆ˜์‹ ์— ๋Œ€ํ•œ open standard protocol
โ†’ Broker Vendor๋‚˜ ํ”Œ๋žซํผ์— ์ข…์†๋˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ํŠน์ง•
โ€ข
Delivery Tag: ์˜ˆ์ œ๊ฐ€ ํ•„์š”ํ•จ
The Delivery tag uniquely identifies the delivery on a channel. If the delivery tag is set to 1, multiple messages can be acknowledged with a single method. If set to zero, the delivery tag refers to a single message.
Plain Text
๋ณต์‚ฌ

2) Exchange Types

โ€ข
Direct Exchange: ๋ฉ”์‹œ์ง€ ๋‚ด routing key์™€ ์ผ์น˜ํ•˜๋Š” binding key๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” queue๋กœ ๋ฉ”์‹œ์ง€ ๋ผ์šฐํŒ…
โ€ข
Default Exchange: ์ผ์ข…์˜ Direct Exchange๋กœ ๋ฉ”์‹œ์ง€ ๋‚ด routing key์™€ ์ผ์น˜ํ•˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” queue๋กœ ๋ฉ”์‹œ์ง€ ๋ผ์šฐํŒ…
โ†’ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” exchange๋ฅผ ์ด์šฉํ•˜๊ณ , exchange์˜ ์ด๋ฆ„์€ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค
โ€ข
Topic Exchange: ๋ฉ”์‹œ์ง€์˜ routing key์™€ Queue์˜ routing pattern์˜ ๋งค์นญ์—ฌ๋ถ€๋ฅผ ํ† ๋Œ€๋กœ ํ•ด๋‹น๋˜๋Š” queue๋กœ ๋ฉ”์‹œ์ง€ ๋ผ์šฐํŒ…
โ†’ ๋ฉ”์‹œ์ง€์˜ routing key๋Š” ๋งˆ์นจํ‘œ๋กœ ๊ตฌ๋ถ„๋˜๋Š” words list์˜ ํ˜•ํƒœ
โ†’ ํ์˜ routing pattern๋„ ๋งˆ์นจํ‘œ๋กœ ๊ตฌ๋ถ„๋˜์ง€๋งŒ *, #์˜ ํŠน์ˆ˜๋ฌธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ณต์ˆ˜์˜ routing key๋ฅผ ๋งค์นญํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑ๋จ
โ†’ : ํ•ด๋‹น ๊ตฌ๊ฐ„์˜ ๋ชจ๋“  ๋‹จ์–ด ํฌํ•จ
ex) ๋ฉ”์‹œ์ง€๊ฐ€ ์ด๋Ÿฌํ•œ ํ˜•์‹(ํšŒ์‚ฌ.๋ณธ๋ถ€.์ง์ฑ… )์„ ๋”ฐ๋ฅผ ๊ฒฝ์šฐ, ogq.*.leader์˜ ํŒจํ„ด์ด ์ง€์ •๋œ ํ๋Š” ogq์˜ ๋ชจ๋“  ๋ณธ๋ถ€์˜ ๋ฆฌ๋”๋“ค์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•จ
โ†’ #: ์ดํ›„ ๋ชจ๋“  ๊ตฌ๊ฐ„์— ํ•ด๋‹น
ex) ๋ฉ”์‹œ์ง€๊ฐ€ ์ด๋Ÿฌํ•œ ํ˜•์‹(ํšŒ์‚ฌ.๋ณธ๋ถ€.์ง์ฑ… )์„ ๋”ฐ๋ฅผ ๊ฒฝ์šฐ, ogq.#์˜ ํŒจํ„ด์ด ์ง€์ •๋œ ํ๋Š” ogq์˜ ๋ชจ๋“  ๋ณธ๋ถ€์˜ ๋ชจ๋“  ๊ตฌ์„ฑ์›์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•จ
โ€ข
Fanout Exchange: ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“  ํ๋กœ ๋ฉ”์‹œ์ง€ ๋ผ์šฐํŒ…
โ†’ routing key๋‚˜ pattern๊ณผ๋Š” ๊ด€๊ณ„ ์—†์ด ๋ฐ”์ธ๋”ฉ๋œ ๋ชจ๋“  ํ๋กœ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ผ์šฐํŒ…ํ•œ๋‹ค
โ€ข
Headers Exchange: ๋ฉ”์‹œ์ง€ ๋‚ด ํ—ค๋” ์†์„ฑ ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฉ”์‹œ์ง€ ๋ผ์šฐํŒ…
โ†’ routing key ๋Œ€์‹  header attributes ๊ธฐ๋ฐ˜์œผ๋กœ ๋งค์นญ๋˜๋Š” ํ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ผ์šฐํŒ…ํ•œ๋‹ค

๋‚˜. ๊ธฐ๋ณธ ์„ค์ •

1) ์‹œ์ž‘ / ์ข…๋ฃŒ

โ€ข
RabbitMQ ์„ค์น˜
โ†’ brew install rabbitmq
โ€ข
ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •
โ†’ ์‰˜ ํ™•์ธ: echo $SHELL
โ†’ (zsh์˜ ๊ฒฝ์šฐ) ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •: vim ~/.zshrc
โ†’ .zshrc ๋‚ด๋ถ€์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€: PATH=$PATH:/usr/local/sbin
โ€ข
RabbitMQ Server ์‹œ์ž‘
โ†’ rabbitmq-server
โ€ข
RabbitMQ Server ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ
โ†’ ์„œ๋ฒ„๋ฅผ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์•ˆ์ฃฝ์—ˆ์„ ๊ฒฝ์šฐ, ๊ด€๋ จ ํ”„๋กœ์„ธ์Šค ๊ฐ•์ œ ์ข…๋ฃŒ
sudo lsof -i :25672 sudo kill -2 <PID>
Shell
๋ณต์‚ฌ

2) ์„ค์ •

RabbitMQ URL ๊ตฌ์„ฑ: amqp://user:password@host:5672/virtual_host
โ€ข
์‚ฌ์šฉ์ž ์ด๋ฆ„๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ
โ†’ rabbitmqctl add_user maxwell passwordOfmaxwell123
โ†’ ์‚ฌ์šฉ์ž ์ด๋ฆ„: maxwell
โ†’ ์‚ฌ์šฉ์ž ๋น„๋ฐ€๋ฒˆํ˜ธ: passwordOfmaxwell123
โ€ข
๊ฐ€์ƒ ํ˜ธ์ŠคํŠธ ์ƒ์„ฑ
โ†’ rabbitmqctl add_vhost maxwell_vhost
โ†’ ๊ฐ€์ƒ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„: maxwell_vhost
โ€ข
์‚ฌ์šฉ์ž ํƒœ๊ทธ ์ƒ์„ฑ
โ†’ rabbitmqctl set_user_tags maxwell maxwell_tag
โ†’ ์‚ฌ์šฉ์ž ์ด๋ฆ„: maxwell
โ†’ ํƒœ๊ทธ ์ด๋ฆ„: maxwell_tag
โ€ข
๊ฐ€์ƒ ํ˜ธ์ŠคํŠธ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ๊ถŒํ•œ ์ƒ์„ฑ
โ†’ rabbitmqctl set_permissions -p maxwell_vhost maxwell ".*" ".*" ".*"
โ†’ first ".*": ๋ชจ๋“  entity์— ๋Œ€ํ•œ ์„ค์ • ๊ถŒํ•œ
โ†’ second ".*": ๋ชจ๋“  entity์— ๋Œ€ํ•œ ์“ฐ๊ธฐ ๊ถŒํ•œ
โ†’ third ".*": ๋ชจ๋“  entity์— ๋Œ€ํ•œ ์ฝ๊ธฐ ๊ถŒํ•œ
โ€ข
๋“ฑ๋ก๋œ ์‚ฌ์šฉ์ž ๋ชฉ๋ก ํ™•์ธ
โ†’ rabbitmqctl list_users
โ†’ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ธ ๊ฐ€๋Šฅ

3) Docker ๊ธฐ๋ฐ˜ ์ดˆ๊ธฐ ์„ค์ •

โ€ข
๋‹จ์ˆœ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ๋ช…๋ น: docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 --restart=unless-stopped rabbitmq:management
โ†’ management์˜ default admin ์ •๋ณด
โ†’ default username: guest
โ†’ default password: guest
โ€ข
๊ฐ€์ƒ ํ˜ธ์ŠคํŠธ ๋ฐ ์‚ฌ์šฉ์ž ๋“ฑ๋ก ์‹คํ–‰ ๋ช…๋ น
โ†’ docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 --restart=unless-stopped -e RABBITMQ_DEFAULT_USER=cg_user -e RABBITMQ_DEFAULT_PASS=password -e RABBITMQ_DEFAULT_VHOST=cg_vhost rabbitmq:management
โ†’ docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 --restart=unless-stopped -e RABBITMQ_DEFAULT_USER=ogq -e RABBITMQ_DEFAULT_PASS=magicogqworld -e RABBITMQ_DEFAULT_VHOST=backgrounds rabbitmq:management
โ€ข
๋˜๋Š” docker Image ์ ‘์†ํ•ด์„œ ์ง์ ‘ ์‹คํ–‰
โ†’ docker exec -it <docker image> /bin/bash

3. Redis

๊ฐ€. ๊ธฐ๋ณธ ์„ค์ •

1) ์‹œ์ž‘ / ์ข…๋ฃŒ

โ€ข
redis ์„ค์น˜
โ†’ brew install redis
โ€ข
redis ์‹คํ–‰
โ†’ redis-server
โ€ข
redis ์„œ๋ฒ„ ์ •์ƒ ์„ค์น˜ ํ™•์ธ
โ†’ redis server ์‹คํ–‰ํ•œ ์ƒํƒœ์—์„œ ์•„๋ž˜ ๋ช…๋ น์„ ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„์—์„œ ์ž…๋ ฅ
โ†’ redis-cli ping
โ†’ ์‘๋‹ต์œผ๋กœ โ€˜PONGโ€™์„ ๋ฐ›์œผ๋ฉด ์ •์ƒ ์„ค์น˜
โ€ข
redis ์„œ๋ฒ„ ์ •์ง€
โ†’ redis-cli shutdown
โ€ข
celery[redis] ์„ค์น˜ ํ•„์š”
โ†’ ๋ฏธ์„ค์น˜ ์‹œ Cellery๊ฐ€ Redis๋ฅผ ์ฐพ์ง€๋ชปํ•˜๊ณ  NonType ๋ฐ˜ํ™˜ ์ด์Šˆ ๋ฐœ์ƒ
โ†’ pip install celery[redis]

2) ์„ค์ •

โ€ข
broker url ์ˆ˜์ •

3) Redis Server Error

โ€ข
ํ˜„์ƒ
โ€ข
ํ•ด๊ฒฐ๋ฐฉ๋ฒ•
โ†’ ps -ef |grep redis
โ†’ kill -9 <PID>

Reference

โ€ข
rabbitmq with docker, https://hub.docker.com/_/rabbitmq
โ€ข
๊ธฐ๋ณธ๊ฐœ๋… ์ฐธ๊ณ , https://www.cloudamqp.com/blog/what-is-message-queuing.html
โ€ข