dotnet consul 与 ocelot
创建
重新创建一个项目
dotnet new webapi --name gateway
安装ocelot
dotnet add package Ocelot --version 15.0.6
在startup.cs的Configure类中注释其它代码,然后添加
app.UseOcelot();
在startup.cs的ConfigureServices类中注释其它代码,然后添加
services.AddOcelot();
在Program.cs的CreateHostBuilder类中修改代码如下:
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(conf =>
{
conf.AddJsonFile("configuration.json", optional: false, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
然后在根目录下添加configuration.json文件
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "39.108.54.23",
"Port": 8081
}
,
{
"Host": "39.108.54.23",
"Port": 8082
},
{
"Host": "39.108.54.23",
"Port": 8083
}
],
"UpstreamPathTemplate": "/up/{url}", //如url冲突可加Priority决定
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
"Type":"RoundRobin" //轮询 //"LeastConnection" //最小连接数 "NoLoadBalance" //不负载均衡 //"CookieStickySessions" //会话粘滞
}
//"LoadBalancerOptions": {
// "Type": "CookieStickySessions",
// "Key": "ASP.NET_SessionId",
// "Expiry": 1800000
//}
}
]
}
运行
运行项目,使用上游设置的url访问 https://localhost:5001/up/value/get
,刷新可以观察到端口有轮询切换
上游的url:/up/value/get
对应下游url:/api/value/get
如把8081端口进程杀掉,刷新页面,当轮询到8081端口时会访问不了,再刷新又访问到(已切换到8082端口)
鉴于以上这种情况,可结合consul使用
安装 Ocelot.Provider.Consul
dotnet add package Ocelot.Provider.Consul --version 15.0.6
在startip.cs文件中修改
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot().AddConsul();
}
configuration.json文件修改如下:
"ReRoutes":[
{
"UseServiceDiscovery":true,
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"ServiceName":"xing",
"LoadBalancerOptions": {
"Type":"RoundRobin"
},
"UpstreamPathTemplate": "/up/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"ReRoutesCaseSensitive":false
}
],
"GlobalConfiguration":{
"ServiceDiscoveryProvider":{
"Host":"localhost",
"Port":8500,
"ConfigurationKey":"ocelot_key",
"Type": "Consul" // 提供服务发现,每次都请求
}
//"ServiceDiscoveryProvider": {
// "Host": "localhost",
// "Port": 8500,
// "Type": "PollConsul",
// "PollingInterval": 1000 //间隔1秒一次啊
// //"Token": "footoken"//ACL
// }
}
然后运行下面命令启动,这可解决上面出现的问题(在本地运行没问题,在linux上运行报错:下图)
dotnet gateway.dll --urls="http://*:9000" --ip="127.0.0.1" --port=9000
缓存
安装
dotnet add package Ocelot.Cache.CacheManager --version 14.1.0
在startup.cs文件configureServices添加
.AddCacheManager(x => {x.WithDictionaryHandle();}) // 默认字典存储
还需引用using Ocelot.Cache.CacheManager;
修改configuration.json文件
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/up/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"UseServiceDiscovery": true,
"ServiceName": "xing",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
"FileCacheOptions": {
"TtlSeconds": 15,
"Region": "UserCache"
}
}
],
"GlobalConfiguration": {
"BaseUrl": "http://127.0.0.1:9000",
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul"
}
//"ServiceDiscoveryProvider": {
// "Host": "localhost",
// "Port": 8500,
// "Type": "PollConsul",
// "PollingInterval": 1000
// //"Token": "footoken"
//}
}
}
超时、熔断、限流、降级、聚合请求
安装
dotnet add package Ocelot.Provider.Polly --version 15.0.6
熔断、限流
配置如下
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/up/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"UseServiceDiscovery": true,
"ServiceName": "xing",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
// 限流
// "RateLimitOptions": {
// "ClientWhitelist": [ "one", "two" ], // 白名单
// "EnableRateLimiting": true,
// "Period": "5m", //1s, 5m, 1h, 1d
// "PeriodTimespan": 30, // 几秒后可重试
// "Limit": 5 // 最大请求数
// }
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3, //允许几个异常
"DurationOfBreak": 10000, // 熔断时间,ms
"TimeoutValue": 4000 //请求超时时间
}
}
],
"GlobalConfiguration": {
"BaseUrl": "http://127.0.0.1:9000",
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul"
},
"RateLimitOptions": {
"QuotaExceededMessage": "Too many requests, maybe later?",
"HttpStatusCode": 440 //返回状态码
}
}
}
请求聚合
配置如下
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/value/get",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8081
}
],
"UpstreamPathTemplate": "/one/value/get",
"UpstreamHttpMethod": [ "Get", "Post" ],
"key": "one"
},
{
"DownstreamPathTemplate": "/api/value/get",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8082
}
],
"UpstreamPathTemplate": "/two/value/get",
"UpstreamHttpMethod": [ "Get", "Post" ],
"key": "two"
},
{
"DownstreamPathTemplate": "/api/value/get",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8083
}
],
"UpstreamPathTemplate": "/three/value/get",
"UpstreamHttpMethod": [ "Get", "Post" ],
"key": "three"
}
],
"Aggregates": [
{
"ReRouteKeys": [
"one",
"two",
"three"
],
"UpstreamPathTemplate": "/UserAggregator",
//"Aggregator": "UserAggregator"
}
]
}