相關文章
Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服務註冊,服務發現
Consul+Ocelot+Polly在.NetCore中使用(.NET5)-閘道器Ocelot+Consul
一、簡介
前一篇Consul中有個問題是,所有客戶端都要和Consul進行連線,且直接拿到了所有的服務例項,這就直接把全部的服務例項暴露出來了,所以需要用閘道器來隔離客戶端和服務例項,
所有api請求都從閘道器進入。
Ocelot作為一個閘道器應用,主要的功能有路由、請求聚合、服務發現、統一認證、統一鑑權、限流熔斷、並內建了負載均衡器等的整合。而且這些功能都只需要簡單的配置即可完成。
二、使用Ocelot
2.1應用配置
新建一個.NetCore專案作閘道器應用。
安裝NuGet包
Ocelot
Startup.cs中把ConfigureServices(),Configure()裡面的程式碼都去掉,加上Ocelot接管程式碼。
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot();
} // 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();
}
這些操作完,程式就再也不是asp.net core,也不是什麼webApi的程式了,就是一個Ocelot閘道器應用。
2.2路由配置
閘道器最重要的功能就是路由,根據路由把功能轉發到其它應用去,它本身的應用有ip地址,別人可能訪問它,但它怎麼知道哪個請求轉到哪個應用去呢,這些全靠配置。
首先在 Program.cs裡的CreateHostBuilder()加入配置檔案資訊
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c =>
{
c.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
根目錄下增加配置檔案ocelot.json。
{
"Routes": [
{
//轉發到下游服務地址--url變數
"DownstreamPathTemplate": "/api/{url}",
//下游http協議
"DownstreamScheme": "http",
//負載方式,
"LoadBalancerOptions": {
"Type": "RoundRobin" // 輪詢
},
"DownstreamHostAndPorts": [
{
"Host": "172.16.2.9",
"Port": 5201 //服務埠
}, //可以多個,自行負載均衡
{
"Host": "172.16.2.9",
"Port": 5202 //服務埠
},
{
"Host": "172.16.2.9",
"Port": 5203 //服務埠
}
],
//上游地址
"UpstreamPathTemplate": "/T1/{url}", //閘道器地址--url變數 //衝突的還可以加權重Priority
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ]
}
]
}
這裡的是路由配置,註釋說明已經很清楚,假如閘道器程式啟動後的地址為http://172.16.2.9:5200
當訪問:http://172.16.2.9:5200/T1/Test/GetName 閘道器會把請求轉發到http://172.16.2.9:5201/T1/Test/GetName,http://172.16.2.9:5202/T1/Test/GetName,http://172.16.2.9:5203/T1/Test/GetName,根據負載演算法決定轉發規則。
LoadBalancer是來決定負載的演算法
LeastConnection:將請求發往最空閒的那個伺服器
RoundRobin:輪流轉發
NoLoadBalance:總是發往第一個請求或者是服務發現
Routes裡面可以配多個路由轉發。
2.3驗證
在前面的Consul註冊的程式中(5201,5202,5203埠程式)加入一個介面
[Route("api/[controller]/[action]")]
public class TestController : Controller
{
private IConfiguration _configuration;
public TestController(IConfiguration configuration)
{
_configuration = configuration;
}
public IActionResult GetName()
{
string port = _configuration["port"];
return Json($"埠:{port},姓名:張三");
}
}
啟動Ocelot的閘道器程式,埠為5200
訪問地址:http://ip:5200/T1/Test/GetName
重新整理一下
可以看到,已經成功轉發到上面配置好的路由地址。
2.4Ocelot結合Consul進行服務發現
上面的示例是沒有經過Consul的,是直接轉發到相應地址,這顯示又面臨了服務地址管理的問題了,所以需要結束Consul自動發現服務的地址。
把ocelot.json的檔案加入consul配置資訊
{
"Routes": [
{
//轉發到下游服務地址--url變數
"DownstreamPathTemplate": "/api/{url}",
//下游http協議
"DownstreamScheme": "http",
//負載方式,
"LoadBalancerOptions": {
"Type": "RoundRobin" // 輪詢
},
//"DownstreamHostAndPorts": [
// {
// "Host": "172.16.2.9",
// "Port": 5201 //服務埠
// }, //可以多個,自行負載均衡
// {
// "Host": "172.16.2.9",
// "Port": 5202 //服務埠
// },
// {
// "Host": "172.16.2.9",
// "Port": 5203 //服務埠
// }
//],
//上游地址
"UpstreamPathTemplate": "/T1/{url}", //閘道器地址--url變數 //衝突的還可以加權重Priority
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ],
"UseServiceDisConvery": true, //使用服務發現
"ServiceName": "api"//Consul服務名稱
}
],
"GlobalConfiguration": {
//Ocelot應用地址
"BaseUrl": "http://172.16.2.9:5200",
"ServiceDiscoveryProvider": {
//Consul地址
"Host": "172.16.2.84",
//Consul埠
"Port": 8500,
"Type": "Consul"//由Consul提供服務發現,每次請求Consul
}
}
}
可以看到,這裡已經把寫死的下游地址去掉了,加入了Consul的資訊。
安裝NuGet包
Ocelot.Provider.Consul
Startup.cs中ConfigureServices(IServiceCollection services)加入.AddConsul();
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot()
.AddConsul();
}
配置完成,驗證效果,啟動專案。
再次訪問http://ip:5200/T1/Test/GetName
重新整理一下
控制檯資訊
到這就已經成功完成 Ocelot+Consul的閘道器和服務發現功能了。