mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 23:22:51 +08:00

* Fixed Format Issue for Kubernetes ServiceDiscoveryProvider * Fixes broken links (#858) * Fix link to issue 262 * Fixes broken link to issue 340 * Fixed broken link to issue 340 (#857) * Update information for Okta Authorization (#853) * +dynamic claim variables (#855) incl. tests * IOcelotPipelineBuilder.Use(): Return IOcelotPipelineBuilder (#875) Fixes ThreeMammals/Ocelot#685 * Fix UpstreamHost checking when reroutes duplicate validation (#864) * Format json in reame (#877) Format json file in AdministrationApi ReadMe * kubernetes use in cluster (#882) * refactor :kubernetes use in cluster * feat:delete KubeClient * add more flexible method to config ocelot pipeline (#880) * update k8s doc & samples (#885) * refactor :kubernetes use in cluster * feat:delete KubeClient * feat : update k8s doc & samples * Update kubernetes.rst * Fix/issue666 (#889) * cache key now can generate from query string for request with Get Methods and request content for requests with post methods * MD5Helper Added. OutputCacheMiddleware now can generate cache key using method, url and content * unit test created for CacheKeyGenerator * CacheKeyGenerator Registered in OcelotBuilder as singletone * Fix issue #890 IDefinedAggregator can't handle error codes from downstream requests (#892) * Release/13.2.0 (#834) * Fix formatting in getting started page (#752) * updated release docs (#745) * Update README.md (#756) Fixed typo "Ocleot" * Fixed typo there => their (#763) * Some Typo fixes (#765) * Typo algorythm => algorithm (#764) * Typo querystring => query string (#766) * Typo usual => usually (#767) * Typos (#768) * kubernetes provider (#772) * feat: Kubernetes ServiceDiscoveryProvider * 编写k8s测试例子 * feat:fix kube config * feat: remove port * feat : complete the k8s test * feat : add kubeserviceDiscovery test * feat : add kube provider unittest * feat :add kubetnetes docs how to use ocelot with kubetnetes docs * keep the configuration as simple as possible, no qos, no cache * fix: use http * add PollingKubeServiceDiscovery * feat : refactor logger * feat : add pollkube docs * feat:Remove unnecessary code * feat : code-block json * fix issue #661 for Advanced aggregations (#704) * Add Advanced Aggregation Feature * fix overwrite error * distinct data for better performance * remove constructor parameter * fix tests issue * fix tests * fix tests issue * Add UnitTest and AcceptanceTest * fix responseKeys typo * Update SimpleJsonResponseAggregator.cs * change port * Fix code example for SSL Errors (#780) DangerousAcceptAnyServerCertificateValidator has to be set to "true" to disable certification validation, not "false". * Changed wording for ease of reading (#776) Just some wording changes for clarification. * Ignore response content if null (fix #785) (#786) * fix bug #791 (#795) * Update loadbalancer.rst (#796) * UriBuilder - remove leading question mark #747 (#794) * Update qualityofservice.rst (#801) Tiny typo * K8s package (#804) * feat: Kubernetes ServiceDiscoveryProvider * 编写k8s测试例子 * feat:fix kube config * feat: remove port * feat : complete the k8s test * feat : add kubeserviceDiscovery test * feat : add kube provider unittest * feat :add kubetnetes docs how to use ocelot with kubetnetes docs * keep the configuration as simple as possible, no qos, no cache * fix: use http * add PollingKubeServiceDiscovery * feat : refactor logger * feat : add pollkube docs * feat:Remove unnecessary code * feat : code-block json * feat: publish package Ocelot.Provider.Kubernetes * Okta integration (#807) Okta integration * update cliamsParser (#798) * update cliamsParser * update using * IOcelotBuilder opens the IMvcCoreBuilder property for easy customization (#790) * IOcelotBuilder opens the IMvcCoreBuilder property for easy customization * Adjustment code * nuget package (#809) * feat: Kubernetes ServiceDiscoveryProvider * 编写k8s测试例子 * feat:fix kube config * feat: remove port * feat : complete the k8s test * feat : add kubeserviceDiscovery test * feat : add kube provider unittest * feat :add kubetnetes docs how to use ocelot with kubetnetes docs * keep the configuration as simple as possible, no qos, no cache * fix: use http * add PollingKubeServiceDiscovery * feat : refactor logger * feat : add pollkube docs * feat:Remove unnecessary code * feat : code-block json * feat: publish package Ocelot.Provider.Kubernetes * feat : nuget package * fix: Namesapce Spelling wrong * fix:Namesapce Spelling Wrong * Fix: errors when using rate limiting (#811) * Fix: errors when using rate limiting Add: QuotaExceededError class for requesting too much Add: QuotaExceededError error code Add: Add an error when limit is reached Reflact: Extract GetResponseMessage method for getting default or configured response message for requ * Fix: modify check_we_have_considered_all_errors_in_these_tests for adding a new OcelotErrorCode * added missing COPY csproj files (#821) * Add note on In-Process hosting (#816) When using ASP.NET Core 2.2 with In-Process hosting in IIS it's important to use .UseIIS() instead of .UseIISIntegration(). * Fix bug: (#810) If the registered Consul node is unexpectedly down and not restarted immediately, other services should continue to find the registered service. * Fixed Dockerfile (missing Kubernetes) * Revert "Fix bug: (#810)" (#823) This reverts commit 19c80afb05290fac3a144f652cd663c8b513a559. * remove duplicate `IHttpRequester` register (#819) * remove duplicate `IHttpRequester` register * reserve the first * fix HttpRequesterMiddleware does not call next bug (#830) call next so that we can do something with the response, such as add some custom header etc... * Removed Packing to fix issues, will be sorted out after create a nuget package on Nuget.Org (#831) * Allows access to unpass node (#825) * Fix bug: If the registered Consul node is unexpectedly down and not restarted immediately, other services should continue to find the registered service. * fix bug: If the registered Consul node is unexpectedly down and not restarted immediately, other services should continue to find the registered service. * Updated FluentValidations Nuget Package (#833) * Removed Warnings * Make the full DownstreamContext available to user defined aggregators This allows error codes to be handled
251 lines
10 KiB
ReStructuredText
251 lines
10 KiB
ReStructuredText
.. service-discovery:
|
|
|
|
Service Discovery
|
|
=================
|
|
|
|
Ocelot allows you to specify a service discovery provider and will use this to find the host and port
|
|
for the downstream service Ocelot is forwarding a request to. At the moment this is only supported in the
|
|
GlobalConfiguration section which means the same service discovery provider will be used for all ReRoutes
|
|
you specify a ServiceName for at ReRoute level.
|
|
|
|
Consul
|
|
^^^^^^
|
|
|
|
The first thing you need to do is install the NuGet package that provides Consul support in Ocelot.
|
|
|
|
``Install-Package Ocelot.Provider.Consul``
|
|
|
|
Then add the following to your ConfigureServices method.
|
|
|
|
.. code-block:: csharp
|
|
|
|
s.AddOcelot()
|
|
.AddConsul();
|
|
|
|
The following is required in the GlobalConfiguration. The Provider is required and if you do not specify a host and port the Consul default
|
|
will be used.
|
|
|
|
.. code-block:: json
|
|
|
|
"ServiceDiscoveryProvider": {
|
|
"Host": "localhost",
|
|
"Port": 8500,
|
|
"Type": "Consul"
|
|
}
|
|
|
|
In the future we can add a feature that allows ReRoute specfic configuration.
|
|
|
|
In order to tell Ocelot a ReRoute is to use the service discovery provider for its host and port you must add the
|
|
ServiceName and load balancer you wish to use when making requests downstream. At the moment Ocelot has a RoundRobin
|
|
and LeastConnection algorithm you can use. If no load balancer is specified Ocelot will not load balance requests.
|
|
|
|
.. code-block:: json
|
|
|
|
{
|
|
"DownstreamPathTemplate": "/api/posts/{postId}",
|
|
"DownstreamScheme": "https",
|
|
"UpstreamPathTemplate": "/posts/{postId}",
|
|
"UpstreamHttpMethod": [ "Put" ],
|
|
"ServiceName": "product",
|
|
"LoadBalancerOptions": {
|
|
"Type": "LeastConnection"
|
|
},
|
|
}
|
|
|
|
When this is set up Ocelot will lookup the downstream host and port from the service discover provider and load balance requests across any available services.
|
|
|
|
A lot of people have asked me to implement a feature where Ocelot polls Consul for latest service information rather than per request. If you want to poll Consul for the latest services rather than per request (default behaviour) then you need to set the following configuration.
|
|
|
|
.. code-block:: json
|
|
|
|
"ServiceDiscoveryProvider": {
|
|
"Host": "localhost",
|
|
"Port": 8500,
|
|
"Type": "PollConsul",
|
|
"PollingInterval": 100
|
|
}
|
|
|
|
The polling interval is in milliseconds and tells Ocelot how often to call Consul for changes in service configuration.
|
|
|
|
Please note there are tradeoffs here. If you poll Consul it is possible Ocelot will not know if a service is down depending on your polling interval and you might get more errors than if you get the latest services per request. This really depends on how volatile your services are. I doubt it will matter for most people and polling may give a tiny performance improvement over calling Consul per request (as sidecar agent). If you are calling a remote Consul agent then polling will be a good performance improvement.
|
|
|
|
Your services need to be added to Consul something like below (C# style but hopefully this make sense)...The only important thing to note
|
|
is not to add http or https to the Address field. I have been contacted before about not accepting scheme in Address and accepting scheme
|
|
in address. After reading `this <https://www.consul.io/docs/agent/services.html>`_ I don't think the scheme should be in there.
|
|
|
|
.. code-block: csharp
|
|
|
|
new AgentService()
|
|
{
|
|
Service = "some-service-name",
|
|
Address = "localhost",
|
|
Port = 8080,
|
|
ID = "some-id",
|
|
}
|
|
|
|
Or
|
|
|
|
.. code-block:: json
|
|
|
|
"Service": {
|
|
"ID": "some-id",
|
|
"Service": "some-service-name",
|
|
"Address": "localhost",
|
|
"Port": 8080
|
|
}
|
|
|
|
ACL Token
|
|
---------
|
|
|
|
If you are using ACL with Consul Ocelot supports adding the X-Consul-Token header. In order so this to work you must add the additional property below.
|
|
|
|
.. code-block:: json
|
|
|
|
"ServiceDiscoveryProvider": {
|
|
"Host": "localhost",
|
|
"Port": 8500,
|
|
"Token": "footoken",
|
|
"Type": "Consul"
|
|
}
|
|
|
|
Ocelot will add this token to the Consul client that it uses to make requests and that is then used for every request.
|
|
|
|
Eureka
|
|
^^^^^^
|
|
|
|
This feature was requested as part of `Issue 262 <https://github.com/ThreeMammals/Ocelot/issues/262>`_ . to add support for Netflix's
|
|
Eureka service discovery provider. The main reason for this is it is a key part of `Steeltoe <https://steeltoe.io/>`_ which is something
|
|
to do with `Pivotal <https://pivotal.io/platform>`_! Anyway enough of the background.
|
|
|
|
The first thing you need to do is install the NuGet package that provides Eureka support in Ocelot.
|
|
|
|
``Install-Package Ocelot.Provider.Eureka``
|
|
|
|
Then add the following to your ConfigureServices method.
|
|
|
|
.. code-block:: csharp
|
|
|
|
s.AddOcelot()
|
|
.AddEureka();
|
|
|
|
Then in order to get this working add the following to ocelot.json..
|
|
|
|
.. code-block:: json
|
|
|
|
"ServiceDiscoveryProvider": {
|
|
"Type": "Eureka"
|
|
}
|
|
|
|
And following the guide `Here <https://steeltoe.io/docs/steeltoe-discovery/>`_ you may also need to add some stuff to appsettings.json. For example the json below tells the steeltoe / pivotal services where to look for the service discovery server and if the service should register with it.
|
|
|
|
.. code-block:: json
|
|
|
|
"eureka": {
|
|
"client": {
|
|
"serviceUrl": "http://localhost:8761/eureka/",
|
|
"shouldRegisterWithEureka": false,
|
|
"shouldFetchRegistry": true
|
|
}
|
|
}
|
|
|
|
I am told that if shouldRegisterWithEureka is false then shouldFetchRegistry will defaut to true so you don't need it explicitly but left it in there.
|
|
|
|
Ocelot will now register all the necessary services when it starts up and if you have the json above will register itself with
|
|
Eureka. One of the services polls Eureka every 30 seconds (default) and gets the latest service state and persists this in memory.
|
|
When Ocelot asks for a given service it is retrieved from memory so performance is not a big problem. Please note that this code
|
|
is provided by the Pivotal.Discovery.Client NuGet package so big thanks to them for all the hard work.
|
|
|
|
Dynamic Routing
|
|
^^^^^^^^^^^^^^^
|
|
|
|
This feature was requested in `issue 340 <https://github.com/ThreeMammals/Ocelot/issues/340>`_. The idea is to enable dynamic routing when using a service discovery provider (see that section of the docs for more info). In this mode Ocelot will use the first segment of the upstream path to lookup the downstream service with the service discovery provider.
|
|
|
|
An example of this would be calling Ocelot with a url like https://api.mywebsite.com/product/products. Ocelot will take the first segment of
|
|
the path which is product and use it as a key to look up the service in Consul. If Consul returns a service Ocelot will request it on whatever host and port comes back from Consul plus the remaining path segments in this case products thus making the downstream call http://hostfromconsul:portfromconsul/products. Ocelot will apprend any query string to the downstream url as normal.
|
|
|
|
In order to enable dynamic routing you need to have 0 ReRoutes in your config. At the moment you cannot mix dynamic and configuration ReRoutes. In addition to this you need to specify the Service Discovery provider details as outlined above and the downstream http/https scheme as DownstreamScheme.
|
|
|
|
In addition to that you can set RateLimitOptions, QoSOptions, LoadBalancerOptions and HttpHandlerOptions, DownstreamScheme (You might want to call Ocelot on https but talk to private services over http) that will be applied to all of the dynamic ReRoutes.
|
|
|
|
The config might look something like
|
|
|
|
.. code-block:: json
|
|
|
|
{
|
|
"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 also allows you to set DynamicReRoutes which lets you set rate limiting rules per downstream service. This is useful if you have for example a product and search service and you want to rate limit one more than the other. An example of this would be as follows.
|
|
|
|
.. code-block:: json
|
|
|
|
{
|
|
"DynamicReRoutes": [
|
|
{
|
|
"ServiceName": "product",
|
|
"RateLimitRule": {
|
|
"ClientWhitelist": [],
|
|
"EnableRateLimiting": true,
|
|
"Period": "1s",
|
|
"PeriodTimespan": 1000.0,
|
|
"Limit": 3
|
|
}
|
|
}
|
|
],
|
|
"GlobalConfiguration": {
|
|
"RequestIdKey": null,
|
|
"ServiceDiscoveryProvider": {
|
|
"Host": "localhost",
|
|
"Port": 8523,
|
|
"Type": "Consul"
|
|
},
|
|
"RateLimitOptions": {
|
|
"ClientIdHeader": "ClientId",
|
|
"QuotaExceededMessage": "",
|
|
"RateLimitCounterPrefix": "",
|
|
"DisableRateLimitHeaders": false,
|
|
"HttpStatusCode": 428
|
|
}
|
|
"DownstreamScheme": "http",
|
|
}
|
|
}
|
|
|
|
This configuration means that if you have a request come into Ocelot on /product/* then dynamic routing will kick in and ocelot will use the rate limiting set against the product service in the DynamicReRoutes section.
|
|
|
|
Please take a look through all of the docs to understand these options.
|