简体   繁体   English

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

[英]Error in my first python microservices with connexion+Flask+Swagger

i'm trying to make a microservice with python (i'm n00b in this language), well i'm following this tutorial -> https://medium.com/@ssola/building-microservices-with-python-part-i-5240a8dcc2fb 我正在尝试用python制作一个微服务(我用这种语言来说是n00b),我正在按照这个教程 - > https://medium.com/@ssola/building-microservices-with-python-part- I-5240a8dcc2fb

but i'm getting this error: 但是我收到了这个错误:

"flask_app.py", line 115, in run
    raise Exception('Server {} not recognized'.format(self.server))
Exception: Server 9090 not recognized

sorry if my english is not good. 抱歉,如果我的英语不好。

Project structure: 项目结构:

项目结构图

App.py file code 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 file code 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 file code items.py文件代码

items = {
    0: {"name": "First item"}
}


def search() -> list:
    return items

ok... i was able to solve this problem... the problem is in app.py, you must specify the variable port: 好的...我能够解决这个问题...问题是在app.py中,你必须指定变量端口:

INCORRECT 不正确

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

CORRECT 正确

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

There are plenty of microservice frameworks in Python that would simplify a lot the code you have to write. Python中有许多微服务框架可以简化你必须编写的代码。

Try for example pymacaron ( http://pymacaron.com/ ). 试试例如pymacaron( http://pymacaron.com/ )。 Here is an example of an helloworld api implemented with pymacaron: https://github.com/pymacaron/pymacaron-helloworld 以下是使用pymacaron实现的helloworld api的示例: https//github.com/pymacaron/pymacaron-helloworld

A pymacaron service requires you only to: (1) write a swagger specification for your api (which is always a good starting point, whatever language you are using). pymacaron服务只需要:(1)为你的api编写一个swagger规范(无论你使用什么语言,这都是一个很好的起点)。 Your swagger file describes the get/post/etc calls of your api and which objects (json dicts) they get and return, but also which python method in your code that implement the endpoint. 你的swagger文件描述了api的get / post / etc调用以及它们获取和返回的对象(json dicts),以及代码中实现端点的python方法。 (2) and implement your endpoints' methods. (2)并实施你的终端方法。

Once you have done that, you get loads of things for free: you can package your code as a docker container, deploy it to amazon beanstalk, start asynchronous tasks from within your api calls, or get the api documentation with no extra work. 完成后,您可以免费获得大量内容:您可以将代码打包为docker容器,将其部署到amazon beanstalk,从api调用中启动异步任务,或者获取api文档而无需额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM