pythondjangorabbitmqmicroservicesrabbitmq-exchange

How to communicate two or more microservices in django rest framework?


I have been looking to communicate two or multiple microservices in django and need to make them communicate with each. I've reasearched about it and didnt get proper info about it. what i understood is each microservices application completely dont depend on one another including the database. Now how to communicate each microservices with one another. there are 2 methods **synchronous and asynchronous ** method.

i dont want to use synchronous. how to communicate the endpoints of api in asynchronous way? i found some message brokers like rabbitMQ, kafka, gRPc... Which is the best brokers. and how to communicate using those service? i didnt get any proper guidance. I'm willing to learn , can anybody please explain with some example ? It will be huge boost for my work.


Solution

  • Because you are starting out, I suggest that you go with RabbitMQ. RabbitMQ is easier to set up and use than Apache Kafka.

    Then it depends on what kind of asynchronous communication you are looking for. For example, if you have two microservices; A and B, and you want A to communicate with B, do you want the microservice B to send a response back to A? Or do you want microservice B just to process some task (like send an email) and don't respond back to A?

    Here is a simple example of how to use RabbitMQ in your Django project to process some tasks:

    First, you will need to install django-rabbitmq package to help you communicate with the RabbitMQ broker on both the microservices, sender and reciever:

    pip install django-rabbitmq
    

    Then add django_rabbitmq to your INSTALLED_APPS setting and create a RABBITMQ_CONFIG dictionary in your Django settings file with the connection details for your RabbitMQ server on both the microservices:

    RABBITMQ_CONFIG = {
        'default': {
            'host': 'localhost',
            'port': 5672,
            'user': 'guest',
            'password': 'guest',
        },
    }
    

    Finally, in microservice A, use the django_rabbitmq.publishers.Publisher class to publish messages to RabbitMQ:

    from django.shortcuts import render
    from django_rabbitmq import publishers
    
    def send_message(request):
        # Connect to RabbitMQ and declare a queue
        publisher = publishers.Publisher(queue_name='my_queue')
    
        # Send a message
        publisher.send_message('Hello World!')
    
        return render(request, 'send_message.html', {})
    

    And create a management command in microservice B to consume messages from the queue. You can do this by creating a file management/commands/consume_messages.py with the following code:

    from django.core.management.base import BaseCommand
    from django_rabbitmq import consumers
    
    class Command(BaseCommand):
        help = 'Consumes messages from the specified queue'
    
        def add_arguments(self, parser):
            parser.add_argument('queue_name', type=str, help='The name of the queue to consume messages from')
    
        def handle(self, *args, **options):
            queue_name = options['queue_name']
    
            # Define a callback function to process the received message
            def callback(ch, method, properties, body):
                print(" [x] Received %r" % body)
    
            # Create a message consumer and start consuming messages from the queue
            consumer = consumers.Consumer(queue_name, callback)
            consumer.start_consuming()
    

    Don't forget to start the command to actually start processing tasks from the RabbitMQ:

    python manage.py consume_messages my_queue
    

    You can check out RabbitMQ Getting Started Page to get started using RabbitMQ. And you can find more information and examples in the django-rabbitmq documentation.