Ocelot +.net6
官方文檔
雖然現在我們不用這個網關了,還是要記錄一下筆記.我是用的是.net6配置的.
1.簡單的手動在配置文件中配置服務
- 安裝Nuget
Ocelot - 新增一個名為
ocelot.json的配置文件
{
"Routes": [
{
"DownstreamPathTemplate": "/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
//可以多個負載均衡
{
"Host": "localhost",
"Port": 5000 //服務端口
},
{
"Host": "localhost",
"Port": 5001 //服務端口
}
],
"UpstreamPathTemplate": "/{url}",
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ],
"LoadBalancerOptions": {
"Type": "RoundRobin" //LeastConnection – 將請求發往最空閑的那個服務器;RoundRobin – 輪流發送;NoLoadBalance – 總是發往第一個請求或者是服務發現
}
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:8000"
}
}
- Program注冊使用代碼如下
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
//將ocelot配置問題ocelot.json添加到配置提供程序
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
//追加ocelot配置文件
config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
});
//注冊Ocelot
//builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddOcelot();
var app = builder.Build();
app.UseOcelot().Wait();
app.Run();
2.使用Consul作為服務發現
Install-Package Ocelot.Provider.Consul- 配置
ocelot.json配置文件
{
"Routes": [
{
"DownstreamPathTemplate": "/{url}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/{url}",
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ],
"UseServiceDiscovery": true,
"ServiceName": "service-a",
"LoadBalancerOptions": {
"Type": "RoundRobin" //LeastConnection – 將請求發往最空閑的那個服務器;RoundRobin – 輪流發送;NoLoadBalance – 總是發往第一個請求或者是服務發現
}
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:8000",
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500
}
}
}
- Program注冊使用Ocelot和Consul,代碼如下
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
//將ocelot配置問題ocelot.json添加到配置提供程序
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
//追加ocelot配置文件
config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
});
//注冊Ocelot
builder.Services.AddOcelot()
.AddConsul()
.AddConfigStoredInConsul();
var app = builder.Build();
app.UseOcelot().Wait();
app.Run();
3.使用Cache
- 安裝Nuget
Install-Package Ocelot.Cache.CacheManager - 注入CacheManager
s.AddOcelot()
.AddCacheManager(x =>
{
x.WithDictionaryHandle();
})
- 在具體的Route里面添加如下配置
"FileCacheOptions": { "TtlSeconds": 15, "Region": "somename" }
4.限流
直接配置在具體的Route里面就行了
"RateLimitOptions": {
"ClientWhitelist": [],//白名單
"EnableRateLimiting": true,//是否啟用限流
"Period": "1s",//1s,5m,1h,1d
"PeriodTimespan": 1,//多少秒之后客戶端可以重試
"Limit": 10 //統計時間段之內允許的最大請求數量
}
還可以設置全局的限流提示
"GlobalConfiguration": {
"BaseUrl": "https://localhost:8000",
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500
},
//全局限流提示配置
"RateLimitOptions": {
"DisableRateLimitHeaders": false,
"QuotaExceededMessage": "Customize Tips!",
"HttpStatusCode": 999,
"ClientIdHeader": "Test"
}
}
超時/熔斷 Polly
Install-Package Ocelot.Provider.Polly
注冊Polly
//將ocelot配置問題ocelot.json添加到配置提供程序
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
//追加ocelot配置文件
config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
});
//注冊Ocelot
//builder.Services.AddOcelot(builder.Configuration);
//builder.Services.AddOcelot()
builder.Services.AddOcelot()
.AddCacheManager(x => x.WithDictionaryHandle())
.AddConsul()
.AddConfigStoredInConsul()
.AddPolly();
配置文件使用Polly
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3,//代表發生錯誤的次數
"DurationOfBreak": 10000,//代表熔斷時間
"TimeoutValue": 5000//代表超時時間
}
