我的要求是调用第二个 API(服务 B)作为来自经过身份验证的用户对服务 A 的单个请求的一部分。
我正在通过一个计划的项目练习 go-swagger 框架。我试图实现的一点是从微服务 A 到微服务 B 的真实调用。我在没有身份验证的情况下实现了,但无法验证这个内部调用。我在微服务 A 端
遇到错误并且服务停止。我还在微服务 B 端EOF
看到错误。runtime error: index out of range [1] with length 1
以下是与 ms-B 通信的 ms-A(遵循 goswagger 文档)的代码:
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]: