Skip to main content

Microservice gateway using Nats

Project description

Metropolis (Python Microservice Gateway with NATS)

Can we make simple, scalable, observable, operable service gateway?

클라우드 환경(특히 Kubernetes)에서 NATS를 이용해서 쉽게 확장할 수 있는 마이크로서비스 아키텍쳐에 대한 PoC 프로젝트.

Overview

  • 보편적으로 내부 서비스들의 통신은 HTTP 프로토콜 스펙을 전부 이용하지 않는 경우가 많다.

    • NATS를 이용할 경우, 통신을 위해 HTTP 프로토콜 스펙을 지키기 위해 발생하는 오버헤드를 줄일 수 있음.
    • NATS를 이용하면서 HTTP 스펙의 기능이 필요한 경우, 별도로 만들어 붙여야 하는 문제는 있음.
  • 횡단 관심들을 분리하기 위해 API gateway를 사용하는 경우가 있는데, 이 경우 APIGW - backend간 통신 비용을 줄이는데 NATS를 사용하면 성능상 이득을 볼 수 있다.

    • API gateway를 사용하게 되면, 뒷단의 서비스들은 HTTP 스펙으로부터 자유로워 질 수 있음.
    • 이 경우 별도의 NATS 게이트웨이가 필요하게 된다.
  • NATS를 이용하는 워커와, Endpoint에 대한 HTTP 프록시를 제공하는 프레임워크를 만들어 본다.

NATS

https://nats.io/

  • Text based simple protocol
  • Light weight, Powerful performance
  • No persistency
  • Synchronized(Request), Asynchronous(Publish)

Architecture Concept

+- Node-1 --+  +- Node-2 -+  +- Node-3 --+  +- Node-4 -+
|           |  |          |  |           |  |          |
|   NATS ========= NATS ======================= NATS   | < Internal Bus >
|    |      |  |    |     |  |    |      |  |    |     |
| Worker-a  |  |    |     |  |    |      |  | Worker-d |
| Worker-b  |  | Worker-c |  | Worker-a  |  | Worker-c |
|    |      |  |    |     |  |    |      |  |    |     |
|   NATS ========= NATS ======================= NATS   | < External Bus >
|    |      |  |          |  |    |      |  |          |
| HttpProxy |  |          |  | HttpProxy |  |          |
|    |      |  |          |  |    |      |  |          |
+--- | -----+  +----------+  +--- | -----+  +----------+
     |                            |
     +--------------+-------------+
                    |
                  Ingress

Components

Nats

메시징 채널

Worker

Business Logic 수행.

HttpProxy

NATS <-> HTTP로 리퀘스트를 프록싱, API Gateway의 역할을 담당한다.

GET /foo/bar 형식의 URL pattern들을 foo.bar.GET 형식의 subject로 변환하여 Nats 스트림을 통해 메시지를 주고받게 된다.

Example

Application

구현된 간단한 수준의 워커와 http proxy 컴포넌트를 이용해서, Nats를 이용한 API 게이트웨이와 백엔드 워커를 구현하였다.

example/app/proxy.py

nats http gateway

example/app/worker.py

nats worker

example/app/settings.py

proxy와 워커의 태스크 설정등을 관리한다.

example/kubernetes

구현된 워커와 프록시, k8s nats operator를 이용해 클러스터를 전개하고 서비스 셋을 구성하기 위한 yaml definition들을 관리

Steps

# nats operator를 k8s cluster에 설치한다.
$ ./example/scripts/install_nats_operator.sh

# example application을 배포한다.
$ kubectl apply -f ./example/kubernetes

# URL test
$ curl http://example-natsgateway.mysite.local/foo?data=asdf

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metropolis-0.1.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

metropolis-0.1.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file metropolis-0.1.2.tar.gz.

File metadata

  • Download URL: metropolis-0.1.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.3

File hashes

Hashes for metropolis-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4afe5749b90399c7424abd97867fb7e1344bd45a148a45c9e333f416bf31dbed
MD5 b2789e806ba2c2c9f0fe1b26eaec90a2
BLAKE2b-256 229aee75757cfa9ba5478cf6596e6edb8c58464335075000cd511684f93509f2

See more details on using hashes here.

File details

Details for the file metropolis-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: metropolis-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.3

File hashes

Hashes for metropolis-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bc035753bcffd309c07f5da439eb4f50ac91a42457d206f3590ee62f3467b792
MD5 29adf961abbb9816ec2a0dbf6c78e88d
BLAKE2b-256 ccf61a4796085bd068abefcd8e0dd7d6826a61b21f41d6ec3c205e8bb32108ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page