新聞中心
這篇文章主要講解了“.NET Core ocelot怎么安裝配置”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“.NET Core ocelot怎么安裝配置”吧!
創(chuàng)新互聯(lián)是專業(yè)的梅江網(wǎng)站建設(shè)公司,梅江接單;提供成都做網(wǎng)站、網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行梅江網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
本文采用Ocelot作為Api網(wǎng)關(guān)。
環(huán)境要求:
vs2019
.NetCore3.1
Ocelot16.0.1
創(chuàng)建一個產(chǎn)品服務(wù)Api站點(AAStore.ProductCatalog.Api)
添加一個ProductController
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
[HttpGet(template:"Get")]
public string GetProductById()
{
return "Product service";
}
}
運行瀏覽
然后再創(chuàng)建一個訂單服務(wù)Api站點(AAStore.Orde.Api)
添加一個OrderController
[Route("api/[controller]")]
[ApiController]
public class OrderController : ControllerBase
{
[HttpGet(template:"Get")]
public string GetOrder()
{
return "Order Service";
}
}
運行瀏覽
兩個服務(wù)已經(jīng)已經(jīng)準(zhǔn)備好了,最后創(chuàng)建一個網(wǎng)關(guān)站點(AAStore.WebApiGateway)
安裝Ocelot
創(chuàng)建一個json配置文件(ocelot.json)
{
"Routes": [
{
"DownstreamPathTemplate": "/api/Product/get",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8081
}
],
"UpstreamPathTemplate": "/api/Product/{everything}",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/Order/get",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8082
}
],
"UpstreamPathTemplate": "/api/Order/get",
"UpstreamHttpMethod": [ "Get" ]
}
]
}
ocelot api網(wǎng)關(guān)的主要功能是接收傳入的HTTP請求并將其轉(zhuǎn)發(fā)到下游服務(wù),目前作為一個HTTP請求。Ocelot將一個請求到另一個請求的路由描述為Routes。
DownstreamPathTemplate、Scheme 和 DownstreamHostAndPorts 構(gòu)成要將此請求轉(zhuǎn)發(fā)到的內(nèi)部微服務(wù) URL。
端口是服務(wù)使用的內(nèi)部端口。使用容器時,在其 dockerfile 中指定端口。Host 是一個服務(wù)名稱,取決于使用的服務(wù)名稱解析。使用 docker-compose 時,服務(wù)名稱由 Docker 主機(jī)提供,它使用 docker-compose 文件中提供的服務(wù)名稱。如果使用 Kubernetes 或 Service Fabric 等業(yè)務(wù)流程協(xié)調(diào)程序,則應(yīng)通過每個業(yè)務(wù)流程協(xié)調(diào)程序提供的 DNS 或名稱解析來解析該名稱。
DownstreamHostAndPorts 是一個數(shù)組,包含要將請求轉(zhuǎn)發(fā)到的任何下游服務(wù)的主機(jī)和端口。通常這只包含一個條目,但有時可能想要將均衡請求加載到下游服務(wù),而通過 Ocelot 即可添加多個條目,然后選擇負(fù)載均衡器。但是如果使用 Azure 和任何業(yè)務(wù)流程協(xié)調(diào)程序,那么通過云和業(yè)務(wù)流程協(xié)調(diào)程序基礎(chǔ)結(jié)構(gòu)進(jìn)行負(fù)載均衡可能會更好。
UpstreamPathTemplate 是一個 URL,Ocelot 將其用來識別用于客戶端中給定請求的 DownstreamPathTemplate。最后,使用了 UpstreamHttpMethod,因此 Ocelot 可區(qū)分對相同 URL 的不同的請求(GET、POST、PUT)。
注意: ocelot16.x版本之后的配置節(jié)點寫為Routes,而非ReRoutes 否則會報錯(Failed to mat ch Route configuration for upstream path)。
在Program.cs 通過AddJsonFile方法向生成器提供ocelot.json文件、添加Ocelot服務(wù)(AddOcelot)和添加ocelot中間件(UseOcelot)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("ocelot.json")
.AddEnvironmentVariables();
;
})
.ConfigureServices(services =>
{
services.AddOcelot();
services.AddHttpContextAccessor();
})
.Configure(app =>
{
app.UseOcelot().Wait();
});
});
然后運行網(wǎng)關(guān),通過網(wǎng)關(guān)訪問產(chǎn)品、訂單微服務(wù):
如果運氣好的話,跟著一步一步做,你也可以運行成功。當(dāng)然ocelot還有很多功能如:路由、請求聚合、服務(wù)發(fā)現(xiàn)、WebSockets、認(rèn)證、授權(quán)、LB、K8S、限流、熔斷等等。
感謝各位的閱讀,以上就是“.NET Core ocelot怎么安裝配置”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對.NET Core ocelot怎么安裝配置這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
當(dāng)前標(biāo)題:.NETCoreocelot怎么安裝配置
轉(zhuǎn)載來源:http://www.ef60e0e.cn/article/pgpjph.html