ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探
概述
Ocelot面向使用.NET運(yùn)行微型服務(wù)/面向服務(wù)的體系結(jié)構(gòu)的人員,這些體系結(jié)構(gòu)需要在系統(tǒng)中具有統(tǒng)一的入口點(diǎn)。特別是我想與IdentityServer參考和承載令牌輕松集成。Ocelot是按特定順序排列的一堆中間件。Ocelot將HttpRequest對(duì)象操作到由其配置指定的狀態(tài),直到到達(dá)請(qǐng)求構(gòu)建器中間件,在該中間件中它創(chuàng)建一個(gè)HttpRequestMessage對(duì)象,該對(duì)象用于向下游服務(wù)發(fā)出請(qǐng)求。發(fā)出請(qǐng)求的中間件是Ocelot管道中的最后一件事。它不會(huì)調(diào)用下一個(gè)中間件。有一塊中間件可將HttpResponseMessage映射到HttpResponse對(duì)象,然后將其返回給客戶端?;旧?,它具有許多其他功能。
代碼實(shí)現(xiàn)
1、新建api客戶端1

2、新建api 網(wǎng)關(guān)test

3、nuget安裝Ocelot

4、Program文件添加ConfigureAppConfiguration
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(conf =>
{
conf.AddJsonFile("ocelot.json", false, true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
5、Startup文件配置
services.AddOcelot(Configuration);
app.UseOcelot().Wait();
6、網(wǎng)關(guān)項(xiàng)目下添加文件ocelot.json
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/WeatherForecast/GetList",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5000
}
],
"UpstreamPathTemplate": "/GetList",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5000
}
],
"UpstreamPathTemplate": "/{everything}",
"UpstreamHttpMethod": [ "Post" ]
},
{
"DownstreamPathTemplate": "/api/WeatherForecast/GetModel?id={s1}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5000
}
],
"UpstreamPathTemplate": "/GetModel?id={s1}",
"UpstreamHttpMethod": [ "Get" ]
}
]
}
7、2個(gè)項(xiàng)目運(yùn)行,測(cè)試

代碼地址
https://gitee.com/conanOpenSource_admin/Example/commit/b3b5a6b15a060b46c5ecd2ea31f0d36791cda18c
以上就是ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core Api網(wǎng)關(guān)Ocelot的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
- ASP.NET?Core設(shè)置Ocelot網(wǎng)關(guān)限流
- ASP.NET?Core中的Ocelot網(wǎng)關(guān)介紹
- .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot超時(shí)、熔斷、限流
- .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot集成Consul
- .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot基礎(chǔ)介紹及集成
- ASP.NET Core3.1 Ocelot負(fù)載均衡的實(shí)現(xiàn)
- ASP.NET Core3.1 Ocelot認(rèn)證的實(shí)現(xiàn)
- ASP.NET Core3.1 Ocelot路由的實(shí)現(xiàn)
- Asp.Net?Core使用Ocelot結(jié)合Consul實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)
相關(guān)文章
asp.net 通過(guò)指定IP地址得到當(dāng)前的網(wǎng)絡(luò)上的主機(jī)的域名
通過(guò)指定的ip地址獲取當(dāng)前網(wǎng)絡(luò)的主機(jī)的域名,大家可以看看2009-02-02
ASP.NET抓取網(wǎng)頁(yè)內(nèi)容的實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET抓取網(wǎng)頁(yè)內(nèi)容的實(shí)現(xiàn)方法,涉及使用HttpWebRequest及WebResponse抓取網(wǎng)頁(yè)內(nèi)容的技巧,需要的朋友可以參考下2015-02-02
ASP.NET MVC中jQuery與angularjs混合應(yīng)用傳參并綁定數(shù)據(jù)
這篇文章主要介紹了ASP.NET MVC中jQuery與angularjs混合應(yīng)用傳參并綁定數(shù)據(jù),需要的朋友可以參考下2017-06-06
ASP.NET Gridview與checkbox全選、全不選實(shí)現(xiàn)代碼
ASP.NET Gridview checkbox全選與全不選實(shí)現(xiàn)代碼,其實(shí)原理就是利用js來(lái)實(shí)現(xiàn)的,但需要簡(jiǎn)單的設(shè)置下回傳。2010-04-04
.NET Framework集成Quartz的實(shí)現(xiàn)示例
本文主要介紹了.NET Framework集成Quartz的實(shí)現(xiàn)示例,Quartz 主要用于定時(shí)執(zhí)行任務(wù)方面,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03
ASP.NET Global.asax應(yīng)用程序文件簡(jiǎn)介
Global.asax 文件,有時(shí)候叫做 ASP.NET 應(yīng)用程序文件,提供了一種在一個(gè)中心位置響應(yīng)應(yīng)用程序級(jí)或模塊級(jí)事件的方法。2009-03-03

