微服务-3.网关GateWay
1.使用Ocelot配置网关
官网:https://ocelot.readthedocs.io
2.新建web api项目
3.引用ocelot包
4.打开项目的Startup.cs文件,进行修改
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot();
// services.AddControllers();
}
// 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(); // 去掉默认管道,使用Ocelot管道处理请求
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
//app.UseHttpsRedirection();
//app.UseRouting();
//app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllers();
//});
}
5.添加配置文件xxx.json(这里我用gateway.json)
6.到Program,cs文件中修改项目所使用的配置文件路径
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(conf =>
{
conf.AddJsonFile("gateway.json", optional: false, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls("http://localhost:9090");
//可自定义部署之后ip地址
});
}
7.配置文件路由 Gateway+Consul
1)引用ocelot.Provider.Consul 包
2)修改Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot().AddConsul();
// services.AddControllers();
}
3)gateway.json配置文件
{
// 配置之后执行本项目,通过访问"网关地址",访问"接口服务地址"(可自定义)
"Routes": [
{
// 下面为负载均衡的例子
"DownstreamPathTemplate": "/api/{url}", // 接口服务地址 -url 变量
"DownstreamScheme": "http", // http、https
"UpstreamPathTemplate": "/fuzaijunheng/1/{url}", // 网关地址 -url变量
"UpstreamHttpMethod": [ "Get", "Post" ],
"UseServiceDiscovery": true, // 启用服务发现
"ServiceName": "BesosService", // consul的组名称
"LoadBalancerOptions": {
"Type": "RoundRobin" //遍历可用服务并发送请求轮询
// LeastConnection-将请求发送给最少连接数的服务器
// CookieStickySessions-使用cookie将所有请求粘贴到特定服务器
// NoLoadBalance 从配置或服务发现中获取第一个可用服务。
}
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul", // 由Consul提供服务发现
}
}
8.配置文件路由 Gateway + Consul + Polly(实现缓存、熔断、限流、降级)
1)引用ocelot.provider.Consul、ocllot.provider.Polly、ocelot.Cache.CacheManager(用于缓存)
2)修改Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot()
.AddConsul()
.AddPolly()
.AddCacheManager(x=> {
x.WithDictionaryHandle(); // 默认字典存储
});
// services.AddControllers();
}
3)gateway.json配置文件
{
//***************************多地址配置路由GateWay + Consul + Polly ******************************
// 配置之后执行本项目,通过访问网关地址,访问接口服务地址(可自定义)
"Routes": [
{
// 下面为负载均衡的例子
"DownstreamPathTemplate": "/api/{url}", // 接口服务地址 -url 变量
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/T/{url}", // 网关地址 -url变量
"UpstreamHttpMethod": [ "Get", "Post" ],
"UseServiceDiscovery": true, // 启用服务发现
"ServiceName": "BesosService", // consul的组名称
"LoadBalancerOptions": {
"Type": "RoundRobin" //遍历可用服务并发送请求轮询
// LeastConnection-将请求发送给最少连接数的服务器
// CookieStickySessions-使用cookie将所有请求粘贴到特定服务器
// NoLoadBalance 从配置或服务发现中获取第一个可用服务。
},
// 缓存 引用 ocelot.Cache.CacheManager
"FileCacheOptions": {
"TtlSeconds": 10, // 缓存保留时间
"Region": "UserCache" //可以调用Api清理
},
// 限流(上游请求限制,一般用在资源超出时使用)
//"RateLimitOptions": {
// "ClientWhitelist": [], // 白名单
// "EnableRateLimiting": true, // 是否启用端点速率限制。
// "Period": "5m", // 指定限制所适用的期间,例如1s,5m,1h,1d等
// "PeriodTimespan": 5, // 指定我们可以在一定秒数后重试。
// "Limit": 5 // 此值指定客户端在定义的时间内可以发出的最大请求数。
}
//熔断(当多少次异常请求时熔断一定时间,即在一定时间内自动请求返回失败)
//"QoSOptions": {
// "ExceptionsAllowedBeforeBreaking": 0, // 允许多少个异常请求
// "DurationOfBreak": 1000, // 熔断时间,请求自动返回失败的时间 单位:ms
// "TimeoutValue": 5000 // 如果请求花费5秒钟以上,它将自动超时。默认90秒
//}
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul", // 由Consul提供服务发现
},
"RateLimitOptions": {
"QuotaExceededMessage": "Too many requests May be later", // 自定义错误信息
"HttpStatusCode": 666 // 自定义请求失败状态码
}
}
}

浙公网安备 33010602011771号