搭配 consul 服务使用 Ocelot,自动路由配置
- 新建webapi 项目,安装 nuget 包
install-package Ocelot
install-package Ocelot.Provider.Consul
install-package Ocelot.Provider.Polly
- Startup.cs 修改
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
// add
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Ocelot.Provider.Polly;
namespace JSClound.Gateway
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.AddOcelot();
// add
services.AddOcelot(Configuration).AddConsul().AddPolly();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
// add
app.UseOcelot().Wait();
}
}
}
- Program.cs 修改
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace JSClound.Gateway
{
public class Program
{
public static void Main(string[] args)
{
// update
BuildWebHost(args).Run();
}
//update
//public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
// WebHost.CreateDefaultBuilder(args)
// .UseStartup<Startup>();
//add
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, builder) =>
{
builder
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("Ocelot.json");
})
.UseStartup<Startup>()
.Build();
}
}
- 添加 Ocelot.json 文件
{
"ReRoutes": [],
"Aggregates": [],
"GlobalConfiguration": {
"RequestIdKey": null,
"ServiceDiscoveryProvider": {
"Host": "192.168.1.3", // Consul Service IP
"Port": 8500 // Consul Service Port
},
"RateLimitOptions": {
"DisableRateLimitHeaders": false, // Http头 X-Rate-Limit 和 Retry-After 是否禁用
"QuotaExceededMessage": "Too many requests, are you OK?", // 当请求过载被截断时返回的消息
"HttpStatusCode": 600, // 当请求过载被截断时返回的http status
"ClientIdHeader": "client_id" // 用来识别客户端的请求头,默认是 ClientId
},
//"RateLimitOptions": {
// "ClientWhitelist": [ "admin" ], // 白名单
// "EnableRateLimiting": true, // 是否启用限流
// "Period": "1m", // 统计时间段:1s, 5m, 1h, 1d
// "PeriodTimespan": 15, // 多少秒之后客户端可以重试
// "Limit": 5 // 在统计时间段内允许的最大请求数量
//},
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3, // 允许多少个异常请求
"DurationOfBreak": 10000, // 熔断的时间,单位为毫秒
"TimeoutValue": 5000 // 如果下游请求的处理时间超过多少则视如该请求超时
},
"BaseUrl": null,
"LoadBalancerOptions": {
"Type": "LeastConnection",
"Key": null,
"Expiry": 0
},
"DownstreamScheme": "http",
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false
}
}
}
- 效果
本机访问(注意添加 hosts 192.168.1.3 xfubuntu-desktop)
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019061712234564.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppZWdlbWVuYQ==,size_16,color_FFFFFF,t_70)
网关访问
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190617122413375.png)
- 问题
- 发现 Ocelot 请求consul 获取到的 服务的 IP地址为所在的 主机名称,添加hosts 初步解决
- 未解决 直接IP地址返回,不需要设置 hosts
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)