简体   繁体   中英

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

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

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

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 = {
    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:

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.

Try for example pymacaron ( http://pymacaron.com/ ). Here is an example of an helloworld api implemented with pymacaron: 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). 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. (2) and implement your endpoints' methods.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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