[英]Error in my first python microservices with connexion+Flask+Swagger
我正在尝试用python制作一个微服务(我用这种语言来说是n00b),我正在按照这个教程 - > https://medium.com/@ssola/building-microservices-with-python-part- I-5240a8dcc2fb
但是我收到了这个错误:
"flask_app.py", line 115, in run
raise Exception('Server {} not recognized'.format(self.server))
Exception: Server 9090 not recognized
抱歉,如果我的英语不好。
项目结构:
App.py文件代码
from connexion.resolver import RestyResolver
import connexion
if __name__ == '__main__':
app = connexion.App(__name__, 9090, specification_dir='swagger/')
app.add_api('my_super_app.yaml', resolver=RestyResolver('api'))
app.run()
my_super_app.yaml文件代码
swagger: "2.0"
info:
title: "My first API"
version: "1.0"
basePath: /v1.0
paths:
/items/:
get:
responses:
'200':
description: 'Fetch a list of items'
schema:
type: array
items:
$ref: '#/definitions/Item'
definitions:
Item:
type: object
properties:
id:
type: integer
format: int64
name: { type: string }
items.py文件代码
items = {
0: {"name": "First item"}
}
def search() -> list:
return items
好的...我能够解决这个问题...问题是在app.py中,你必须指定变量端口:
不正确
app = connexion.App(__name__, 9090, specification_dir='swagger/')
正确
app = connexion.App(__name__, port=9090, specification_dir='swagger/')
Python中有许多微服务框架可以简化你必须编写的代码。
试试例如pymacaron( http://pymacaron.com/ )。 以下是使用pymacaron实现的helloworld api的示例: https : //github.com/pymacaron/pymacaron-helloworld
pymacaron服务只需要:(1)为你的api编写一个swagger规范(无论你使用什么语言,这都是一个很好的起点)。 你的swagger文件描述了api的get / post / etc调用以及它们获取和返回的对象(json dicts),以及代码中实现端点的python方法。 (2)并实施你的终端方法。
完成后,您可以免费获得大量内容:您可以将代码打包为docker容器,将其部署到amazon beanstalk,从api调用中启动异步任务,或者获取api文档而无需额外的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.