日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ocelot网关

發(fā)布時間:2023/12/4 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ocelot网关 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Ocelot是一個.net core框架下的網(wǎng)關(guān)的開源項目,下圖是官方給出的基礎(chǔ)實現(xiàn)圖,即把后臺的多個服務(wù)統(tǒng)一到網(wǎng)關(guān)處,前端應(yīng)用:桌面端,web端,app端都只用訪問網(wǎng)關(guān)即可。

?

Ocelot的實現(xiàn)原理就是把客戶端對網(wǎng)關(guān)的請求(Request),按照configuration.json的映射配置,轉(zhuǎn)發(fā)給對應(yīng)的后端http service,然后從后端http service獲取響應(yīng)(Response)后,再返回給客戶端。當(dāng)然有了網(wǎng)關(guān)后,我們可以在網(wǎng)關(guān)這層去做統(tǒng)一驗證,也可以在網(wǎng)關(guān)處統(tǒng)一作監(jiān)控。

接下來做個Demo

新建三個asp.net core web aip項目:

OcelotGateway網(wǎng)關(guān)項目,端口是5000

DemoAAPI項目A,端口是5001

DemoBAPI項目B,端口是5002

(注:端口可以在每個項目的Properties下的launchSettings.json中修改,發(fā)布后的端口可以在Program.cs中用UseUrls(“http://*:5000”)來修改)

對于OcelotGateway:

引用Ocelot的Nuget包:

視圖->其他窗口->程序包管理控制臺:Install-Package Ocelot

或項目右鍵“管理Nuget程序包”,在瀏覽里查找Ocelot進行安裝

在OcelotGateway項目中添加一個configuration.json文件,關(guān)把它的屬性“復(fù)制到輸出目錄”,設(shè)成“始終復(fù)制”,內(nèi)容如下:

{


? "ReRoutes": [


? ? {


? ? ? "DownstreamPathTemplate": "/demoaapi/values",


? ? ? "DownstreamScheme": "http",


? ? ? "DownstreamPort": 5001,


? ? ? "DownstreamHost": "localhost",


? ? ? "UpstreamPathTemplate": "/demoaapi/values",


? ? ? "UpstreamHttpMethod": [ "Get" ],


? ? ? "QoSOptions": {


? ? ? ? "ExceptionsAllowedBeforeBreaking": 3,


? ? ? ? "DurationOfBreak": 10,


? ? ? ? "TimeoutValue": 5000


? ? ? },


? ? ? "HttpHandlerOptions": {


? ? ? ? "AllowAutoRedirect": false,


? ? ? ? "UseCookieContainer": false


? ? ? },


? ? ? "AuthenticationOptions": {


? ? ? ? "AuthenticationProviderKey": "",


? ? ? ? "AllowedScopes": []


? ? ? }


? ? },


? ? {


? ? ? "DownstreamPathTemplate": "/demobapi/values",


? ? ? "DownstreamScheme": "http",


? ? ? "DownstreamPort": 5002,


? ? ? "DownstreamHost": "localhost",


? ? ? "UpstreamPathTemplate": "/demobapi/values",


? ? ? "UpstreamHttpMethod": [ "Get" ],


? ? ? "QoSOptions": {


? ? ? ? "ExceptionsAllowedBeforeBreaking": 3,


? ? ? ? "DurationOfBreak": 10,


? ? ? ? "TimeoutValue": 5000


? ? ? },


? ? ? "HttpHandlerOptions": {


? ? ? ? "AllowAutoRedirect": false,


? ? ? ? "UseCookieContainer": false


? ? ? },


? ? ? "AuthenticationOptions": {


? ? ? ? "AuthenticationProviderKey": "",


? ? ? ? "AllowedScopes": []


? ? ? }


? ? }


? ]


}

接下來對OcelotGateway的Program.cs進行改造?

using Microsoft.AspNetCore.Hosting;


using Microsoft.Extensions.Configuration;


using Microsoft.Extensions.DependencyInjection;


?


namespace OcelotGateway


{


? ? public class Program


? ? {


? ? ? ? public static void Main(string[] args)


? ? ? ? {


? ? ? ? ? ? BuildWebHost(args).Run();


? ? ? ? }


? ? ? ? public static IWebHost BuildWebHost(string[] args)


? ? ? ? {


? ? ? ? ? ? IWebHostBuilder builder = new WebHostBuilder();


? ? ? ? ? ? //注入WebHostBuilder


? ? ? ? ? ? return builder.ConfigureServices(service =>


? ? ? ? ? ? ? ? {


? ? ? ? ? ? ? ? ? ? service.AddSingleton(builder);


? ? ? ? ? ? ? ? })


? ? ? ? ? ? ? ? //加載configuration配置文人年


? ? ? ? ? ? ? ? .ConfigureAppConfiguration(conbuilder =>


? ? ? ? ? ? ? ? {


? ? ? ? ? ? ? ? ? ? conbuilder.AddJsonFile("configuration.json");


? ? ? ? ? ? ? ? })


? ? ? ? ? ? ? ? .UseKestrel()


? ? ? ? ? ? ? ? .UseUrls("http://*:5000")


? ? ? ? ? ? ? ? .UseStartup<Startup>()


? ? ? ? ? ? ? ? .Build();


? ? ? ? }


? ? }


}

同時,修改Startup.cs


using Microsoft.AspNetCore.Builder;


using Microsoft.AspNetCore.Hosting;


using Microsoft.Extensions.Configuration;


using Microsoft.Extensions.DependencyInjection;


using Ocelot.DependencyInjection;


using Ocelot.Middleware;


?


namespace OcelotGateway


{


? ? public class Startup


? ? {


? ? ? ? public Startup(IConfiguration configuration)


? ? ? ? {


? ? ? ? ? ? Configuration = configuration;


? ? ? ? }


? ? ? ? public IConfiguration Configuration { get; } ? ??


? ? ? ? public void ConfigureServices(IServiceCollection services)


? ? ? ? { ? ? ?


? ? ? ? ? ? //注入配置文件,AddOcelot要求參數(shù)是IConfigurationRoot類型,所以要作個轉(zhuǎn)換


? ? ? ? ? ? services.AddOcelot(Configuration as ConfigurationRoot);


? ? ? ? }


? ? ? ? public ?void Configure(IApplicationBuilder app, IHostingEnvironment env)


? ? ? ? {


? ? ? ? ? ? //添加中間件


? ? ? ? ? ? app.UseOcelot().Wait();


? ? ? ? }


? ? }


}

為了測試數(shù)據(jù)好看,我們把DemoAAPI項目和DemoBAPI項目的ValuesController作一下修改: ? ?


[Route("demoaapi/[controller]")]


? ? public class ValuesController : Controller


? ? { ? ? ??


? ? ? ? [HttpGet]


? ? ? ? public IEnumerable<string> Get()


? ? ? ? {


? ? ? ? ? ? return new string[] { "DemoA服務(wù)", "請求" };


? ? ? ? }


//……


}


?


? ? [Route("demobapi/[controller]")]


? ? public class ValuesController : Controller


? ? { ? ? ??


? ? ? ? [HttpGet]


? ? ? ? public IEnumerable<string> Get()


? ? ? ? {


? ? ? ? ? ? return new string[] { "DemoB服務(wù)", "請求" };


? ? ? ? }


//……


}

最后在解決方案屬性->多個啟動項目中,把DemoAAPI,DemoBAPI,OcelotGateway都設(shè)成啟動,開始啟動解決方案,效果如下圖?

相關(guān)文章:

  • Ocelot——初識基于.Net Core的API網(wǎng)關(guān)

  • Ocelot API網(wǎng)關(guān)的實現(xiàn)剖析

  • 微服務(wù)網(wǎng)關(guān)Ocelot

  • API網(wǎng)關(guān)Ocelot 使用Polly 處理部分失敗問題

  • 談?wù)勎⒎?wù)中的 API 網(wǎng)關(guān)(API Gateway)

原文:http://www.cnblogs.com/axzxs2001/p/8005041.html


.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結(jié)

以上是生活随笔為你收集整理的Ocelot网关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。