【问题标题】:Communication between microservices using go-swagger使用 go-swagger 实现微服务之间的通信
【发布时间】:2021-11-27 22:43:49
【问题描述】:

我的要求是调用第二个 API(服务 B)作为来自经过身份验证的用户对服务 A 的单个请求的一部分。

我正在通过一个计划中的项目练习 go-swagger 框架。我试图实现的一点是从微服务 A 到微服务 B 的真实调用。我已经实现了没有身份验证,但无法验证这个内部调用。
我在微服务 A 端遇到 EOF 错误并且服务停止。我还在微服务 B 端看到 runtime error: index out of range [1] with length 1 错误。

以下是ms-A(遵循goswagger文档)与ms-B通信的代码:

transport := httptransport.New(apiclient.DefaultHost, apiclient.DefaultBasePath, apiclient.DefaultSchemes)
clientm := apiclient.New(transport, strfmt.Default)
bearerTokenAuth:= httptransport.BearerToken(os.Getenv("Authorization"))

resp, err := clientm.Show.Show(show.NewShowParams(),bearerTokenAuth)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%#v\n", resp.Payload.Prompt)

微服务 B 中的 Show 端点是测试此内部通信的简单入口点。我确实为 yaml 文件中的路径添加了安全性,如下所示:

/show:
    get:
      description: "To test the entrypoint"
      operationId: "show"
      tags:
        - "show"
      security:
        - Bearer: [ ]
      responses:
        200:
          description: "Success response on hiting endpoint"
          schema:
            $ref: "#/definitions/Show"
        400:
          description: Bad Request
        404:
          description: items not found
        500:
          schema:
            type: string
          description: Server error

错误

微服务-A 错误
//service-A which is used to hit show-endpoint of service-B
go run cmd/ustore-server/main.go --scheme http --port=8080
2021/10/08 01:47:00 Serving ustore at http://127.0.0.1:8080
2021/10/08 01:47:06 Get "http://localhost:9090/v1/show": EOF
exit status 1
微服务-B 错误
go run ./cmd/datastore-server/main.go --port=9090
2021/10/08 01:32:36 Serving datastore at http://127.0.0.1:9090
2021/10/08 01:34:14 http: panic serving 127.0.0.1:46040: runtime error: index out of range [1] with length 1
goroutine 23 [running]:

【问题讨论】:

  • 我认为 Microservice-A 中的 EOF 只是因为 Microservice-B 在处理请求时发生恐慌并断开连接而不是发送任何内容。 MS-A 发现它所坚持的阅读器意外结束,因此 EOF。如果没有更多关于 MS-B 正在做什么的信息,就很难诊断出这一点,因为这可能就是问题所在。任何地方都可能出现超出范围的错误。在这里,您似乎在只有一个元素的切片中查找了第二项,但如果没有代码,则无法判断发生在哪里。
  • @TroyHurts 谢谢......你已经很好地向我解释了 EOF。我已经分享了服务-B 的 swagger.yaml,我认为这会有所帮助,因为 goswagger 会自动处理处理程序和中间件。是的,你写的是我被困在令牌处理中。我没有管理服务 B 的标题。它现在工作并在我的代码中发布了更正。谢谢

标签: go microservices communication go-swagger


【解决方案1】:

在查看了参数和上面提到的错误之后,我终于解决了这个问题,现在它可以工作了。

这里我改变的是service-A中的代码:

    //token received for user authentication by service-A
    bearerHeader := params.HTTPRequest.Header.Get("Authorization")
    
    //connection with service-B client
    transport := httptransport.New(apiclient.DefaultHost,apiclient.DefaultBasePath,apiclient.DefaultSchemes)
    clientm := apiclient.New(transport, strfmt.Default)
    
    //spliting the key and token(string) and pass only the token part
    bearerTokenAuth:= httptransport.BearerToken(strings.Split(bearerHeader, " ")[1])
    //receive response of the service-B endpoint
    resp, err := clientm.Show.Show(show.NewShowParams(),bearerTokenAuth)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("%#v\n", resp.Payload.Prompt)

【讨论】:

    猜你喜欢
    • 2019-07-12
    • 2022-10-02
    • 2016-06-10
    • 2016-03-05
    • 2018-01-22
    • 1970-01-01
    • 2021-06-20
    • 2019-11-23
    • 2016-08-10
    相关资源
    最近更新 更多