使用 connexion+Flask+Swagger 的 python 个微服务出错

Error in python microservices with connexion+Flask+Swagger

我正在尝试使用 python 制作微服务,我正在关注 this tutorial

但是我收到这个错误:

"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/). Here is an example of an helloworld api implemented with pymacaron: https://github.com/pymacaron/pymacaron-helloworld

pymacaron 服务只需要您: (1) 为您的 api 编写一个 swagger 规范(无论您使用什么语言,这始终是一个很好的起点)。您的 swagger 文件描述了您的 api 的 get/post/etc 调用以及它们获得的对象(json 指令)和 return,还描述了代码中的哪个 python 方法实现端点。 (2) 并实施您的端点方法。

完成后,您可以免费获得很多东西:您可以将代码打包为 docker 容器,将其部署到 amazon beanstalk,从 api 中启动异步任务调用,或无需额外工作即可获取 api 文档。