[译]Ocelot - Service Discovery

原文html

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

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算法。api

{
    "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获取服务配置的频率。ide

服务须要以下同样添加到Consul中去。注意了这里不须要添加scheme。post

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

ACL Token

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

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

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

Dynamic Routing

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

例如,若是经过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",
    }
}
相关文章
相关标签/搜索