一、介紹
Ocelot 是基于.NetCore實(shí)現(xiàn)的開源的API網(wǎng)關(guān)棋恼,支持IdentityServer
認(rèn)證蘸泻。Ocelot具有路由嘲玫、請(qǐng)求聚合去团、服務(wù)發(fā)現(xiàn)土陪、認(rèn)證鬼雀、鑒權(quán)、限流熔斷等功能鞋吉,并內(nèi)置了負(fù)載均衡器與Service Fabric谓着、Butterfly Tracing集成赊锚。
Ocelot本質(zhì)是由一系列特定順序的.Net Core Middleware
組成的一個(gè)管道舷蒲。Ocelot接收到請(qǐng)求之后友多,用request bulider
來創(chuàng)建一個(gè)HttpRequestMessage
對(duì)象,該對(duì)象根據(jù)配置將請(qǐng)求下發(fā)給下游指定的服務(wù)器進(jìn)行請(qǐng)求處理欠拾。下游服務(wù)返回response
之后藐窄,一個(gè)middleware
將它返回的HttpResponseMessage
映射到HttpResponse
,再轉(zhuǎn)發(fā)給客戶端荆忍。
二刹枉、如何搭建一個(gè)Ocelot項(xiàng)目
新建一個(gè).net core 項(xiàng)目微宝。我的環(huán)境是:vs2017,.net core 2.1,Ocelot 8.0蟋软。
NuGet安裝Ocelot
image.png
添加完項(xiàng)目之后添加
ocelot.json
配置文件
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/values", // 下游游請(qǐng)求模板
"DownstreamScheme": "http", //下游服務(wù) schema
"UpstreamPathTemplate": "/api/values", // 上游請(qǐng)求模板
"UpstreamHttpMethod": [ "Get" ], // 上游請(qǐng)求http方法
//下游服務(wù)的地址岳守,如果使用LoadBalancer的話這里可以填多項(xiàng)
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8802
}
],
//LeastConnection – 將請(qǐng)求發(fā)往最空閑的那個(gè)服務(wù)器
//"RoundRobin""輪流發(fā)送"
//"NoLoadBalance" "總是發(fā)往第一個(gè)請(qǐng)求或者是服務(wù)發(fā)現(xiàn)",
"LoadBalancer": "LeastConnection",
//熔斷配置
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3, //允許的異常請(qǐng)求數(shù)
"DurationOfBreak": 10, //熔斷的時(shí)間,單位為秒
"TimeoutValue": 5000 //如果下游請(qǐng)求的處理時(shí)間超過多少則將請(qǐng)求設(shè)置為超時(shí)
},
//緩存配置
"FileCacheOptions": {
"TtlSeconds": 10,
"Region": "somename" //是對(duì)緩存進(jìn)行的一個(gè)分區(qū)
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false
},
//配置服務(wù)認(rèn)證
"AuthenticationOptions": {
"AuthenticationProviderKey": "",
"AllowedScopes": []
}
},
{
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/product",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8801
}
],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3,
"DurationOfBreak": 10,
"TimeoutValue": 5000
},
"AuthenticationOptions": {
}
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/admin"
}
}
將配置文件加入Configuration
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostcontext, bulid) => {
bulid.SetBasePath(hostcontext.HostingEnvironment.ContentRootPath)
.AddJsonFile("ocelot.json");
})
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>();
}
添加依賴注入
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot();
}
添加Ocelot中間件
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseOcelot().Wait();
}
添加兩個(gè)下游api服務(wù),對(duì)應(yīng)ocelot.json
配置的下游服務(wù)
設(shè)置
APIProductService
端口為8801
,APIUserServiec
端口為8802
岂昭,APIGateway
端口為5000
邑遏,設(shè)置方式如圖所示:
image.png
啟動(dòng)項(xiàng)目設(shè)置為啟動(dòng)多個(gè)項(xiàng)目
image.png
啟動(dòng)項(xiàng)目
請(qǐng)求結(jié)果如下
image.png
image.png
image.png
image.png
將http://localhost:5000/api/values
請(qǐng)求轉(zhuǎn)發(fā)給了下游服務(wù)APIUserServiec
處理
將請(qǐng)求 http://localhost:5000/api/product
轉(zhuǎn)發(fā)給了APIProductService
服務(wù)進(jìn)行處理
三俩檬、參考
[1] .NET Core開源API網(wǎng)關(guān) – Ocelot中文文檔
[2] Ocelot GitHub