How to handle proxying to multiple services with golang and labstack echo

huangapple go评论86阅读模式
英文:

How to handle proxying to multiple services with golang and labstack echo

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言和微服务开发,但我希望我的问题能够有意义:

假设我有一个处理用户操作的微服务,例如创建、显示和更新用户,可在localhost:8081/users上访问。

此外,我还有一个处理事件创建、显示和更新的微服务,可在localhost:8082/events上访问。

在这之上,还有一个网关,可在localhost:8080上访问,它应该作为代理将传入的请求分发到正确的服务。

我找到了一段代码,可以将请求从我的网关重定向到用户服务:

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
    Scheme: "http",
    Host:   "localhost:8081",
})
http.ListenAndServe(":8080", proxy)

但有两件事让我困扰:

  • 我应该如何处理多个微服务的分发?我想要一个条件,例如:“如果客户端请求localhost:8080/users,它应该转到用户服务。如果请求localhost:8080/events,它应该转到事件服务。”(如果这种方法是错误的,请随时告诉我)

  • 正如我在标题中提到的,我正在使用labstack/echo路由器,所以我不想使用http.ListenAndServe(":8080", proxy)来启动我的服务器,而是使用类似以下的方式:

e := echo.New()
e.Start(":8080")

但我找不到如何使用这个工具传递代理作为参数的方法。

英文:

I'm new to Go and Micro-services development, but I hope my question will make sense:

Let's say I have a micro-service handling users actions such as creating, showing & updating an user, available at localhost:8081/users.

Beside that, I have a micro-service handlings events' creation, show & update as well, available at localhost:8082/events.

And, above that, there is a gateway, available at localhost:8080 which is suppose to act as a proxy to dispatch the incoming request to the right service.

I found this piece of code which is working well to redirect from my gateway to my user's service:

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
	Scheme: "http",
	Host:   "localhost:8081",
})
http.ListenAndServe(":8080", proxy)

But two things are bothering me:

  • How am I suppose to handle dispatching on multiple micro-services? I'd like to have a condition such as: "If the client requests localhost:8080/users it should go to the user's service. If he requests localhost:8080/events it should go to the event's service. (Please feel free to tell me if this approach is just wrong)

  • As I mentioned in the title, I'm using the labstack/echo Router, so I don't want to start my server with http.ListenAndServe(":8080", proxy), but with something like

    e := echo.New()
    e.Start(":8080")

But I can't find how to pass a proxy as parameter with this tool.

答案1

得分: 6

感谢您在labstack/echo的GitHub上发布的问题的答案,以下是解决方案:

httputil.ReverseProxy实现了http.Handler接口,所以您可以这样做:

e := echo.New()
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
    Scheme: "http",
    Host:   "localhost:8081",
})
e.Any("/users", echo.WrapHandler(proxy))
e.Start(":8080")
英文:

Thanks to an answer on an issue I posted on labstack/echo github, here is the solution:

httputil.ReverseProxy implements http.Handler, so you can do something like:

e := echo.New()
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
    Scheme: "http",
    Host:   "localhost:8081",
})
e.Any("/users", echo.WrapHandler(proxy))
e.Start(":8080")

huangapple
  • 本文由 发表于 2017年2月26日 20:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/42468552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定