Skip to main content

Simple toolset for implementing microservices architecture with Fast API

Project description

This contains very simple implementation of discovery server and gateway in python as well as a tools for creating microservices including self registration and heartbeat.

Example of usage

Discovery server

from py_micro_services.discovery_server import DiscoveryServer, DiscoveryServerConfig

if __name__ == "__main__":

    config = DiscoveryServerConfig.load("config/discovery_server_config.json")

    server = DiscoveryServer(config)
    server.start()

after starting application the discovery server will run on address specified in the config file. You can watch all connected services on endpoint /dashboard.

Gateway

from py_micro_services.gateway import Gateway, GatewayConfig

if __name__ == "__main__":

    config = GatewayConfig.load("config/gateway_config.json")

    server = Gateway(config)
    server.start()

after starting application the gateway will run on the address specified in the config file.

Microservice

from fastapi import FastAPI
import uvicorn
import argparse
from routes.FirstRouter import FirstRouter
from routes.SecondRouter import SecondRouter
from fastapi import APIRouter

from py_micro_services.core import PyMicroservice

app = FastAPI()
service = PyMicroservice(config_file="config/service_config.json")

# include your routes here
FirstRouter.use_router(APIRouter(prefix="/first"))
app.include_router(FirstRouter(service).get_router(), tags=["Route 1"])

SecondRouter.use_router(APIRouter(prefix="/second"))
app.include_router(SecondRouter(service).get_router(), tags=["Second"])

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--host', type=str, default='127.0.0.1', help='Hostname or IP address to bind to (default: 127.0.0.1)')
    parser.add_argument('--port', type=int, default=5000, help='Port number to bind to (default: 5000)')
    
    args = parser.parse_args()

    try:
        service.start(args.host, args.port)
        uvicorn.run("service_example:app", host=args.host, port=args.port, reload=True)
    finally:
        service.stop()

For microservices an implementation in FastAPI is expected. It is also expected that all endpoints are implemented in their own rotes, that closed in classes that extend PyMicroserviceRouter and registered in a similar way as in code above. The implementation of such a route can look like this

from py_micro_services.core import PyMicroserviceRouter, get, post
from pydantic import BaseModel


class User(BaseModel):
    id: int
    first_name: str
    last_name: str
    email: str
    age: int | None = None

class FirstRouter(PyMicroserviceRouter):
    @get("/ping")
    async def ping(self) -> str:
        return "first router - pong"
    
    @get("/service_info")
    async def service_info(self) -> str:
        return "Service name: " + self._service.config.service_name

    @post("/make_user_older")
    async def make_user_older(self, user: User, years: int) -> User:
        user.age += years
        return user

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

py_micro_services-0.2.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_micro_services-0.2.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file py_micro_services-0.2.0.tar.gz.

File metadata

  • Download URL: py_micro_services-0.2.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for py_micro_services-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5f05ee5a80afbba72279537aae1522387096b290e70e35cd92c284ab3eb7364b
MD5 ead9ebe5c706f6b22f25e4c0641ebba1
BLAKE2b-256 80d5506405e814ef4dbf0bc130ae5cd953629d031d5ffbcef542fef4c74a19ef

See more details on using hashes here.

File details

Details for the file py_micro_services-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_micro_services-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5c3767dab14af6d9af26fad79fe86f4b6f8bb042daf24f253f7b4a76185c467
MD5 28654832bc0ddbca495ef6b022375f35
BLAKE2b-256 297f353dcaeeb9e9f3662d8e186346b58994062171a8dfb0dfa7997369f96151

See more details on using hashes here.

Supported by

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