简体   繁体   English

链接的Dtab表达式未按预期转换

[英]linkerd Dtab expression not transforming as expected

I've got linkerd running locally per this getting started page , and the basic proxy example works. 此入门页面上 ,我已经在本地运行linkerd ,并且基本的代理示例有效。 My use case requires me to proxy web requests to one of several .NET WebApi services, so I made a little sample WebApi project running locally that has two routes: 我的用例要求我将Web请求代理到多个.NET WebApi服务之一,因此我制作了一个在本地运行的小示例WebApi项目,该项目具有两条路线:

  1. localhost:58371 returns hello world 本地主机:58371返回hello world
  2. localhost:58371/api/values returns ["value1", "value2"] 本地主机:58371 / api / values返回["value1", "value2"]

Mapping requests with matching routes 用匹配的路线映射请求

If I leave my linkerd.yaml file like it comes out-of-the-box: 如果我将linkerd.yaml文件保留为开箱即用:

routers: - protocol: http dtab: | /svc => /#/io.l5d.fs

then mapping routes directly works: 然后直接映射路线:

curl -H "Host: web" http://localhost:4140/ --> hello world curl -H "Host: web" http://localhost:4140/ -> hello world

curl -H "Host: web" http://localhost:4140/api/values --> ["value1", "value2"] curl -H "Host: web" http://localhost:4140/api/values > ["value1", "value2"]

Mapping mismatched routes 映射不匹配的路线

Now, I would like to map localhost:4140/tacos --> localhost:58371/api/values . 现在,我想映射localhost:4140/tacos localhost:58371/api/values > localhost:58371/api/values So, I update my .yaml file to: 因此,我将.yaml文件更新为:

routers: - protocol: http dtab: | /svc => /#/io.l5d.fs; /tacos => /api/values; and restart linkerd. 并重新启动linkerd。

However, http://localhost:4140/ always seems to resolve to http://localhost:58371/tacos , not http://localhost:58371/api/values . 但是, http://localhost:4140/似乎总是解析为http://localhost:58371/tacos ,而不是http://localhost:58371/api/values What am I not understanding? 我不明白什么?

I've read through a bunch of example dtab transformations on linkerd's site, and I've played around with a bunch of different configurations in my yaml file. 我已经在linkerd的站点上阅读了许多示例dtab转换,并且在yaml文件中使用了许多不同的配置。 Surely it's just something silly that I'm missing because this seems like a really simple use case. 当然,我缺少的只是一些愚蠢的事情,因为这似乎是一个非常简单的用例。

By default, linkerd uses the value of the HTTP Host header associated with the request to route traffic. 默认情况下,linkerd使用与请求关联的HTTP Host标头的值来路由流量。 In your example, you're setting Host: web , so all traffic is going to the "web" service that's found in service discovery, regardless of the request URI's path. 在您的示例中,您将设置Host: web ,因此所有流量都将流向服务发现中找到的“ web”服务,而不管请求URI的路径如何。

So rather than sending your request to localhost:4140/tacos , you should send it to -H 'Host: tacos' localhost:4140/api/values . 因此,应将请求发送到-H 'Host: tacos' localhost:4140/api/values ,而不是将请求发送到localhost:4140/tacos You would also need to adjust your dtab as follows: 您还需要按以下方式调整dtab:

/svc       => /#/io.l5d.fs;
/svc/tacos => /svc/web;

That will route all traffic with Host: tacos to the "web" service that's found in service discovery. 它将使用Host: tacos将所有流量路由到服务发现中找到的“ Web”服务。 This is just the default configuration, however. 但是,这只是默认配置。

If you're interested in routing based on the URI path instead of the HTTP Host header, you can use linkerd's path identifier . 如果您对基于URI路径而不是HTTP Host标头的路由感兴趣,可以使用linkerd的路径标识符 Something like: 就像是:

routers:
- protocol: http
  identifier:
    kind: io.l5d.path
    segments: 1
  dtab: |
    /svc => /#/io.l5d.fs;

With that configuration, a request to localhost:4140/api/values would be routed to the "api" service in service discovery, and a request to localhost:4140/tacos would be routed to the "tacos" service, and both of those routing decisions can be changed by modifying your dtab. 使用该配置,对localhost:4140/api/values的请求将被路由到服务发现中的“ api”服务,而对localhost:4140/tacos的请求将被路由到“ tacos”服务,两者可以通过修改dtab更改路由决策。

I should note that linkerd does not do arbitrary HTTP URI path rewriting based on dtab rules. 我应该注意,linkerd不会基于dtab规则进行任意的HTTP URI路径重写。 Since it's a proxy, it expects to proxy the request without modification, with a few exceptions (such as the consume option on the path identifier). 由于它是代理,因此它希望无需更改即可代理请求,但有一些例外(例如,路径标识符上的consume选项)。 You could always write a linkerd plugin though that would fit your specific use case. 您始终可以编写一个linkerd插件,尽管它适合您的特定用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM