Nameko is a microservices framework for Python that lets service developers concentrate on application logic and encourages testability. In this blog, we build a simple application using microservices architecture with the help of Nameko.
Microservices have become a popular way to build modern, scalable applications. They allow developers to break down large monolithic applications into smaller and more manageable components that can be developed, deployed and managed independently. Nameko is a Python framework that makes building and deploying microservices easy.
What is Nameko?
Nameko is a microservice framework for Python that provides tools and libraries to help you build and deploy microservices quickly and easily. It takes care of many of the common challenges which come while building the microservices, such as service discovery, service communication, and service management. So a developer can focus on writing your application logic.
Key Features of Nameko:
Building a Microservice with Nameko:
Building a microservice with Nameko is straightforward and can be done in just a few steps. Let's build a simple example project to understand how to build a microservice using nameko.
About the sample Project:
- Auth service: Helps to authenticate the user
- Adder service: which takes 2 inputs and gives the addition of it
- Multiplier service: which takes 2 inputs and returns the multiplication of it.
- API service: which will act as a gateway between the frontend and other nameko services
This approach has one disadvantage which is the project will be dependent on the API service (but just for the illustration of the communication between the non-nameko and nameko services I’m using this approach)
When we build the microservice architecture we have to choose between code redundancy and inter-service dependency
This requires RabbitMQ to communicate between the services because Nameko is using the built-in AMQP RPC features.
There are multiple ways to set up the message queue using RabbitMQ. A quicker way is to use the Cloud AMQP. Here’s the doc - cloudAMQP.
We would need the AMQP URL for the configuration of our services.
Let’s Code the services:
Folder structure: create the following folder structure
This will be the non-nameko service. So we are using a simple flask application as the api_gateway service.
This api_gateway service acts as a middleware between the request and the nameko services. This receives the request and routes it to the respective nameko service and returns the response returned by the nameko service.
Since this is a non-nameko service, for this service to communicate with other nameko services we use the ClusterRpcProxy block. We have to configure it with the AMPQ URL
Build the API gateway service:
- To run the service - “python api_gateway.py run”
The authentication service is the nameko service. It has 2 functions.
We Are using @rpc decorator to define functions as the RPC functions so that we could be able to call these functions from other services.
Build the Authentication service:
- Run - “nameko run --config ./config.yaml auth_service”
Note: Having the config.yaml file is not mandatory but it is recommended use. Since you’re building the microservices you may have a lot of services, so having the config.yaml file will help you keep the deployment of services in sync. - If you don't want to create the YAML file you can mention the broker URL in the command line and the service will still run, you can use the following command - “nameko run AuthService --broker amqps://***.cloudamqp.com/***”
Note - It's just for the demonstration of the authentication. This is not how an authentication service should look like
Adder service is another nameko service.
In the adder service, we are calling the auth_service to verify the user.
Build the Adder service:
- Run - “nameko run --config ./config.yaml adder_service”
- if you don't want to create the YAML file you can still run the service using the command - “nameko run adder_service --broker amqps://***.cloudamqp.com/***”
Build the Multiplier service:
- Run - “nameko run --config ./config.yaml multiplier_service”
- if you don't want to create the YAML file you can still run the service using the command - “nameko run multiplier_service --broker amqps://***.cloudamqp.com/***”
Note: Click here to see the code - git_hub_link
Nameko is a powerful and flexible microservice framework that makes it easy to build and deploy microservices in Python. It takes care of many of the common challenges which come while building microservices. So you can focus on writing your application logic. Whether you're building a new application from scratch or migrating an existing monolithic application to a microservices architecture, Nameko is a great choice for quickly building scalable and resilient microservices. However, it may not be the best choice for projects which need to be built in another programming language than python.
Learn how to implement structural design patterns for reusability and maintainability in modern development. In this blog, we code examples of Facade and Decorator patterns to optimize your software design.
Learn about securing desktop applications through penetration and security testing. Explore our five-step process for identifying and mitigating potential threats, and adopt recommended best practices to enhance your application's security.
This blog outlines the process of creating a custom database migration system using Node.js, & any database ORM. Furthermore, it explains creating migration files, writing migration code, and applying Electron-specific changes in order for the migration system to operate effectively in a production environment.