Can't connect to RabbitMQ with Flask and Nameko

987 views Asked by At

I'm currently learning how to program in Python and, at the same time, how to use Nameko to make microservices and Flask to make a Website. The idea, here, is to make microservices and let them be available through RabbitMQ while my Flask app is using methods from microservices via RPC and RabbitMQ to display a result or something like that. Of course, RabbitMQ is running and I used iptables to let connections pass. For my first tests, both programs are on the same server. But if Nameko is working perfectly, I can't connect to RabbitMQ with Flask. Here are my codes...

For Nameko program :

# -*-coding:utf-8 -*

# namekotesting.py

from nameko.rpc import rpc

class firstService:
        name = "First microservice"
        result = "";

        @rpc
        def mymethode(self, test):
                result = test
                return result

And it returns :

starting services: First microservice
Connected to amqp://guest:**@127.0.0.1:5672//

And for Flask now :

#! /usr/bin/python
# -*- coding:utf-8 -*-

from flask import Flask, request
from flasgger import Swagger
from nameko.standalone.rpc import ClusterRpcProxy

app = Flask(__name__)
Swagger(app)
CONFIG = {'AMQP-URI': "amqp://guest:guest@<myipserverhere>:5672"}

@app.route('/')

def mymethode():
        with ClusterRpcProxy(CONFIG) as rpc:
                littletest = rpc.mymethode("hello")

def index():
        return littletest

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=5000)

But here is the result on the page, at port 5000 :

 * Serving Flask app "flasktest" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 126-185-375
[03/Jun/2018 18:42:00] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/oyo/flasktest/flasktest.py", line 21, in mymethode
    with ClusterRpcProxy(CONFIG) as rpc:
  File "/usr/local/lib/python2.7/dist-packages/nameko/standalone/rpc.py", line 223, in __enter__
    return self.start()
  File "/usr/local/lib/python2.7/dist-packages/nameko/standalone/rpc.py", line 229, in start
    self._reply_listener.setup()
  File "/usr/local/lib/python2.7/dist-packages/nameko/rpc.py", line 256, in setup
    self.queue_consumer.register_provider(self)
  File "/usr/local/lib/python2.7/dist-packages/nameko/standalone/rpc.py", line 119, in register_provider
    amqp_uri = provider.container.config[AMQP_URI_CONFIG_KEY]
KeyError: 'AMQP_URI'

Could you explain to me what are my mistakes here ? I would like to know how to use methods from Nameko program, to use microservices (later) for sending and receiving values.

Thank you in advance.

1

There are 1 answers

1
Druta Ruslan On BEST ANSWER

rename your key in config dictionary instead of AMQP-URI try this AMQP_URI

CONFIG = {'AMQP_URI': "amqp://guest:guest@<myipserverhere>:5672"}