代码改变世界

Traefic 入门

2022-06-02 18:37  qgbo  阅读(196)  评论(0编辑  收藏  举报

1. 下载traefic 的windows 版本 ,和consul 类似,只有一个可执行文件。

2. 建这个文件 myconfigfile.yml, 这文件要符合Go的tmeplate, traefik 是go 写的 ,这个文件的更多配置

global:
  checkNewVersion: true

providers:
  file:
    watch: true
    filename: foobar.yml
    debugLogGeneratedTemplate: true
api:
  insecure: true
  dashboard: true
  debug: true

3。Create  foobar.yml

http:
  middlewares:
    test-replacepath:
      replacePathRegex:
        regex: "/origin/(.*)"
        replacement: "/bar/$1"
    test-stripprefix:
      stripPrefix:
        prefixes:
          - "/foo"
          - "/a=/hello"
          - "/origin/ddd?" 
          
  # Add the router
  routers:
    router0:
      middlewares:
      - test-replacepath
      service: service-foo
      rule: PathPrefix(`/`)

  # Add the middleware

  # Add the service
  services:
    service-foo:
      loadBalancer:
        servers:
        - url: http://localhost:5000/

 

这个文件可以热加载。

4.执行 traefik.exe --configFile=myconfigfile.yml  --accesslog

5.打开浏览器 http://localhost:8080/dashboard/

6.新建 .netcore 项目,修改confgiure ,添加如下中间件,查看中间件改变了啥:

app.Use(async (c, n) => {

                var s= new StringBuilder(c.Request.Path);

                s.AppendLine(c.Request.QueryString.Value);
                s.AppendLine();

                foreach (var item in c.Request.Headers)
                {
                    s.AppendLine($"{item.Key}:{item.Value}");
                }


               var b= Encoding.UTF8.GetBytes(s.ToString());

               await c.Response.Body.WriteAsync(b);
            });
minimal api:
using System.Text;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();


app.MapGet("{*path}", (HttpContext c) =>
{
    var s = new StringBuilder(c.Request.Path);
    s.AppendLine(c.Request.QueryString.Value);
    s.AppendLine();

    foreach (var item in c.Request.Headers)
    {
        s.AppendLine($"{item.Key}:{item.Value}");
    }
    return $"the path: {s}";
});

app.Run();
经常多出的头:
X-Forwarded-For:::1 X-Forwarded-Host:localhost X-Forwarded-Port:80 X-Forwarded-Prefix:/foo X-Forwarded-Proto:http X-Forwarded-Server:DESKTOP-QQUASDG X-Real-Ip:::1
7. entryPoint: 这是监控的那个端口,地址等,这个要重新启动才起效,这个是和 global,provider 同一级
entryPoints:
  web:
    # Listen on port 8081 for incoming requests
    address: :8081
  web2:
    address: ":8089"

8. 一些Middleware:

8.1  replacePath,这个类型,是吧 path 替换掉。path 是端口号之后,?之前的部分,只有一个参数。

这些中间件,都没处理一个东西:就是 QueryString, 就是如何把某个 参数的名称改变下。

这在IIS下,好像也是不行的:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="^proxy/aaa?doc=folder/(.*)$" />
                    <conditions>
                    </conditions>
                    <action type="Rewrite" url="http://localhost:5000/{R:1}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

 

9.  dashboard 可以看到有些默认的services like api, 可以这样访问:  http://localhost:8080/api/http/routers

traefik 能动态检查docker swarm,是因为挂载了 /var/run/docker.sock   https://docs.docker.com/engine/api/v1.43/#tag/Exec/operation/ExecResize

sudo curl --unix-socket /var/run/docker.sock  http:/api/_ping    如果出现 curl: (7) Couldn't connect to server, 尝试加sudo