2 ๋ถ„ ์†Œ์š”

RabbitMQ๋ฅผ ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค๋กœ ์‚ฌ์šฉํ•˜๋Š” Celery!

Django์—์„œ Celery๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์ž‘์„ฑํ•œ ๋ฐ” ์žˆ๋‹ค.

๐Ÿ‘‰ย ๐Ÿ“˜[Django] ์ง„์งœ ๊ฐ„๋‹จํ•œ Celery ์‹คํ—˜

๊ธฐ์กด์—๋Š” ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค๋กœ Redis ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—” RabbitMQ ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ ค ํ•œ๋‹ค.

RabbitMQ?

RabbitMQ ๋ž€, AMQP(Advanced Message Queuing Protocol) ๋ฅผ ๊ตฌํ˜„ํ•œ ์˜คํ”ˆ์†Œ์Šค Message Broker์ด๋‹ค.

  • AMQP(Advanced Message Queuing Protocol) : ์‹ ๋ขฐ์„ฑ ์žˆ๋Š” ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ, ๋ผ์šฐํŒ…, ๋ณด์•ˆ ๋“ฑ์„ ์ง€์›ํ•˜๋Š” ํ‘œ์ค€ ํ”„๋กœํ† ์ฝœ

RabbitMQ ๋Š” producer โ†’ consumer ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•  ๋•Œ, ์ด ์ค‘๊ฐ„ ๊ณผ์ •์—์„œ ๋ธŒ๋กœ์ปค ์—ญํ• ์„ ํ•˜๋Š” ๋…€์„์ด๋‹ค.

์ด ๊ณผ์ •์—์„œ ๋ฉ”์‹œ์ง€๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅํ•˜๊ณ , Queue์— ์Œ“์•„๋‘๋ฉฐ, ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์–ธ์ œ ์‚ฌ์šฉํ•˜์ง€?

  • ์š”์ฒญ์„ ๋งŽ์€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌํ•  ๋•Œ
  • ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ์‹œ๊ฐ„์ด ๊ธธ ๋•Œ
  • ๋งŽ์€ ์ž‘์—…์ด ์š”์ฒญ๋˜์–ด ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•  ๋•Œ

๊ตฌ์„ฑ

Producer

  • ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์ฃผ์ฒด
  • ๋ณด๋‚ด๊ณ ์ž ํ•˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ exchange์— publishํ•จ

Consumer

  • Producer๋กœ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฃผ์ฒด

Exchange

  • Producer๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฉ”์‹œ์ง€๋ฅผ ์–ด๋–ค queue๋กœ ๋ณด๋‚ผ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ณต๊ฐ„
  • 4๊ฐ€์ง€ ํƒ€์ž…์ด ์กด์žฌํ•จ (๋ฉ”์‹œ์ง€ ์ „๋‹ฌ ๋ฐฉ์‹)
    • Direct
    • Fanout
    • Topic
    • Headers

Queue

  • ๋ฉ”์‹œ์ง€๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„
  • ๋ฉ”์‹œ์ง€๋Š” ํ์— ์Œ“์˜€๋‹ค๊ฐ€ ์†Œ๋น„์ž(Consumer)๊ฐ€ ์ด๋ฅผ ๊บผ๋‚ด ์ฒ˜๋ฆฌํ•  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ ์ƒํƒœ๋ฅผ ์œ ์ง€

Binding

  • Exchange์™€ Queue ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ์„ ์ •์˜
  • ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋Š” Exchange๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์ˆ˜์‹ ํ•˜๊ณ , Exchange ํƒ€์ž…๊ณผ Binding ๊ทœ์น™์— ๋”ฐ๋ผ ์ ์ ˆํ•œ Queue๋กœ ์ „๋‹ฌ
  • ๋ณดํ†ต ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • Exchange๊ฐ€ ํŠน์ • Queue๋ฅผ Bindingํ•˜๋„๋ก ์ •์˜ํ•จ (fanout์€ ์˜ˆ์™ธ)

[ChatGPT] RabbitMQ์˜ ์žฅ์ 

์‹ ๋ขฐ์„ฑ

  • RabbitMQ๋Š” ๋ฉ”์‹œ์ง€์˜ ์•ˆ์ „ํ•œ ์ „๋‹ฌ์„ ๋ณด์žฅํ•˜๋ฉฐ, ๋ฉ”์‹œ์ง€ ์†์‹ค์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์˜ต์…˜์„ ์ œ๊ณตํ•จ

์œ ์—ฐ์„ฑ

  • ๋‹ค์–‘ํ•œ ๋ผ์šฐํŒ… ์˜ต์…˜๊ณผ ํ”Œ๋Ÿฌ๊ทธ์ธ ์‹œ์Šคํ…œ์„ ํ†ตํ•ด ์œ ์—ฐํ•œ ๋ฉ”์‹œ์ง€ ํ๋ฆ„ ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•จ

์„ฑ๋Šฅ

  • ๋†’์€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ์ง€์›ํ•˜๊ณ , ํด๋Ÿฌ์Šคํ„ฐ๋ง์„ ํ†ตํ•ด ์„ฑ๋Šฅ์„ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Œ

์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฐ ์ง€์›

  • ์˜คํ”ˆ ์†Œ์Šค์ธ ๋งŒํผ ๋‹ค์–‘ํ•˜๊ณ  ํ™œ๋ฐœํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ ์กด์žฌํ•˜๊ณ , ๋‹ค์–‘ํ•œ ๋ฌธ์„œ์™€ ์˜ˆ์ œ ๋“ฑ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ

[ChatGPT] RabbitMQ์˜ ๋‹จ์ 

๋ณต์žก์„ฑ

  • ์ดˆ๊ธฐ ์„ค์ • ๋ฐ ๊ด€๋ฆฌ๊ฐ€ ๋ณต์žกํ•  ์ˆ˜ ์žˆ์Œ
  • ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ ์‚ฌ์šฉ ์‹œ, ๊นŠ์€ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Œ โ†’ ๋Ÿฌ๋‹ ์ปค๋ธŒ

๋Œ€๊ทœ๋ชจ ์‹œ์Šคํ…œ์—์„œ์˜ ํ•œ๊ณ„

  • ๋งค์šฐ ํฐ ๊ทœ๋ชจ์˜ ์‹œ์Šคํ…œ์—์„œ๋Š” RabbitMQ์˜ ์„ฑ๋Šฅ์ด ์ œํ•œ๋  ์ˆ˜ ์žˆ์Œ
  • ๋Œ€์•ˆ์œผ๋กœ Reids, Kafka ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

Django + Celery + RabbitMQ ์„ธํŒ…

RabbitMQ ์„ค์น˜

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install rabbitmq-server

RabbitMQ ์„ธํŒ…

# user ์ƒ์„ฑ
rabbitmqctl add_user [user_name] [password]
# vhost ์ƒ์„ฑ
rabbitmqctl add_vhost [vhost_name]
# user tag ์ƒ์„ฑ
rabbitmqctl set_user_tags [user_name] [user_tag_name]
# user ๊ถŒํ•œ ์„ค์ • - ".*" ".*" ".*" ๋Š” configure, write, read
rabbitmqctl set_permissions -p [vhost_name] [user_name] ".*" ".*" ".*"

# ์˜ˆ์‹œ
rabbitmqctl add_user kyungtaeklee 123456
rabbitmqctl add_vhost playr_host
rabbitmqctl set_user_tags kyungtaeklee kyungtaeklee_tag
rabbitmqctl set_permissions -p playr_host kyungtaeklee ".*" ".*" ".*"๏ปฟ

Celery ์„ค์น˜

#Celery ์„ค์น˜
pip install celery

#celery ๋ฐฑ์—”๋“œ ์„ค์น˜
pip install -U django-celery-results
python manage.py migrate django_celery_results

Celery ์„ธํŒ…

INSTALLED_APPS = [ 
    ...
    'django_celery_results',
]

CELERY_BROKER_URL = 'amqp://[user_name]:[password]@localhost/[vhost_name]'
CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'

CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TAST_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Seoul'

DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH = 191

์—ฌ๊ธฐ๊นŒ์ง€ ์„ธํŒ…์„ ์™„๋ฃŒ ํ–ˆ๋‹ค๋ฉด, [๐Ÿ“˜[Django] ์ง„์งœ ๊ฐ„๋‹จํ•œ Celery ์‹คํ—˜](https://hellojunho.github.io/django/DJANGO-%EC%A7%84%EC%A7%9C-%EA%B0%84%EB%8B%A8%ED%95%9C-Celery-%EC%8B%A4%ED%97%98/) ์—์„œ ์ง„ํ–‰ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ Celery task์˜ ์„ธํŒ…์„ ์ง„ํ–‰ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

  • CELERY_RESULT_BACKEND: ํƒœ์Šคํฌ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ์œ„์น˜
  • CELERY_CACHE_BACKEND: ํƒœ์Šคํฌ ๊ฒฐ๊ณผ๋‚˜ ๊ธฐํƒ€ ์บ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์œ„์น˜

์„œ๋ฒ„ ์‹คํ–‰

Django์—์„œ Celery๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ ค๋ฉด Django, Celery, Message Broker์˜ ์„œ๋ฒ„ 3๊ฐœ๊ฐ€ ๋ชจ๋‘ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

Docker-Compose ๋ฅผ ์‚ฌ์šฉํ•ด ํ•œ ๋ฒˆ์— ์„œ๋ฒ„๋ฅผ ๋ชจ๋‘ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜๋Š” ํŽธ์ด๋‹ค.

์ด๋ฒˆ์—๋Š” Message Broker๋กœ Redis๊ฐ€ ์•„๋‹Œ RabbitMQ์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰์‹œํ‚ค๋ฉด ๋œ๋‹ค.

# RabbitMQ
sudo rabbitmq-server

# Celery
celery -A config worker -l info

# Django
python manage.py runserver

์ฐธ๊ณ  ์ž๋ฃŒ

[Django + Celery + RabbitMQ] ๋ถ„์‚ฐ ๋น„๋™๊ธฐ ์ž‘์—… ์ˆ˜ํ–‰์„ ์œ„ํ•œ Celery

๐Ÿ“˜[Django] ์ง„์งœ ๊ฐ„๋‹จํ•œ Celery ์‹คํ—˜

[Django] Celery + RabbitMQ ์„ธํŒ…

ํƒœ๊ทธ: , ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ