如何解决没有 Docker 的 Traefik v2 反向代理
我有一个简单的 Golang 微服务(没有 Docker,只是简单的二进制文件),它在 GET 请求时返回简单的消息。
curl -XGET 'http://localhost:36001/apI/Operability/list'
{"message": "ping 123"}
现在我想通过 Traefik-v2 做反向代理,所以我制作了配置文件“traefik.toml”:
[global]
checkNewVersion = false
sendAnonymousUsage = false
[entryPoints]
[entryPoints.web]
address = ":8090"
[entryPoints.traefik]
address = ":8091"
[log]
level = "DEBUG"
filePath = "logs/traefik.log"
[accessLog]
filePath = "logs/access.log"
[api]
insecure = true
dashboard = true
[providers]
[providers.file]
filename = "traefik.toml"
# dynamic conf
[http]
[http.routers]
[http.routers.my-router]
rule = "Path(`/proxy`)"
service = "my-service"
entryPoints = ["web"]
[http.services]
[http.services.my-service.loadBalancer]
[[http.services.my-service.loadBalancer.servers]]
url = "http://localhost:36001"
启动 Traefik(我使用的是二进制分发版):
traefik --configFile=traefik.toml
现在端口 8091 上的仪表板工作起来很有吸引力,但我在处理反向代理请求方面很挣扎。我想它应该是这样的(基于我的配置文件):
curl -XGET 'http://localhost:8090/proxy/apI/Operability/list'
但我只知道:
未找到 404 页面
编辑: 我的配置文件基于以下问题的答案:
编辑 #2: Traefik 版本信息:
traefik version
Version: 2.4.9
Codename: livarot
Go version: go1.16.5
Built: 2021-06-21T16:17:58Z
OS/Arch: windows/amd64
解决方法
我已经找到了答案。
- 如果我决定 Traefik 将采用 /proxy 并简单地将所有请求重定向到 /api/*,我就不是那么聪明了。官方文档 (https://doc.traefik.io/traefik/routing/routers/) 说(我在引用):
如果您的服务仅侦听确切路径,请使用 Path。例如,路径:/products 将匹配 /products 但不匹配 /products/shoes。
如果您的服务在特定基本路径上侦听,但也在子路径上提供请求,请使用 前缀 匹配器。例如,PathPrefix: /products 将匹配 /products 但也匹配 /products/shoes 和 /products/shirts。由于路径按原样转发,您的服务应该侦听 /products。
- 我没有使用任何中间件来替换路径的子字符串
现在作为例子回答。
首先:main.go 文件中的微服务代码
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter,r *http.Request) {
fmt.Fprintf(w,"{\"message\": \"ping 123\"}")
}
func main() {
http.HandleFunc("/operability/list",handler)
log.Fatal(http.ListenAndServe(":36001",nil))
}
现在, config.tom 文件中 Traefik v2 的配置文件:
[global]
checkNewVersion = false
sendAnonymousUsage = false
[entryPoints]
[entryPoints.web]
address = ":36000"
[entryPoints.traefik]
address = ":8091"
[log]
level = "DEBUG"
filePath = "logs/traefik.log"
[accessLog]
filePath = "logs/access.log"
[api]
insecure = true
dashboard = true
[providers]
[providers.file]
debugLogGeneratedTemplate = true
# Point this same file for dynamic configuration
filename = "config.toml"
watch = true
[http]
[http.middlewares]
[http.middlewares.test-replacepathregex.replacePathRegex]
# We need middleware to replace all "/proxy/" with "/api/"
regex = "(?:^|\\W)proxy(?:$|\\W)"
replacement = "/api/"
[http.routers]
[http.routers.my-router]
# We need to handle all request with pathes defined as "/proxy/*"
rule = "PathPrefix(`/proxy/`)"
service = "my-service"
entryPoints = ["web"]
# Use of defined middleware for path replacement
middlewares = ["test-replacepathregex"]
[http.services]
[http.services.my-service.loadBalancer]
[[http.services.my-service.loadBalancer.servers]]
url = "http://localhost:36001/"
启动微服务:
go run main.go
启动traefik:
traefik --configFile config.toml
现在检查微服务是否正常工作:
curl -XGET 'http://localhost:36001/api/operability/list'
{"message": "ping 123"}
并检查 Traefik v2 是否也能正常工作:
curl -XGET 'http://localhost:36000/proxy/operability/list'
{"message": "ping 123"}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。