dotnetcore之Ocelot
一、基本用法
1、Ocelot.json
{ "ReRoutes": [ //CatService { "DownstreamPathTemplate": "/api/findcat", //下游服务的访问路径 "DownstreamScheme": "http", //下游服务是http还是https "DownstreamHostAndPorts": [ //下游服务的地址以及端口号 { "Host": "localhost", //下游服务的地址 "Port": "6001" //下游服务的端口号 } ], "UpstreamPathTemplate": "/service/cat", //上游服务的访问路径 "UpstreamHttpMethod": [ "GET", "POST" ], //上游服务的访问模式,本例里只用到了POST "AuthenticationOptions": { "AuthenticationProviderKey": "TestKey", "AllowedScopes": [] } }, //Report { "DownstreamPathTemplate": "/{url}", //下游服务的访问路径 "DownstreamScheme": "http", //下游服务是http还是https "DownstreamHostAndPorts": [ //下游服务的地址以及端口号 { "Host": "localhost", //下游服务的地址 "Port": "5000" //下游服务的端口号 } ], "UpstreamPathTemplate": "/service/report/{url}", //上游服务的访问路径 "UpstreamHttpMethod": [ "GET", "POST" ] //上游服务的访问模式,本例里只用到了POST } ] }
2、startup注入
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Ocelot.DependencyInjection; using Ocelot.Middleware; namespace ApiGateway { 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) { string authProviderKey = "TestKey"; services.AddAuthentication() .AddJwtBearer(authProviderKey,x=> { }); services.AddOcelot(Configuration); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } bool ValidatePermission(HttpContext httpContext) { return false; } // 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(); } else { app.UseHsts(); } //app.UseHttpsRedirection(); app.UseMvc(); app.UseOcelot().Wait(); } } }
二、占位符,比方上游、下游模板都有,可以使用{something}的方式在模板中添加变量占位符。占位符需要在DownstreamPathTemplate 和UpstreamPathTemplate中都添加。如果是这样,当请求到达时Ocelot将试图使用上游url中的正确的变量值来替换占位符。
1、http://localhost:50002/report/api/values可以访问下游http://localhost:5000/api/values
{ "ReRoutes": [ { //"DownstreamPathTemplate": "/api/values", // 下游游请求模板 "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", //下游服务 schema //"UpstreamPathTemplate": "/api/values", // 上游请求模板 "UpstreamPathTemplate": "/report/{url}", // 上游请求模板 "UpstreamHttpMethod": [ "Get", "Post" ], // 上游请求http方法 //下游服务的地址,如果使用LoadBalancer的话这里可以填多项 "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], //LeastConnection – 将请求发往最空闲的那个服务器 //"RoundRobin""轮流发送" //"NoLoadBalance" "总是发往第一个请求或者是服务发现", "LoadBalancer": "LeastConnection", //熔断配置 "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 3, //允许的异常请求数 "DurationOfBreak": 10, //熔断的时间,单位为秒 "TimeoutValue": 5000 //如果下游请求的处理时间超过多少则将请求设置为超时 }, //缓存配置 "FileCacheOptions": { "TtlSeconds": 10, "Region": "somename" //是对缓存进行的一个分区 }, "HttpHandlerOptions": { "AllowAutoRedirect": false, "UseCookieContainer": false }, //配置服务认证 "AuthenticationOptions": { "AuthenticationProviderKey": "", "AllowedScopes": [] } }, { "DownstreamPathTemplate": "/api/product", "DownstreamScheme": "http", "UpstreamPathTemplate": "/api/product", "UpstreamHttpMethod": [ "Get" ], "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 50001 } ], "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 3, "DurationOfBreak": 10, "TimeoutValue": 5000 }, "AuthenticationOptions": { } } ], "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "AdministrationPath": "/admin" } }

浙公网安备 33010602011771号