Implementation of Nameko (microservercies) (pub/sub)
Zubair Shahzad
Senior Software Engineer | Full Stack Engineer| Digitization & Automation Expert | Data Engineer| Data analyst | AI Engineer | Solution Architect | Software Architect |TOGAF Certified Professional
Communication of nameko service with non nameko services over amqp protocol using pub/sub design pattern.
Nowadays? in micro services we are? using different types of technologies to develop micro services and all the services communicate through the message broker like Rabbitmq.
For example you have some micro services in Microsoft dotnet grpc or any other services that communicate with each other? on rabbitmq and you want that service in nameko project .
In this article we will try to implement a basic? pub/sub pattern using nameko that will communicate to other services . you can receive and push a message to non nameko services on rabbitmq
In this example we will listen to the queue . any message will received by? queue? ,? we will pass this? message to other service then we will publish it to other exchange and queue where? other non nameko service can listen?
Code:
领英推荐
test.py
from nameko.messaging import consum
from nameko.messaging import Publisher
from kombu.messaging import Exchange, Queue
from nameko.rpc import rpc,RpcProxy
import json
class Consumer:
? name = 'consumer'
? test = Exchange('EX_1', type='direct')
? queue = Queue('Q1', exchange=test)
? service_1 = RpcProxy('service_1')
? @consume(queue)
? def handle_consume(self, body):
? ? ? print("Received message: {0}".format(body))
? ? ? self.service_1.hello("call other service")
class Service1:
? name = "service_1"
? test = Exchange('Ex_2', type='direct')
? tq = Queue('Q2', exchange=test)
?
? publish = Publisher(exchange=test,queue=tq)
? @rpc
? def hello(self, payload):
? ? ? print(payload)
? ? ? x={
? ? ? "Message":"hello ."+payload
? ? ? }
? ? ? print(x)
? ? ? print("{0} said hello!".format(x))
? ? ? self.publish(x)e
nameko run test?--broker amqps://username:[email protected]:5555
Happy coding
Engr Zubair shahzad