python微服务设计

Nameko + API Swagger

创建项目

## 安装微服务框架
pip install nameko==2.5.4.4
## 安装api框架
pip install nameko-swagger==1.2.7
## 创建项目
nameko-admin createproject demo

项目目录结构

demo/
    .tox/   
    bin/
        run.sh
    conf/
        config.yaml
    logs/
    service/
        demo.py
    spec/
        v1/
            api.yaml
    test/
    ... ##

默认配置

## api.yaml
paths:
  /demo/health:
    get:
      summary: 获取服务健康状态
      tags:
        - demo
      operationId: get_health
      responses:
        default:
          description: |
            返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'
## demo.py
@swagger.unmarshal_request
@http('GET', '/v1/demo/health')
def http_get_health(self, request):
    return dict(code='OK', msg='', data={}, time=int(time.time()))

配置在路径path的作用域中

## api.yaml添加path的路径参数配置
/demo/path/index/{uid}:
    get:
      summary: 根据请求参数在path中进行查询
      tags:
        - demo
      operationId: index
      parameters:
        - name: uid
          in: path        # Note the name is the same as in the path
          description: 用户uid
          required: true
          type: integer
      responses:
        default:
          description: |
            返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'
## demo.py
@swagger.unmarshal_request
@http('GET', '/v1/demo/path/index/<int:uid>')
def path_index(self, request, uid):
    logger.info(request)
    logger.info(uid)
    return dict(code='OK', msg='', data={r"msg": "path index success..."}

配置在路径query作用域中

## api.yaml
/demo/query/index:
    get:
      summary: 根据请求参数在query中查询
      operationId: query_index
      parameters:
        - name: cid
          in: query
          description: 用户频道Id
          required: true
          type: integer
      responses:
        default:
          description: |
           返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'
## demo.py
@swagger.unmarshal_request
@http('GET', '/v1/demo/query/index')
def query_index(self, request, cid):
    logger.info(request)
    logger.info(cid)
    return dict(code='OK',
                msg='',
                data={"cid": cid},
                time=int(time.time()))

配置在query中查询多个

## api.yaml
/demo/query/many:
    get:
      summary: 根据请求参数在query中查询
      operationId: query_many
      parameters:
        - name: cid
          in: query
          description: 用户频道Id
          required: true
          type: integer
          minimum: 1
        - name: uid
          in: query
          description: 用户id
          required: false
          type: integer
          minimum: 1
        - name: channelid
          in: query
          required: false
          type: string
      responses:
        default:
          description: |
           返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'
## demo.py
@swagger.unmarshal_request
@http('GET', '/v1/demo/query/many')
def query_many(self, request,
               cid,
               uid=0,
               channelid=0):
    logger.info(request)
    logger.info(cid)
    logger.info(uid)
    logger.info(channelid)
    return dict(code='OK',
                msg='',
                data={"cid": cid,
                      "uid": uid,
                      "channelid": channelid},
                time=int(time.time()))

在post的表单中查询

## api.yaml
/demo/post:
    post:
      summary: 根据请求参数在post的表单中查询
      operationId: do_post
      parameters:
        - name: cid
          in: formData
          description: 用户频道Id
          required: true
          type: integer
          minimum: 1
        - name: uid
          in: formData
          description: 用户id
          required: false
          type: integer
          minimum: 1
        - name: channelid
          in: formData
          required: false
          type: string
      responses:
        default:
          description: |
           返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'
## demo.py
@swagger.unmarshal_request
@http('POST', '/v1/demo/post')
def do_post(self, request,
               cid,
               uid=0,
               channelid=""):
    logger.info(request)
    logger.info(cid)
    logger.info(uid)
    logger.info(channelid)
    return dict(code='OK',
                msg='do post message',
                data={"cid": cid,
                      "uid": uid,
                      "channelid": channelid},
                time=int(time.time()*1000))

配置在post中查询多个

## api.yaml
/demo/post/query:
    post:
      summary: 根据请求参数在post的表单以及query的路径中查询
      operationId: query_post
      parameters:
        - name: cid
          in: query
          description: 用户频道Id
          required: true
          type: integer
          minimum: 1
        - name: uid
          in: query
          description: 用户id
          required: false
          type: integer
          minimum: 1
        - name: channelid
          in: formData
          required: false
          type: string
      responses:
        default:
          description: |
           返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'
## demo.py
@swagger.unmarshal_request
@http('POST', '/v1/demo/post/query')
def query_post(self, request,
               cid,
               uid=0,
               channelid=""):
    logger.info(request)
    logger.info(cid)
    logger.info(uid)
    logger.info(channelid)
    return dict(code='OK',
                msg='query post message',
                data={"cid": cid,
                      "uid": uid,
                      "channelid": channelid},
                time=int(time.time() * 1000))

配置在body中

## api.yaml
/demo/post/body:
    post:
      summary: 根据请求参数在body中查询
      operationId: query_body
      parameters:
        - in: body
          name: jsonParameters
          schema:
            $ref: '#/definitions/RequestJson'
      responses:
        default:
          description: |
           返回结果:{"code": "xxx", "msg": "xxx", "data": {}, "time": xxx, "why": "xxx"}

            其中`code`字段可能返回的错误码包括:
            * "OK" 操作成功,结果见`data`字段
            * "INTERNAL_SERVER_ERROR" 内部错误,具体原因见`why`字段
          schema:
            $ref: '#/definitions/HealthResponse'

## 定义的requestJson格式
definitions:
  RequestJson:
    description: 请求参数在body中的json格式
    type: object
    properties:
      cid:
         description: 用户的cid
         type: integer
      uid:
         description: 用户的uid
         type: integer
      channelid:
         description: 频道Id
         type: string
    required:
      - cid
## demo.py
@swagger.unmarshal_request
@http('POST', '/v1/demo/post/body')
def query_body(self, request, jsonParameters):
    u"""
    jsonParameters名称要和api.yaml上声明的name一致,请求的body的json格式为
    {
        cid:"",
        uid:"",
        channelid:""
    }
    ## 使用这种可以传递一个对象的json数据而避免使用过长的参数列表来进行request请求
    """
    return dict(code='OK',
                msg='query post message',
                data=jsonParameters,
                time=int(time.time() * 1000))
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疾风先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值