2

我正在尝试使用 python 制作微服务,我正在关注本教程

但我收到了这个错误:

"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
4

2 回答 2

6

好的...我能够解决这个问题...问题出在 app.py 中,您必须指定变量端口:

不正确

app = connexion.App(__name__, 9090, specification_dir='swagger/')

正确的

app = connexion.App(__name__, port=9090, specification_dir='swagger/')
于 2019-01-19T14:58:00.260 回答
-1

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 文档。

于 2019-02-09T16:29:35.340 回答