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.1.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.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_micro_services-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a9dc9b40cb0389ed452c7d9918e67e376615bcbc76da31b375c21ee168166b2d
MD5 0e992c2aac5b4f009cc5da00c0a3d2c0
BLAKE2b-256 d0b8b9fd350836165b5d70c107edefe517ccd78e98ba291b2f5869d10699b2fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_micro_services-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f8e51daf4a244ccbc0148dd08bfe0d6d8cd2ca513e3b8f187b35ac1480985b55
MD5 f1f3ed127e3831946a5f8702293f0d79
BLAKE2b-256 41cc6a2787ac30006c472a7fd3f08fd5b97abdfcb32c8fa3947bd2e95e012a60

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