AppConfiguration provider brings the possibility to divide the routing configuration from the service address definition.
- Define routing configuration
"Routes": [
{
"DownstreamPathTemplate": "/api/users",
"ServiceName": "authorization",
"UpstreamPathTemplate": "/organizations/{organizationId}/users",
"SwaggerKey": "UsersKey"
},
{
"DownstreamPathTemplate": "/api/users/{everything}",
"ServiceName": "authorization",
"UpstreamPathTemplate": "/organizations/{organizationId}/users/{everything}",
"SwaggerKey": "PermissionsKey"
},
{
"DownstreamPathTemplate": "/api/permissions",
"ServiceName": "authorization",
"UpstreamPathTemplate": "/organizations/{organizationId}/permissions",
"SwaggerKey": "PermissionsKey"
},
{
"DownstreamPathTemplate": "/api/permissions/{everything}",
"ServiceName": "authorization",
"UpstreamPathTemplate": "/organizations/{organizationId}/permissions/{everything}",
"SwaggerKey": "PermissionsKey"
},
{
"DownstreamPathTemplate": "/api/todos",
"ServiceName": "toDos",
"UpstreamPathTemplate": "/organizations/{organizationId}/todos",
"SwaggerKey": "TodosKey"
},
{
"DownstreamPathTemplate": "/api/todos/{everything}",
"ServiceName": "toDos",
"UpstreamPathTemplate": "/organizations/{organizationId}/todos/{everything}",
"SwaggerKey": "TodosKey"
},
{
"DownstreamPathTemplate": "/api/organizations",
"ServiceName": "organizations",
"UpstreamPathTemplate": "/organizations",
"SwaggerKey": "OrganizationsKey"
},
{
"DownstreamPathTemplate": "/api/organizations/{everything}",
"ServiceName": "organizations",
"UpstreamPathTemplate": "/organizations/{everything}",
"SwaggerKey": "OrganizationsKey"
}
]
- Define services addresses in
appsettings.json
"Services": {
"organizations": {
"DownstreamPath": "http://localhost:9003"
},
"authorization": {
"DownstreamPath": "http://localhost:9002"
},
"toDos": {
"DownstreamPath": "http://localhost:9001"
}
}
- Define Service Discovery provider
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Type": "AppConfiguration",
"PollingIntervalSeconds": 10000
}
}
- Add AppConfiguration provider in
Startup.cs
dotnet add package MMLib.Ocelot.Provider.AppConfiguration
services.AddOcelot()
.AddAppConfiguration();
This provider from optimization reason cache services 5 minutes by default. If you want change or turn off this caching, then set PollingInterval
in provider definition (miliseconds).
If you want change default section name, than you can set property AppConfigurationSectionName
in provider definition.
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Type": "AppConfiguration",
"PollingIntervalSeconds": 10000,
"AppConfigurationSectionName": "ApiServices"
}
}