Gateway 用法

使用方法

原来代码:

  h := handler.NewGeneral()
    _ = general.RegisterGeneralHandler(service.Server(), h)
    if err := service.Run(); err != nil {
        log.Fatal(err.Error())
    }

Api服务:

  h := handler.NewGeneral()
    _ = general.RegisterGeneralHandler(service.Server(), h)
    //if err := service.Run(); err != nil {
    //    log.Fatal(err.Error())
    //}

    ws := gateway.InitWeb(service, h, general.IService())
  // 自定义路由 /webhook/token_xxxxxxx/
    ws.HandleFunc("/webhook/", func(writer http.ResponseWriter, request *http.Request) {})
    if err := ws.Run(); err != nil {
        log.Fatal(err.Error())
    }

Rpc服务:

  h := handler.NewGeneral()
    _ = general.RegisterGeneralHandler(service.Server(), h)
    //if err := service.Run(); err != nil {
    //    log.Fatal(err.Error())
    //}

    // 本地环境运行 web服务调试,其他环境运行 rpc服务
    gateway.RunSrv(service, h, pb.IService())

代码调试

### 老方式,需要启动 Micro Api,路由规则复杂
POST http://172.16.1.220:8081/bensrv/channel/channelService/CheckSubChannelAgoraConnect
Content-Type: application/json
{"subchannelID": 434,"userID": 112,}
### Gate Api直接用本地端口,支持两种路由
# POST http://0.0.0.0:8082/channel/GetSubchannelMembers
POST http://0.0.0.0:8082/GetSubchannelMembers
Content-Type: application/json
{"base":{"subchannelID":17050},"currentUser":197}

日志

ELK日志可以收集到Micro Api 层的错误:

  • 方法名错误:General./General/VoiceAdminDeafen2 404
  • 服务名错误:General./General1/VoiceAdminDeafen 404
  • 参数类型错误:General.VoiceAdminDeafen 400
    {"id":"com.xinyan.beenest.debug.bengapi:VoiceAdminDeafen","code":400,"detail":"error application/json. detail: json: cannot unmarshal string into Go struct field VoiceAdminReq.subchannelID of type int64","status":"Bad Request"}
    

注意事项

# 原来的格式,比较宽松
{
    "subchannelID": "434",
    "userID": "112",
    "xxx":233 // 多余参数json格式不会报错,但proto会报错
}

# gateway的格式,严格匹配字段类型
{
    "subchannelID": 434,
    "userID": 112,
    "xxx":233 // json格式和proto格式,多余参数都不会报错
}
Copyright © xinyan all right reserved,powered by Gitbook该文件修订时间: 2020-07-29 10:10:22

results matching ""

    No results matching ""