I am trying to build a very simple RabbitMQ-FastAPI based application that would be able to run on Kubernetes. Created two separate APIs for producer and consumer. Producer API:
@app.get("/hello/{message}")
def post_message(message: str = Body(..., example="Hello World!")):
try:
connection = pika.BlockingConnection(
pika.ConnectionParameters(host="rabbitmq-0.rabbitmq-headless.keda.svc.cluster.local", port=5672,
credentials=pika.PlainCredentials("user", "PASSWORD")))
channel = connection.channel()
#channel.exchange_declare(exchange='logs', exchange_type='direct')
#severity = ['hello', 'message', 'error']
#messages = ['Hafizur', 'message', 'error']
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body=message)
connection.close()
return {"Successfully sended {} do queue".format(message)}
Consumer API:
@app.post("/message")
def get_message():
try:
connection = pika.BlockingConnection(
pika.ConnectionParameters(host="rabbitmq-0.rabbitmq-headless.keda.svc.cluster.local", port=5672,
credentials=pika.PlainCredentials("user", "PASSWORD")))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_consume(on_message_callback=callback, queue="hello", auto_ack=True)
print(" [*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
From here I am building two docker images, one for the producer and another for the consumer. Using those built two deployments on K8S. Did port forwarding and accessed to the producer API and tried to send some message to RabbitMQ queue. But getting Error: Unprocessable Entity.
I am seeking your help to solve this issue.