首先先nuget:ocelot包
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot().AddConsul().AddPolly();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseOcelot();
}
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c =>
{
c.AddJsonFile("configuration.json", optional: false, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
configuration.json
////*****************************单地址********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5726 //服务端口
// } //可以多个,自行负载均衡
// ],
// "UpstreamPathTemplate": "/T5726/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
// "UpstreamHttpMethod": [ "Get", "Post" ]
// }
// ]
//}
//*****************************多地址多实例********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5726 //服务端口
// } //可以多个,自行负载均衡
// ],
// "UpstreamPathTemplate": "/T5726/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
// "UpstreamHttpMethod": [ "Get", "Post" ]
// },
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5727 //服务端口
// }
// ],
// "UpstreamPathTemplate": "/T5727/{url}", //网关地址--url变量
// "UpstreamHttpMethod": [ "Get", "Post" ]
// },
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5728 //服务端口
// }
// ],
// "UpstreamPathTemplate": "/T5728/{url}", //网关地址--url变量
// "UpstreamHttpMethod": [ "Get", "Post" ]
// }
// ]
//}
////*****************************单地址多实例负载均衡********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5726 //服务端口
// } //可以多个,自行负载均衡
// ,
// {
// "Host": "localhost",
// "Port": 5727 //服务端口
// },
// {
// "Host": "localhost",
// "Port": 5728 //服务端口
// }
// ],
// "UpstreamPathTemplate": "/T5/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
// "UpstreamHttpMethod": [ "Get", "Post" ],
// "LoadBalancerOptions": {
// "Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡
// }
// }
// ]
//}
////*****************************单地址多实例负载均衡+Consul********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "UpstreamPathTemplate": "/TConsul/{url}", //网关地址--url变量
// "UpstreamHttpMethod": [ "Get", "Post" ],
// "ServiceName": "ZhaoxiUserService", //consul服务名称
// "LoadBalancerOptions": {
// "Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡
// },
// "UseServiceDiscovery": true
// }
// ],
// "GlobalConfiguration": {
// "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
// "ServiceDiscoveryProvider": {
// "Host": "localhost",
// "Port": 8500,
// "Type": "Consul" //由Consul提供服务发现
// }
// }
//}
//*****************************单地址多实例负载均衡+Consul+Polly********************************
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/consul/{url}", //网关地址--url变量
"UpstreamHttpMethod": [ "Get", "Post" ],
"ServiceName": "ZhaoxiUserService", //consul服务名称
"LoadBalancerOptions": {
"Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡
},
"UseServiceDiscovery": true,
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3, //允许多少个异常请求
"DurationOfBreak": 10000, // 熔断的时间,单位为ms
"TimeoutValue": 10000 //如果下游请求的处理时间超过多少则自如将请求设置为超时 默认90秒
}
//"RateLimitOptions": {
// "ClientWhitelist": [], //白名单
// "EnableRateLimiting": true,
// "Period": "5m", //1s, 5m, 1h, 1d jeffzhang
// "PeriodTimespan": 5, //多少秒之后客户端可以重试
// "Limit": 5 //统计时间段内允许的最大请求数量
//},
//"FileCacheOptions": {
// "TtlSeconds": 10
//} //"缓存"
}
],
"GlobalConfiguration": {
"BaseUrl": "http://127.0.0.1:6299", //网关对外地址
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul" //由Consul提供服务发现
},
//"RateLimitOptions": {
// "QuotaExceededMessage": "Too many requests, maybe later? 11", // 当请求过载被截断时返回的消息
// "HttpStatusCode": 666 // 当请求过载被截断时返回的http status
//}
}
}