99re6这里有精品热视频,久久综合九色欧美综合狠狠,国产精品久久久久久无毒不卡,av免费无插件在线观看,欧美放荡办公室videos

當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  編程技術(shù) >  正文

ASP.NET Core3.1 Ocelot負(fù)載均衡的實(shí)現(xiàn)

 2020-11-20 14:46  來(lái)源: 腳本之家   我來(lái)投稿 撤稿糾錯(cuò)

  阿里云優(yōu)惠券 先領(lǐng)券再下單

這篇文章主要介紹了ASP.NET Core3.1 Ocelot負(fù)載均衡的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.負(fù)載均衡

Ocelot可以在每個(gè)路由的可用下游服務(wù)中實(shí)現(xiàn)負(fù)載均衡,這使我們更有效地選擇下游服務(wù)來(lái)處理請(qǐng)求。負(fù)載均衡類(lèi)型:

LeastConnection:根據(jù)服務(wù)正在處理請(qǐng)求量的情況來(lái)決定哪個(gè)服務(wù)來(lái)處理新請(qǐng)求,即將新請(qǐng)求發(fā)送到具有最少現(xiàn)有請(qǐng)求的服務(wù)去處理。算法狀態(tài)沒(méi)有分布在Ocelot集群中。

RoundRobin:遍歷可用服務(wù)并發(fā)送請(qǐng)求。算法狀態(tài)沒(méi)有分布在Ocelot集群中。

NoLoadBalancer:從配置或服務(wù)發(fā)現(xiàn)中獲取第一個(gè)可用服務(wù)來(lái)處理新請(qǐng)求。

CookieStickySessions:通過(guò)使用Cookie,確保特定的請(qǐng)求能夠被分配到特定的服務(wù)上進(jìn)行處理。

在Ocelot負(fù)載均衡項(xiàng)目示例中,通過(guò)網(wǎng)關(guān)項(xiàng)目的路由LoadBalancerOptions選項(xiàng)可以配置負(fù)載均衡類(lèi)型:

{
"Routes": [
{
//下游路由服務(wù)地址
"DownstreamPathTemplate": "/api/values",
//下游服務(wù)地址訪(fǎng)問(wèn)協(xié)議類(lèi)型http或者h(yuǎn)ttps
"DownstreamScheme": "http",
//下游服務(wù)的主機(jī)和端口
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 9001
},
{
"Host": "localhost",
"Port": 9002
}
],
//上游服務(wù)地址,即下游服務(wù)真實(shí)訪(fǎng)問(wèn)地址
"UpstreamPathTemplate": "/",
//負(fù)載均衡類(lèi)型:輪詢(xún)
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
//上游服務(wù)HTTP請(qǐng)求方式,例如Get、Post
"UpstreamHttpMethod": [ "Get" ]
}
]
}

新請(qǐng)求通過(guò)上游訪(fǎng)問(wèn)下游服務(wù)的時(shí)候,Ocelot會(huì)根據(jù)LoadBalancerOptions負(fù)載均衡選項(xiàng)類(lèi)型來(lái)分發(fā)到具體下游服務(wù)。

2.服務(wù)發(fā)現(xiàn)

下面展示如何使用服務(wù)發(fā)現(xiàn)來(lái)設(shè)置路由:

{
"DownstreamPathTemplate": "/api/posts/{postId}",
"DownstreamScheme": "https",
"UpstreamPathTemplate": "/posts/{postId}",
"UpstreamHttpMethod": [ "Put" ],
"ServiceName": "product",
"LoadBalancerOptions": {
"Type": "LeastConnection"
}
}

設(shè)置此選項(xiàng)后,Ocelot將從服務(wù)發(fā)現(xiàn)提供程序中查找下游主機(jī)和端口,并在所有可用服務(wù)中進(jìn)行負(fù)載平衡請(qǐng)求。如果您從服務(wù)發(fā)現(xiàn)提供者(領(lǐng)事)中添加和刪除服務(wù),Ocelot會(huì)停止調(diào)用已刪除的服務(wù),并開(kāi)始調(diào)用已添加的服務(wù)。后續(xù)學(xué)習(xí)服務(wù)發(fā)現(xiàn)這塊知識(shí)點(diǎn)時(shí)候會(huì)重新再講解。

3.項(xiàng)目演示

3.1APIGateway項(xiàng)目

該項(xiàng)目通過(guò)LoadBalancerOptions配置選項(xiàng)定義服務(wù)負(fù)載均衡請(qǐng)求機(jī)制,事例項(xiàng)目使用的負(fù)載均衡類(lèi)型是RoundRobin,在Program添加Ocelot支持代碼如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
//.UseStartup<Startup>()
.UseUrls("http://*:9000")
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
//添加Ocelot配置文件
.AddJsonFile("configuration.json")
.AddEnvironmentVariables();
})
.ConfigureServices(s =>
{
//添加Ocelot服務(wù);
s.AddOcelot();
})
.Configure(a =>
{
    //使用Ocelot
a.UseOcelot().Wait();
});

3.2APIServicesA和APIServicesB下游服務(wù)項(xiàng)目

APIServicesA和APIServicesB項(xiàng)目分別新建兩個(gè)GET請(qǐng)求方法,代碼分別如下:

//APIServicesA
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public string Get()
{
return "From APIServiceA";
}
}
//APIServicesB
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public string Get()
{
return "From APIServiceB";
}
}

通過(guò)dotnet run命令啟動(dòng)APIGateway項(xiàng)目(網(wǎng)關(guān)層)

dotnet run --project APIGateway項(xiàng)目路徑\APIGateway.csproj

通過(guò)dotnet run命令啟動(dòng)APIServicesA項(xiàng)目

dotnet run --project APIGateway項(xiàng)目路徑\APIGateway.csproj

通過(guò)dotnet run命令啟動(dòng)APIServicesB項(xiàng)目

dotnet run --project APIServicesB項(xiàng)目路徑\APIServicesB.csproj

通過(guò)瀏覽器查看輪詢(xún)分發(fā)給下游服務(wù)返回的結(jié)果:

負(fù)載均衡輪詢(xún)分發(fā)下游服務(wù)成功。

4.自定義負(fù)載均衡

Ocelot支持自定義負(fù)載均衡的方法。自定義負(fù)載均衡的類(lèi)需要繼承ILoadBalancer接口類(lèi),下面我們定義一個(gè)簡(jiǎn)單的負(fù)載均衡循環(huán)輸出下游服務(wù)的示例:

public class CustomLoadBalancer : ILoadBalancer
{
private readonly Func<Task<List<Service>>> _services;
private readonly object _lock = new object();
private int _last;

public CustomLoadBalancer(Func<Task<List<Service>>> services)
{
_services = services;
}
public async Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
{
var services = await _services();
lock (_lock)
{
if (_last >= services.Count)
{
_last = 0;
}
var next = services[_last];
_last++;
return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
}
}
public void Release(ServiceHostAndPort hostAndPort)
{
}
}

在Ocelot中注冊(cè)此類(lèi):

Func<IServiceProvider, DownstreamRoute, IServiceDiscoveryProvider, CustomLoadBalancer> loadBalancerFactoryFunc =
(serviceProvider, Route, serviceDiscoveryProvider) => new CustomLoadBalancer(serviceDiscoveryProvider.Get);
s.AddOcelot().AddCustomLoadBalancer(loadBalancerFactoryFunc);

最后在路由的LoadBalancerOptions配置選項(xiàng)上修改為CustomLoadBalancer自定義負(fù)載均衡類(lèi)名:

"LoadBalancerOptions": {
"Type": "CustomLoadBalancer"
}

運(yùn)行項(xiàng)目調(diào)試查看結(jié)果:

從上面結(jié)果來(lái)看,自定義負(fù)載均衡成功。

參考文獻(xiàn):

Ocelot官網(wǎng)

到此這篇關(guān)于ASP.NET Core3.1 Ocelot負(fù)載均衡的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)ASP.NET Core3.1 Ocelot負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

來(lái)源:腳本之家

鏈接:https://www.jb51.net/article/199546.htm

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
asp.net
.net開(kāi)發(fā)

相關(guān)文章

熱門(mén)排行

信息推薦