[译]Ocelot

原文

你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port。

Consul

下面的配置要放在GlobalConfiguration中。如果你没有指定host和port,那么就需要一个service discovery provider,默认使用的是Consul。

"ServiceDiscoveryProvider": {
    "Host": "localhost",
    "Port": 8500,
    "Type": "Consul"
}

为了让Ocelot知道一个ReRoute是通过service discovery provider找host和port,必须给ReRoute加上ServiceNameUseServiceDiscovery。如果要使用负载均衡处理downstream的请求,还要指定负载均衡的算法。目前Ocelot支持RoundRobinLeastConnection算法。

{
    "DownstreamPathTemplate": "/api/posts/{postId}",
    "DownstreamScheme": "https",
    "UpstreamPathTemplate": "/posts/{postId}",
    "UpstreamHttpMethod": [ "Put" ],
    "ServiceName": "product",
    "LoadBalancerOptions": {
        "Type": "LeastConnection"
    },
    "UseServiceDiscovery": true
}

如果你想直接从consul中拉取最新的services,而不是每次请求都去consul中请求的话,可以加上如下配置:

"ServiceDiscoveryProvider": {
    "Host": "localhost",
    "Port": 8500,
    "Type": "PollConsul",
    "PollingInterval": 100
}

PollingInterval的单位为毫秒,它告诉Ocelot去调用Consul获取服务配置的频率。

服务需要如下一样添加到Consul中去。注意了这里不需要添加scheme。

"Service": {
       "ID": "some-id",
       "Service": "some-service-name",
       "Address": "localhost",
       "Port": 8080
   }

ACL Token

可以使用ACL和Consul交互,ocelot支持添加X-Consul-Token请求头。为了启用,必须添加下面的配置:

"ServiceDiscoveryProvider": {
    "Host": "localhost",
    "Port": 8500,
    "Token": "footoken"
}

Ocelot在每次请求consul的时候会带上这个token。

Dynamic Routing

这种模式下,ocelot会使用上游path的第一个segment去service discovery provider中查找对应的下游服务。

例如,如果通过https://api.mywebsite.com/product/products这个url请求ocelot。ocelot会使用这个url种的第一个segment,也就是product,作为key去consul里面查找服务。如果consul返回了一个service,ocelot会请求这个service的host+port+原始url第一个segment后面的path来访问下游,这个例子中会访问http://hostfromconsul:portfromconsul/products

为了启用dynamic routing,配置里面的ReRoutes应该是空的。另外需要指定service discovery provider。

{
    "ReRoutes": [],
    "Aggregates": [],
    "GlobalConfiguration": {
        "RequestIdKey": null,
        "ServiceDiscoveryProvider": {
            "Host": "localhost",
            "Port": 8500,
            "Type": "Consul",
            "Token": null,
            "ConfigurationKey": null
        },
        "RateLimitOptions": {
            "ClientIdHeader": "ClientId",
            "QuotaExceededMessage": null,
            "RateLimitCounterPrefix": "ocelot",
            "DisableRateLimitHeaders": false,
            "HttpStatusCode": 429
        },
        "QoSOptions": {
            "ExceptionsAllowedBeforeBreaking": 0,
            "DurationOfBreak": 0,
            "TimeoutValue": 0
        },
        "BaseUrl": null,
            "LoadBalancerOptions": {
            "Type": "LeastConnection",
            "Key": null,
            "Expiry": 0
        },
        "DownstreamScheme": "http",
        "HttpHandlerOptions": {
            "AllowAutoRedirect": false,
            "UseCookieContainer": false,
            "UseTracing": false
        }
    }
}

通过Ocelot还可以设置DynamicReRoutes,通过设置它来设置下游服务的rate limiting。

{
    "DynamicReRoutes": [
        {
        "ServiceName": "product",
        "RateLimitRule": {
                "ClientWhitelist": [],
                "EnableRateLimiting": true,
                "Period": "1s",
                "PeriodTimespan": 1000.0,
                "Limit": 3
            }
        }
    ],
    "GlobalConfiguration": {
        "RequestIdKey": null,
        "ServiceDiscoveryProvider": {
            "Host": "localhost",
            "Port": 8523,
        },
        "RateLimitOptions": {
            "ClientIdHeader": "ClientId",
            "QuotaExceededMessage": "",
            "RateLimitCounterPrefix": "",
            "DisableRateLimitHeaders": false,
            "HttpStatusCode": 428
        }
        "DownstreamScheme": "http",
    }
}
原文地址:https://www.cnblogs.com/irocker/p/ocelot-servicediscovery.html