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.