mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-17 23:43:22 +08:00
Remove extra level of indentation. Fixed a typo in one of tje json examples
This commit is contained in:
parent
0b4aee7b96
commit
45341df237
200
README.md
200
README.md
@ -62,10 +62,10 @@ configuration is a bit hacky and allows overrides of ReRoute specific settings.
|
|||||||
if you don't want to manage lots of ReRoute specific settings.
|
if you don't want to manage lots of ReRoute specific settings.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"ReRoutes": [],
|
"ReRoutes": [],
|
||||||
"GlobalConfiguration": {}
|
"GlobalConfiguration": {}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
More information on how to use these options is below..
|
More information on how to use these options is below..
|
||||||
|
|
||||||
@ -75,45 +75,45 @@ An example startup using a json file for configuration can be seen below.
|
|||||||
Currently this is the only way to get configuration into Ocelot.
|
Currently this is the only way to get configuration into Ocelot.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Startup
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IHostingEnvironment env)
|
||||||
{
|
{
|
||||||
public Startup(IHostingEnvironment env)
|
var builder = new ConfigurationBuilder()
|
||||||
{
|
.SetBasePath(env.ContentRootPath)
|
||||||
var builder = new ConfigurationBuilder()
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||||
.SetBasePath(env.ContentRootPath)
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
.AddJsonFile("configuration.json")
|
||||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
.AddEnvironmentVariables();
|
||||||
.AddJsonFile("configuration.json")
|
|
||||||
.AddEnvironmentVariables();
|
|
||||||
|
|
||||||
Configuration = builder.Build();
|
Configuration = builder.Build();
|
||||||
}
|
|
||||||
|
|
||||||
public IConfigurationRoot Configuration { get; }
|
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
|
||||||
{
|
|
||||||
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
|
||||||
{
|
|
||||||
x.WithMicrosoftLogging(log =>
|
|
||||||
{
|
|
||||||
log.AddConsole(LogLevel.Debug);
|
|
||||||
})
|
|
||||||
.WithDictionaryHandle();
|
|
||||||
};
|
|
||||||
|
|
||||||
services.AddOcelotOutputCaching(settings);
|
|
||||||
services.AddOcelotFileConfiguration(Configuration);
|
|
||||||
services.AddOcelot();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
||||||
{
|
|
||||||
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
||||||
|
|
||||||
app.UseOcelot();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IConfigurationRoot Configuration { get; }
|
||||||
|
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
||||||
|
{
|
||||||
|
x.WithMicrosoftLogging(log =>
|
||||||
|
{
|
||||||
|
log.AddConsole(LogLevel.Debug);
|
||||||
|
})
|
||||||
|
.WithDictionaryHandle();
|
||||||
|
};
|
||||||
|
|
||||||
|
services.AddOcelotOutputCaching(settings);
|
||||||
|
services.AddOcelotFileConfiguration(Configuration);
|
||||||
|
services.AddOcelot();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||||
|
|
||||||
|
app.UseOcelot();
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is pretty much all you need to get going.......more to come!
|
This is pretty much all you need to get going.......more to come!
|
||||||
@ -140,14 +140,14 @@ In order to set up a ReRoute you need to add one to the json array called ReRout
|
|||||||
the following.
|
the following.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"DownstreamPathTemplate": "/api/posts/{postId}",
|
"DownstreamPathTemplate": "/api/posts/{postId}",
|
||||||
"DownstreamScheme": "https",
|
"DownstreamScheme": "https",
|
||||||
"DownstreamPort": 80,
|
"DownstreamPort": 80,
|
||||||
"DownstreamHost" "localhost"
|
"DownstreamHost" "localhost"
|
||||||
"UpstreamPathTemplate": "/posts/{postId}",
|
"UpstreamPathTemplate": "/posts/{postId}",
|
||||||
"UpstreamHttpMethod": "Put"
|
"UpstreamHttpMethod": "Put"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The DownstreamPathTemplate,Scheme, Port and Host make the URL that this request will be forwarded to.
|
The DownstreamPathTemplate,Scheme, Port and Host make the URL that this request will be forwarded to.
|
||||||
@ -163,7 +163,7 @@ At the moment without any configuration Ocelot will default to all ReRoutes bein
|
|||||||
In order to change this you can specify on a per ReRoute basis the following setting.
|
In order to change this you can specify on a per ReRoute basis the following setting.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"ReRouteIsCaseSensitive": true
|
"ReRouteIsCaseSensitive": true
|
||||||
```
|
```
|
||||||
|
|
||||||
This means that when Ocelot tries to match the incoming upstream url with an upstream template the
|
This means that when Ocelot tries to match the incoming upstream url with an upstream template the
|
||||||
@ -185,11 +185,11 @@ GlobalConfiguration. The Provider is required and if you do not specify a host a
|
|||||||
will be used.
|
will be used.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"ServiceDiscoveryProvider": {
|
"ServiceDiscoveryProvider": {
|
||||||
"Provider":"Consul",
|
"Provider":"Consul",
|
||||||
"Host":"localhost",
|
"Host":"localhost",
|
||||||
"Port":8500
|
"Port":8500
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In order to tell Ocelot a ReRoute is to use the service discovery provider for its host and port you must add the
|
In order to tell Ocelot a ReRoute is to use the service discovery provider for its host and port you must add the
|
||||||
@ -197,14 +197,14 @@ ServiceName and load balancer you wish to use when making requests downstream. A
|
|||||||
and LeastConnection algorithm you can use. If no load balancer is specified Ocelot will not load balance requests.
|
and LeastConnection algorithm you can use. If no load balancer is specified Ocelot will not load balance requests.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"DownstreamPathTemplate": "/api/posts/{postId}",
|
"DownstreamPathTemplate": "/api/posts/{postId}",
|
||||||
"DownstreamScheme": "https",
|
"DownstreamScheme": "https",
|
||||||
"UpstreamPathTemplate": "/posts/{postId}",
|
"UpstreamPathTemplate": "/posts/{postId}",
|
||||||
"UpstreamHttpMethod": "Put",
|
"UpstreamHttpMethod": "Put",
|
||||||
"ServiceName": "product"
|
"ServiceName": "product",
|
||||||
"LoadBalancer": "LeastConnection"
|
"LoadBalancer": "LeastConnection"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
When this is set up Ocelot will lookup the downstream host and port from the service discover provider and load balancer
|
When this is set up Ocelot will lookup the downstream host and port from the service discover provider and load balancer
|
||||||
@ -218,16 +218,16 @@ come if required). In order to identity a ReRoute as authenticated it needs the
|
|||||||
configuration added.
|
configuration added.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"AuthenticationOptions": {
|
"AuthenticationOptions": {
|
||||||
"Provider": "IdentityServer",
|
"Provider": "IdentityServer",
|
||||||
"ProviderRootUrl": "http://localhost:52888",
|
"ProviderRootUrl": "http://localhost:52888",
|
||||||
"ScopeName": "api",
|
"ScopeName": "api",
|
||||||
"AdditionalScopes": [
|
"AdditionalScopes": [
|
||||||
"openid",
|
"openid",
|
||||||
"offline_access"
|
"offline_access"
|
||||||
],
|
],
|
||||||
"ScopeSecret": "secret"
|
"ScopeSecret": "secret"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In this example the Provider is specified as IdentityServer. This string is important
|
In this example the Provider is specified as IdentityServer. This string is important
|
||||||
@ -248,9 +248,9 @@ Ocelot supports claims based authorisation which is run post authentication. Thi
|
|||||||
you have a route you want to authorise you can add the following to you ReRoute configuration.
|
you have a route you want to authorise you can add the following to you ReRoute configuration.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"RouteClaimsRequirement": {
|
"RouteClaimsRequirement": {
|
||||||
"UserType": "registered"
|
"UserType": "registered"
|
||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
||||||
In this example when the authorisation middleware is called Ocelot will check to see
|
In this example when the authorisation middleware is called Ocelot will check to see
|
||||||
@ -291,10 +291,10 @@ and add whatever was at the index requested to the transform.
|
|||||||
Below is an example configuration that will transforms claims to claims
|
Below is an example configuration that will transforms claims to claims
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"AddClaimsToRequest": {
|
"AddClaimsToRequest": {
|
||||||
"UserType": "Claims[sub] > value[0] > |",
|
"UserType": "Claims[sub] > value[0] > |",
|
||||||
"UserId": "Claims[sub] > value[1] > |"
|
"UserId": "Claims[sub] > value[1] > |"
|
||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
||||||
This shows a transforms where Ocelot looks at the users sub claim and transforms it into
|
This shows a transforms where Ocelot looks at the users sub claim and transforms it into
|
||||||
@ -305,9 +305,9 @@ UserType and UserId claims. Assuming the sub looks like this "usertypevalue|user
|
|||||||
Below is an example configuration that will transforms claims to headers
|
Below is an example configuration that will transforms claims to headers
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"AddHeadersToRequest": {
|
"AddHeadersToRequest": {
|
||||||
"CustomerId": "Claims[sub] > value[1] > |"
|
"CustomerId": "Claims[sub] > value[1] > |"
|
||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
||||||
This shows a transform where Ocelot looks at the users sub claim and trasnforms it into a
|
This shows a transform where Ocelot looks at the users sub claim and trasnforms it into a
|
||||||
@ -318,9 +318,9 @@ CustomerId header. Assuming the sub looks like this "usertypevalue|useridvalue".
|
|||||||
Below is an example configuration that will transforms claims to query string parameters
|
Below is an example configuration that will transforms claims to query string parameters
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"AddQueriesToRequest": {
|
"AddQueriesToRequest": {
|
||||||
"LocationId": "Claims[LocationId] > value",
|
"LocationId": "Claims[LocationId] > value",
|
||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
||||||
This shows a transform where Ocelot looks at the users LocationId claim and add its as
|
This shows a transform where Ocelot looks at the users LocationId claim and add its as
|
||||||
@ -335,11 +335,11 @@ want to use a circuit breaker when making requests to a downstream service. This
|
|||||||
Add the following section to a ReRoute configuration.
|
Add the following section to a ReRoute configuration.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"QoSOptions": {
|
"QoSOptions": {
|
||||||
"ExceptionsAllowedBeforeBreaking":3,
|
"ExceptionsAllowedBeforeBreaking":3,
|
||||||
"DurationOfBreak":5,
|
"DurationOfBreak":5,
|
||||||
"TimeoutValue":5000
|
"TimeoutValue":5000
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You must set a number greater than 0 against ExceptionsAllowedBeforeBreaking for this rule to be
|
You must set a number greater than 0 against ExceptionsAllowedBeforeBreaking for this rule to be
|
||||||
@ -362,7 +362,7 @@ have an OcelotRequestId.
|
|||||||
In order to use the requestid feature in your ReRoute configuration add this setting
|
In order to use the requestid feature in your ReRoute configuration add this setting
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"RequestIdKey": "OcRequestId"
|
"RequestIdKey": "OcRequestId"
|
||||||
```
|
```
|
||||||
|
|
||||||
In this example OcRequestId is the request header that contains the clients request id.
|
In this example OcRequestId is the request header that contains the clients request id.
|
||||||
@ -371,7 +371,7 @@ There is also a setting in the GlobalConfiguration section which will override w
|
|||||||
set at ReRoute level for the request id. The setting is as fllows.
|
set at ReRoute level for the request id. The setting is as fllows.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"RequestIdKey": "OcRequestId",
|
"RequestIdKey": "OcRequestId",
|
||||||
```
|
```
|
||||||
|
|
||||||
It behaves in exactly the same way as the ReRoute level RequestIdKey settings.
|
It behaves in exactly the same way as the ReRoute level RequestIdKey settings.
|
||||||
@ -392,7 +392,7 @@ and setting a TTL in seconds to expire the cache. More to come!
|
|||||||
In orde to use caching on a route in your ReRoute configuration add this setting.
|
In orde to use caching on a route in your ReRoute configuration add this setting.
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||||
```
|
```
|
||||||
|
|
||||||
In this example ttl seconds is set to 15 which means the cache will expire after 15 seconds.
|
In this example ttl seconds is set to 15 which means the cache will expire after 15 seconds.
|
||||||
@ -406,15 +406,15 @@ When setting up Ocelot in your Startup.cs you can provide some additonal middlew
|
|||||||
and override middleware. This is done as follos.
|
and override middleware. This is done as follos.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var configuration = new OcelotMiddlewareConfiguration
|
var configuration = new OcelotMiddlewareConfiguration
|
||||||
|
{
|
||||||
|
PreErrorResponderMiddleware = async (ctx, next) =>
|
||||||
{
|
{
|
||||||
PreErrorResponderMiddleware = async (ctx, next) =>
|
await next.Invoke();
|
||||||
{
|
}
|
||||||
await next.Invoke();
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
app.UseOcelot(configuration);
|
app.UseOcelot(configuration);
|
||||||
```
|
```
|
||||||
|
|
||||||
In the example above the provided function will run before the first piece of Ocelot middleware.
|
In the example above the provided function will run before the first piece of Ocelot middleware.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user