악! Django 실시간채팅 앱 만들기 (3)

Dev_Gony·2024년 3월 18일
0

Redis 서버 구동 및 접속

Redis ( Remote Dictionary Server )

  • 고성능 오픈소스 메모리 Key/Value NoSQL 데이터베이스
  • 주요 사용 사례 : 캐싱, 세션 저장, Pub/Sub, 랭킹서버 등, 그리고 Channels의 ChannelLayer 백엔드
  • 다양한 자료구조를 지원
    - Strings, Bitmaps, Hashes, Lists, Sets, Sorted Sets, Geospatial Indexes 등

레디스는 고성능 오픈소스 메모리 Key/Value NoSQL 데이터베이스


Redis 서버 구동 방법

  • 외부 서비스를 활용
    - Redis Enterprise Cloud의 Free Plan (무료/유료)
    - 다양한 클라우드 벤더의 Redis as a Service 활용 (무료/유료)
  • 로컬에 직접 설치
    - 도커 활용 (추천)
    - OS용 배포판 설치 (윈도우는 미지원이기에 WSL 활용)

개발용으로는 로컬에 직접 설치/구동하는 것이 속도면이나 비용면에서 유리

Redis Enterprise Cloud의 관리형 Redis 서비스는 신용카드 등록없이도 Free Plan을 이용할 수 있기에 로컬 설치가 어려운사람은 개발용으로 사용해도 됨!

Redis Enterprise Cloud 의 Free Plan으로 가입하겠음

한국이 없음..! 그러면 일본해야지...
30MB는 무료니까 이걸로 합시다

Public endpoint와 secret password 에 있는 호스트, 포트, 패스워드 는 임시로 복사해두고

이제 이걸 이용해서 프로젝트 폴더 바로아래에 .env 파일을 만들어주고

CHANNEL_LAYER_REDIS_URL=redis://:[패스워드]@[호스트]:[포트]

를 작성

대괄호는 빼고 @, : 는 살리면 됨

그리고 settings 로 이동해서

BASE_DIR = Path(__file__).resolve().parent.parent


env = Env()
env_path = BASE_DIR / ".env"
if env_path.exists():
    with env_path.open(encoding="utf8") as f:
        env.read_env(f, overwrite=True)

BASE_DIR 정의 아래에서 env_path를 계산하고, .env내역을 환경변수로서 로딩

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# django channels layer
if "CHANNEL_LAYER_REDIS_URL" in env:
    channel_layer_redis = env.db_url("CHANNEL_LAYER_REDIS_URL")
    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {
                "hosts": [
                    {
                        "host": channel_layer_redis["HOST"],
                        "port": channel_layer_redis.get["PORT"] or 6379,
                        "password": channel_layer_redis["PASSWORD"],
                    }
                ]
            }
        }
    }

DATABASES 설정 아래에 CHANNEL_LAYERS 설정을 추가
CHANNELS_LAYER_REDIS_URL 환경변수가 로딩된 상황에서만 이를 파싱하여 CHANNEL_LAYERS 설정


python manage.py shell 을 구동하여

from django.conf import settings

settings.CHANNEL_LAYERS

를 통해 로딩이 되어있는지 확인!

profile
잔디심는 코린이

0개의 댓글

관련 채용 정보