簡體   English   中英

第一個使用connexion + Flask + Swagger的python微服務出錯

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM