ocelot的主要功能是接收传入的http请求并将其转发到下游服务。ocelot目前仅以另一个http请求的形式支持此功能(将来可能是任何传输机制)。

ocelot描述了将一个请求路由到另一个请求作为reroute。为了在ocelot中工作,您需要在配置中设置reroute。

{
    "reroutes": [
    ]
}

要配置reroute,您需要在reroutes json数组中添加一个。

{
    "downstreampathtemplate": "/api/posts/{postid}",
    "downstreamscheme": "https",
    "downstreamhostandports": [
            {
                "host": "localhost",
                "port": 80,
            }
        ],
    "upstreampathtemplate": "/posts/{postid}",
    "upstreamhttpmethod": [ "put", "delete" ]
}

downstreampathtemplate、downstreamscheme和downstreamhostandports 确定请求的转发。

downstreamhostandports是一个数组,包含请求要转发的主机和端口。通常这只包含一个条目,但有时您可能需要将请求负载平衡到您的下游服务,这是ocelot允许我们添加多个条目,然后选择一个负载均衡器。

upstreampathtemplate是ocelot用来标识哪个downstreampathtemplate用于给定的请求url。 最后,upstreamhttpmethod的使用,可以让ocelot区分对同一个url的请求,并且显然这是需要的工作。

你可以指定一个http请求方法列表,或者一个空的列表以允许任务http请求方法。在ocelot中,你可以使用{something}的方式在模板中添加变量占位符。占位符需要在downstreampathtemplate 和upstreampathtemplate中都添加。如果是这样,当请求到达时ocelot将试图使用上游url中的正确的变量值来替换占位符。

你也可以想这样使用一个reroute处理所有请求:

{
    "downstreampathtemplate": "/api/{everything}",
    "downstreamscheme": "https",
    "downstreamhostandports": [
            {
                "host": "localhost",
                "port": 80,
            }
        ],
    "upstreampathtemplate": "/{everything}",
    "upstreamhttpmethod": [ "get", "post" ]
}

这将转发所有请求到下游服务路径/api。

目前在没有任何配置的请求下,ocelot将默认所有reroutes不区分大小写。 为了改变这种情况,您可以在每个reroute中指定以下设置:

"rerouteiscasesensitive": true

这意味着,当ocelot尝试将上行url与上游模板匹配时将区分大小写。 此设置默认为false,这也是我的建议。因此只有在您希望reroute区分大小写时才用设置它。

捕获所有

ocelot的路由还支持捕获所有样式的路由,用户可以指定他们想要匹配所有流量。如果你像下面那样设置你的配置,请求将被直接代理(它不一定叫url,任何占位符名称都可以)。

{
    "downstreampathtemplate": "/{url}",
    "downstreamscheme": "https",
    "downstreamhostandports": [
            {
                "host": "localhost",
                "port": 80,
            }
        ],
    "upstreampathtemplate": "/{url}",
    "upstreamhttpmethod": [ "get" ]
}

该捕获所有的优先级低于其他任何reroute。 如果你的配置中还有下面的reroute,那么ocelot会在捕获所有配置之前先匹配它。

{
    "downstreampathtemplate": "/",
    "downstreamscheme": "https",
    "downstreamhostandports": [
            {
                "host": "10.0.10.1",
                "port": 80,
            }
        ],
    "upstreampathtemplate": "/",
    "upstreamhttpmethod": [ "get" ]
}

上游主机

此功能允许您基于上游主机进行reroutes。 这是通过查看客户端使用的主机头来工作,然后将其用作识别reroute的信息的一部分。

为了使用这个功能,在你的配置中加上如下配置。

{
    "downstreampathtemplate": "/",
    "downstreamscheme": "https",
    "downstreamhostandports": [
            {
                "host": "10.0.10.1",
                "port": 80,
            }
        ],
    "upstreampathtemplate": "/",
    "upstreamhttpmethod": [ "get" ],
    "upstreamhost": "somedomain.com"
}

上面的reroute只会匹配主机头是somedomain.com的请求。

如果您没有在reroue上设置upstreamhost,则任何主机头都可以匹配它。 这基本上是一个捕获所有功能并保留构建功能时的现有功能。这意味着如果您有两个相同的reroute,其中一个与upstreamhost是null,另一个有值。 ocelot会倾向于设定值的那个。

这个功能在问题 216提出要求。

优先级

在问题 270中,我最终决定在ocelot.json中公开reroute的优先级。这意味着您可以决定上游httprequest与你的reroutes的匹配顺序。

为了是其起作用,将以下内容添加到ocelot.json的reroute中,0仅仅是一个示例值,将在下面解释。

{
    "priority": 0
}

0是最低优先级,ocelot将始终使用0作为/{catchall}路由条目,并且可以硬编码。之后,你可以自由设置你想要的任何优先级。

例如你可以这样:

{
    "upstreampathtemplate": "/goods/{catchall}",
    "priority": 0
}

还可以:

{
    "upstreampathtemplate": "/goods/delete",
    "priority": 1
}

在上面的例子中,如果您向ocelot请求/goods/delete,ocelot将匹配/goods/delete这个reroute。不过在不设置优先级以前它会匹配/goods/{catchall}(因为这是列表中的第一个reroute!)。