我目前正在学习如何使用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
它返回:
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程序中的方法,使用微服务(稍后)来发送和接收值。
提前谢谢你。
答案 0 :(得分:1)
在配置字典中重命名您的密钥而不是AMQP-URI
尝试此AMQP_URI
CONFIG = {'AMQP_URI': "amqp://guest:guest@<myipserverhere>:5672"}