-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1225 Update ServiceDiscovery documentation and samples to include Cu…
…stom 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]>
- Loading branch information
1 parent
fa179bf
commit 190b001
Showing
27 changed files
with
792 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
**/.classpath | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
11 changes: 11 additions & 0 deletions
11
samples/OcelotServiceDiscovery/ApiGateway/Ocelot.Samples.ServiceDiscovery.ApiGateway.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Ocelot\Ocelot.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Ocelot.DependencyInjection; | ||
using Ocelot.Middleware; | ||
using Ocelot.ServiceDiscovery; | ||
|
||
namespace Ocelot.Samples.ServiceDiscovery.ApiGateway; | ||
|
||
using ServiceDiscovery; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BuildWebHost(args).Run(); | ||
} | ||
|
||
public static IWebHost BuildWebHost(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.ConfigureAppConfiguration((hostingContext, config) => | ||
{ | ||
config | ||
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) | ||
.AddJsonFile("appsettings.json", true, true) | ||
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) | ||
.AddJsonFile("ocelot.json", false, false) | ||
.AddEnvironmentVariables(); | ||
}) | ||
.ConfigureServices(s => | ||
{ | ||
// Initialize from app configuration or hardcode/choose the best option. | ||
bool easyWay = true; | ||
if (easyWay) | ||
{ | ||
// Option #1. Define custom finder delegate to instantiate custom provider | ||
// by default factory which is ServiceDiscoveryProviderFactory | ||
s.AddSingleton<ServiceDiscoveryFinderDelegate>((serviceProvider, config, downstreamRoute) | ||
=> new MyServiceDiscoveryProvider(serviceProvider, config, downstreamRoute)); | ||
} | ||
else | ||
{ | ||
// Option #2. Abstract from default factory (ServiceDiscoveryProviderFactory) and from FinderDelegate, | ||
// and build custom factory by implementation of the IServiceDiscoveryProviderFactory interface. | ||
s.RemoveAll<IServiceDiscoveryProviderFactory>(); | ||
s.AddSingleton<IServiceDiscoveryProviderFactory, MyServiceDiscoveryProviderFactory>(); | ||
// Will not be called, but it is required for internal validators, aka life hack | ||
s.AddSingleton<ServiceDiscoveryFinderDelegate>((serviceProvider, config, downstreamRoute) | ||
=> null); | ||
} | ||
s.AddOcelot(); | ||
}) | ||
.Configure(a => | ||
{ | ||
a.UseOcelot().Wait(); | ||
}) | ||
.Build(); | ||
} |
28 changes: 28 additions & 0 deletions
28
samples/OcelotServiceDiscovery/ApiGateway/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:54060/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"ApiGateway": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "categories", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:5000/" | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
samples/OcelotServiceDiscovery/ApiGateway/ServiceDiscovery/MyServiceDiscoveryProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Ocelot.Configuration; | ||
using Ocelot.ServiceDiscovery.Providers; | ||
using Ocelot.Values; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ocelot.Samples.ServiceDiscovery.ApiGateway.ServiceDiscovery; | ||
|
||
public class MyServiceDiscoveryProvider : IServiceDiscoveryProvider | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly ServiceProviderConfiguration _config; | ||
private readonly DownstreamRoute _downstreamRoute; | ||
|
||
public MyServiceDiscoveryProvider(IServiceProvider serviceProvider, ServiceProviderConfiguration config, DownstreamRoute downstreamRoute) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_config = config; | ||
_downstreamRoute = downstreamRoute; | ||
} | ||
|
||
public Task<List<Service>> Get() | ||
{ | ||
|
||
// Returns a list of service(s) that match the downstream route passed to the provider | ||
var services = new List<Service>(); | ||
|
||
// Apply configuration checks | ||
// ... if (_config.Host) | ||
if (_downstreamRoute.ServiceName.Equals("downstream-service")) | ||
{ | ||
//For this example we simply do a manual match to a single service | ||
var service = new Service( | ||
name: "downstream-service", | ||
hostAndPort: new ServiceHostAndPort("localhost", 5001), | ||
id: "downstream-service-1", | ||
version: "1.0", | ||
tags: new string[] { "downstream", "hardcoded" } | ||
); | ||
|
||
services.Add(service); | ||
} | ||
|
||
return Task.FromResult(services); | ||
} | ||
} |
Oops, something went wrong.