You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#1225 Update ServiceDiscovery documentation and samples to include Custom Providers (#1656)
* Update servicediscovery documentation to include custom provider
* Update servicediscovery.rst
Custom Providers paragraph
* Update servicediscovery.rst
Fix lower/upper case in paragraph name
* Added custom service provider sample
* Minor clarification to custom service discovery provider docs
* Move usings to the top. Use file-scoped namespace declaration
* Moved custom service discovery sample
Added sample to Ocelot.sln
* Added custom service provider sample
* Move usings to the top. Use file-scoped namespace declaration
* Moved custom service discovery sample
Added sample to Ocelot.sln
* Add 2 options/ways of solution development via ConfigureServices
* Upgrade DownstreamService to ASP.NET 7
* Upgrade ApiGateway to ASP.NET 7
* Update README.md
* Removed redundant spring section in config
Move urls config from Program.cs to appsettings.json
* Workaround for the Categories route
* Rename controller: class name should be the same as file name
* Upgrade to Web API app: multiple startup profiles, add Docker profile. Basic microservices template
* Correct registration of IServiceDiscoveryProviderFactory interface
* Update README.md: Fix upstream path because of case sensitivity
* Update servicediscovery.rst: Update Custom Providers section. Add sample solution.
* Update servicediscovery.rst: Update actual code from the sample
* Remove obsolete code
* CS8632 The annotation for nullable reference types should only be used in code within a '#nullable' annotations context
* Revert to previous state
* Revert back to the version from ThreeMammals:develop
* Update servicediscovery.rst: English checking
---------
Co-authored-by: raman-m <[email protected]>
Copy file name to clipboardExpand all lines: docs/features/servicediscovery.rst
+126-6Lines changed: 126 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,13 @@ Ocelot allows you to specify a service discovery provider and will use this to f
7
7
GlobalConfiguration section which means the same service discovery provider will be used for all Routes you specify a ServiceName for at Route level.
8
8
9
9
Consul
10
-
^^^^^^
10
+
------
11
11
12
12
The first thing you need to do is install the NuGet package that provides Consul support in Ocelot.
13
13
14
-
``Install-Package Ocelot.Provider.Consul``
14
+
.. code-block:: powershell
15
+
16
+
Install-Package Ocelot.Provider.Consul
15
17
16
18
Then add the following to your ConfigureServices method.
17
19
@@ -92,7 +94,7 @@ Or
92
94
}
93
95
94
96
ACL Token
95
-
---------
97
+
^^^^^^^^^
96
98
97
99
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.
98
100
@@ -108,13 +110,15 @@ If you are using ACL with Consul Ocelot supports adding the X-Consul-Token heade
108
110
Ocelot will add this token to the Consul client that it uses to make requests and that is then used for every request.
109
111
110
112
Eureka
111
-
^^^^^^
113
+
------
112
114
113
115
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.
114
116
115
117
The first thing you need to do is install the NuGet package that provides Eureka support in Ocelot.
116
118
117
-
``Install-Package Ocelot.Provider.Eureka``
119
+
.. code-block:: powershell
120
+
121
+
Install-Package Ocelot.Provider.Eureka
118
122
119
123
Then add the following to your ConfigureServices method.
120
124
@@ -150,7 +154,7 @@ Ocelot will now register all the necessary services when it starts up and if you
150
154
Ocelot will use the scheme (http/https) set in Eureka if these values are not provided in ocelot.json
151
155
152
156
Dynamic Routing
153
-
^^^^^^^^^^^^^^^
157
+
---------------
154
158
155
159
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.
156
160
@@ -242,3 +246,119 @@ Ocelot also allows you to set DynamicRoutes which lets you set rate limiting rul
242
246
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 DynamicRoutes section.
243
247
244
248
Please take a look through all of the docs to understand these options.
249
+
250
+
Custom Providers
251
+
----------------------------------
252
+
253
+
Ocelot also allows you to create your own ServiceDiscovery implementation.
254
+
This is done by implementing the ``IServiceDiscoveryProvider`` interface, as shown in the following example:
This solution is ready for any deployment. All services are bound, meaning all ports and hosts are prepared for immediate use (running in Visual Studio).
311
+
312
+
All instructions for running this solution are in `README.md <../../samples/OcelotServiceDiscovery/README.md>`_.
313
+
314
+
DownstreamService
315
+
"""""""""""""""""
316
+
317
+
This project provides a single downstream service that can be reused across `ApiGateway <#apigateway>`_ routes.
318
+
It has multiple **launchSettings.json** profiles for your favorite launch and hosting scenarios: Visual Studio running sessions, Kestrel console hosting, and Docker deployments.
319
+
320
+
ApiGateway
321
+
""""""""""
322
+
323
+
This project includes a custom Service Discovery provider and it only has route(s) to `DownstreamService <#downstreamservice>`_ services in the **ocelot.json** file.
324
+
You can add more routes!
325
+
326
+
The main source code for the custom provider is in the `ServiceDiscovery <../../samples/OcelotServiceDiscovery/ApiGateway/ServiceDiscovery>`_ folder:
327
+
the ``MyServiceDiscoveryProvider`` and ``MyServiceDiscoveryProviderFactory`` classes. You are welcome to design and develop them!
328
+
329
+
Additionally, the cornerstone of this custom provider is the ``ConfigureServices`` method, where you can choose design and implementation options: simple or more complex:
330
+
331
+
.. code-block:: csharp
332
+
333
+
builder.ConfigureServices(s=>
334
+
{
335
+
// Perform initialization from application configuration or hardcode/choose the best option.
336
+
booleasyWay=true;
337
+
338
+
if (easyWay)
339
+
{
340
+
// Design #1. Define a custom finder delegate to instantiate a custom provider under the default factory, which is ServiceDiscoveryProviderFactory
The easy way, lite design means that you only design the provider class, and specify ``ServiceDiscoveryFinderDelegate`` object for default ``ServiceDiscoveryProviderFactory`` in Ocelot core.
359
+
360
+
A more complex design means that you design both provider and provider factory classes.
361
+
After this, you need to add the ``IServiceDiscoveryProviderFactory`` interface to the DI-container, removing the default registered ``ServiceDiscoveryProviderFactory`` class.
362
+
Note that in this case the Ocelot core will not use ``ServiceDiscoveryProviderFactory`` by default.
363
+
Additionally, you do not need to specify ``"Type": "MyServiceDiscoveryProvider"`` in the **ServiceDiscoveryProvider** properties of the **GlobalConfiguration** settings.
364
+
But you can leave this ``Type`` option for compatibility between both designs.
0 commit comments