没有 Docker 的 Traefik v2 反向代理
Traefik v2 reverse proxy without Docker
我有一个非常简单的 Golang 微服务(没有 Docker,只是简单的二进制文件),它 returns 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 page not found
问题是:是配置文件有误还是只是请求错字?
编辑:
我的配置文件基于以下问题的答案:
- Simple reverse proxy example with Traefik
编辑#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/) 说(我在引用):
Use Path if your service listens on the exact path only. For instance, Path: /products would match /products but not /products/shoes.
Use a Prefix matcher if your service listens on a particular base path but also serves requests on sub-paths. For instance, PathPrefix: /products would match /products but also /products/shoes and /products/shirts. Since the path is forwarded as-is, your service is expected to listen on /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"}
我有一个非常简单的 Golang 微服务(没有 Docker,只是简单的二进制文件),它 returns 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 page not found
问题是:是配置文件有误还是只是请求错字?
编辑: 我的配置文件基于以下问题的答案:
- Simple reverse proxy example with Traefik
编辑#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/) 说(我在引用):
Use Path if your service listens on the exact path only. For instance, Path: /products would match /products but not /products/shoes.
Use a Prefix matcher if your service listens on a particular base path but also serves requests on sub-paths. For instance, PathPrefix: /products would match /products but also /products/shoes and /products/shirts. Since the path is forwarded as-is, your service is expected to listen on /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"}