Microservices architecture is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. Each microservice focuses on a specific business capability and can be developed, deployed, and scaled independently.
FastAPI is a web framework built on top of Starlette and Pydantic. It uses Python type hints to provide automatic data validation, serialization, and documentation generation. FastAPI is known for its high performance, thanks to its asynchronous programming capabilities and the use of modern Python features.
First, you need to install FastAPI and its dependencies. You can use pip
to install them:
pip install fastapi uvicorn
Here, uvicorn
is an ASGI server that will run your FastAPI application.
The following is a simple example of a FastAPI microservice that returns a “Hello, World!” message:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
To run this microservice, save the code in a file named main.py
and run the following command in the terminal:
uvicorn main:app --reload
The --reload
option enables auto - reloading, which is useful during development. Now, you can access the microservice at http://127.0.0.1:8000/
in your browser.
FastAPI allows you to define different routes for different HTTP methods. For example, the following code defines a route to handle POST requests and accepts a JSON payload:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
def create_item(item: Item):
return item
In this example, the Item
class is a Pydantic model that defines the structure of the incoming JSON data. FastAPI will automatically validate the data and deserialize it into an Item
object.
In a microservice, it’s important to handle errors gracefully. FastAPI provides a way to define custom exception handlers. For example:
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get("/items/{item_id}")
def read_item(item_id: int):
if item_id not in [1, 2, 3]:
raise HTTPException(status_code=404, detail="Item not found")
return {"item_id": item_id}
Here, if the item_id
is not in the allowed list, a 404
error will be returned with a custom error message.
Proper logging is essential for debugging and monitoring microservices. You can use the built - in Python logging
module in FastAPI. For example:
import logging
from fastapi import FastAPI
app = FastAPI()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@app.get("/")
def read_root():
logger.info("Root endpoint accessed")
return {"Hello": "World"}
For secure microservices, you need to implement authentication and authorization. FastAPI supports various authentication methods, such as OAuth2, JWT, etc. Here is a simple example of using basic authentication:
from fastapi import Depends, FastAPI, HTTPException
from fastapi.security import HTTPBasic, HTTPBasicCredentials
app = FastAPI()
security = HTTPBasic()
def get_current_username(credentials: HTTPBasicCredentials = Depends(security)):
if credentials.username != "admin" or credentials.password != "password":
raise HTTPException(
status_code=401,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username
@app.get("/users/me")
def read_current_user(username: str = Depends(get_current_username)):
return {"username": username}
Containerizing your FastAPI microservices with Docker makes it easier to deploy and manage them across different environments. Here is a simple Dockerfile
for a FastAPI microservice:
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install fastapi uvicorn
# Make port 8000 available to the world outside this container
EXPOSE 8000
# Run app.py when the container launches
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
You can build the Docker image using the following command:
docker build -t my-fastapi-microservice .
And run the container:
docker run -p 8000:8000 my-fastapi-microservice
A service registry is a key component in a microservices architecture. It helps microservices discover and communicate with each other. Tools like Consul or etcd can be used as service registries. FastAPI microservices can register themselves with the service registry and look up other services when needed.
Monitoring the performance and health of your microservices is crucial. You can use tools like Prometheus and Grafana to collect and visualize metrics. FastAPI has middleware that can be used to integrate with Prometheus for collecting metrics such as request latency and throughput.
Building microservices with FastAPI offers many advantages, including high performance, easy development, and automatic documentation. By following the fundamental concepts, usage methods, common practices, and best practices outlined in this blog, you can create robust, scalable, and secure microservices. Whether you are building a small - scale application or a large - scale enterprise system, FastAPI can be a great choice for your microservices architecture.