Building Your First Microservice with Django REST Framework Introduction
In Part 1, we introduced microservices architecture and set up a basic Django-based microservices structure. We identified three independent services:
- Authentication Service – Manages user registration, authentication, and security.
- Inventory Service – Manages the product catalog and stock levels
- Order Service – Manages customer orders and transactions.
Now, in Part 2, we will focus on developing the Authentication microservice, which is a crucial component in any application. This service will handle user registration, login, and authentication using Django REST Framework (DRF) and JWT (JSON Web Token) authentication.
By the end of this part, you will have:
A working User Registration API
A JWT-based Authentication System
A Secure API with Protected Routes
The Authentication Microservice Dockerized
This will lay the foundation for managing secure, scalable, and independent authentication across your microservices-based API system.
Step 1: Setting Up the Authentication Microservice
We will improve the Authentication Service by incorporating the
following features:
- User Registration
- User Login
- JWT-based Authentication
Installing Required Packages
Navigate to the auth_service directory and install the
required dependencies: pip install djangorestframework djangorestframework-simplejwt ##code
Step 2: Conflguring Django REST Framework and JWT
Modify auth_service/settings.py to enable DRF and JWT authentication:
INSTALLED_APPS = [
'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
'rest_framework',
'rest_framework_simplejwt', 'accounts',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}
Step 3: Creating User Registration and Login APIs
User Serializer (auth_service/accounts/serializers.py)
from rest_framework import serializers
from django.contrib.auth import get_user_model
User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
class Meta: model = User
fields = ['id', 'username', 'email', 'password']
def create(self, validated_data):
user = User.objects.create_user(**validated_data) return user
User Registration API (auth_service/accounts/views.py)
from rest_framework import generics
from django.contrib.auth import get_user_model from .serializers import UserSerializer
User = get_user_model()
class RegisterUserView(generics.CreateAPIView): queryset = User.objects.all()
serializer_class = UserSerializer
URL Conflguration (auth_service/accounts/urls.py):
from django.urls import path
from .views import RegisterUserView
urlpatterns = [
path('register/', RegisterUserView.as_view(), name='register'),
]
User Login API with JWT (auth_service/accounts/views.py)
from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
class CustomTokenObtainPairSerializer(TokenObtainPairSerializer): @classmethod
def get_token(cls, user):
token = super().get_token(user) token['username'] = user.username return token
class CustomTokenObtainPairView(TokenObtainPairView): serializer_class = CustomTokenObtainPairSerializer
URL Conflguration (auth_service/accounts/urls.py):
from django.urls import path
from .views import CustomTokenObtainPairView
urlpatterns += [
path('login/', CustomTokenObtainPairView.as_view(), name='login'),
]
Step 4: Dockerizing the Authentication Microservice
To run the Authentication service as a container, create a
Dockerfile: FROM python:3.10
WORKDIR /app
COPY requirements.txt .
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "auth_service.wsgi:application", "--bind", "0.0.0.0:8000"]
Conclusion
In Part 2, we successfully built our flrst microservice:
User Registration API.
JWT Authentication API.
Secured API Endpoints.
Dockerized Authentication Microservice.
📌 In Part 3, we will explore how microservices communicate with each other.