无法使用 Flask 和 Nameko 连接到 RabbitMQ

Can't connect to RabbitMQ with Flask and Nameko

我目前正在学习如何在 Python 中编程,同时,如何使用 Nameko 制作微服务和 Flask 制作网站。这里的想法是制作微服务并让它们通过 RabbitMQ 可用,而我的 Flask 应用程序通过 RPC 和 RabbitMQ 使用来自微服务的方法来显示结果或类似的东西。当然,RabbitMQ 是 运行 并且我使用 iptables 让连接通过。对于我的第一次测试,两个程序都在同一台服务器上。但是,如果 Nameko 运行良好,我将无法使用 Flask 连接到 RabbitMQ。这是我的代码...

对于 Nameko 程序:

# -*-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

它 returns :

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

现在对于 Flask:

#! /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)

但这是页面上的结果,在 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'

你能给我解释一下我的错误是什么吗?我想知道如何使用 Nameko 程序中的方法,使用微服务(稍后)发送和接收值。

提前谢谢你。

在配置字典中重命名您的密钥而不是 AMQP-URI 试试这个 AMQP_URI

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