登录
首页 >  Golang >  Go问答

实现分布式跟踪的方法:使用 golang 的 http.PostForm

来源:stackoverflow

时间:2024-03-20 10:48:29 126浏览 收藏

实现分布式跟踪时,使用 Go 语言的 http.PostForm 方法存在局限性。要将跟踪跨度从一个服务传递到另一个服务,需要使用 http.NewRequest 创建 POST 请求,注入跟踪标头,然后使用 Client.Do 发送请求。

问题内容

在我的项目中,我尝试使用 opentracing 实现分布式跟踪。

我的微服务具有以下结构。

-- api-gateway
       |_ user-service
       |_ notification

在我的 api 网关中,我启动并在 api 网关中,我使用一个函数来开始跟踪,代码取自设置跟踪器

main() 中:

gatewayTracer := &apiTracer{tracer: startTracing("API Gateway")}

http.HandleFunc("/getemail", gatewayTracer.validatemail)

func (apitracer apiTracer) validatemail(res http.ResponseWriter, req *http.Request) {

    validateEmailSpan := apitracer.tracer.StartSpan("Validate Email")
}

我使用 http.postform()validateemail() 调用我的用户服务。

_, err := http.postform("http://user:7071/checkemail", url.values{"uuid": {uuid}, "email": {email}})

这里 uuid 用于单独的任务,而不是用于跟踪。 我无法使用 postform() 将此 span 发布到下一个服务。

如何解决这个问题?


解决方案


我认为PostForm 无法完成此操作。您需要使用 http.NewRequest 创建 POST 请求,Inject 标头中的跨度并使用 Client.Do 发送请求。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《实现分布式跟踪的方法:使用 golang 的 http.PostForm》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>