【问题标题】:Traefik v2 reverse proxy without Docker没有 Docker 的 Traefik v2 反向代理
【发布时间】:2021-09-07 16:58:45
【问题描述】:

我有一个简单的 Golang 微服务(没有 Docker,只是简单的二进制文件),它在 GET 请求上返回简单的消息。

curl -XGET 'http://localhost:36001/api/operability/list'

{“消息”:“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 页面未找到

问题是:配置文件有什么错误还是只是请求拼写错误?

编辑: 我的配置文件基于以下问题的答案:

  1. Simple reverse proxy example with Traefik
  2. Traefik v2 as a reverse proxy without docker

编辑#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

【问题讨论】:

    标签: go networking microservices reverse-proxy traefik


    【解决方案1】:

    我已经找到了答案。

    1. 如果我决定 Traefik 将采用 /proxy 并简单地将所有请求重定向到 /api/*,那我就不是那么聪明了。官方文档 (https://doc.traefik.io/traefik/routing/routers/) 说(我引用):

    如果您的服务仅侦听确切的路径,请使用 Path。例如,Path: /products 将匹配 /products 但不匹配 /products/shoes。

    如果您的服务在特定的基本路径上侦听但也为子路径上的请求提供服务,请使用 前缀 匹配器。例如,PathPrefix: /products 将匹配 /products,但也匹配 /products/shoes 和 /products/shirts。由于路径按原样转发,因此您的服务应在 /products 上进行侦听。

    1. 我没有使用任何中间件来替换路径的子字符串

    现在作为例子回答。

    首先: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))
    }
    

    现在,Traefik v2 的配置文件在 config.tom 文件中:

    [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'

    {“消息”:“ping 123”}

    并检查 Traefik v2 是否也能正常工作:

    curl -XGET 'http://localhost:36000/proxy/operability/list'

    {“消息”:“ping 123”}

    【讨论】:

      猜你喜欢
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      相关资源
      最近更新 更多