.net core ocelot 获取路由的mothed_Net Core微服务入门全纪录(四)Ocelot网关(上)
上一篇【.Net Core微服務(wù)入門(mén)全紀(jì)錄(三)——Consul-服務(wù)注冊(cè)與發(fā)現(xiàn)(下)】已經(jīng)使用Consul完成了服務(wù)的注冊(cè)與發(fā)現(xiàn),實(shí)際中光有服務(wù)注冊(cè)與發(fā)現(xiàn)往往是不夠的,我們需要一個(gè)統(tǒng)一的入口來(lái)連接客戶端與服務(wù)。
Ocelot
官網(wǎng):https://ocelot.readthedocs.io/
Ocelot正是為.Net微服務(wù)體系提供一個(gè)統(tǒng)一的入口點(diǎn),稱為:Gateway(網(wǎng)關(guān))。
- 上手Ocelot:
首先創(chuàng)建一個(gè)空的asp.net core web項(xiàng)目。
注意ocelot.json是我們添加的Ocelot的配置文件,記得設(shè)置生成時(shí)復(fù)制到輸出目錄。ocelot.json的文件名不是固定的,可以自己定義。
NuGet安裝一下Ocelot:
只需簡(jiǎn)單的修改幾處默認(rèn)代碼:
Program.cs:
Startup.cs:
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { //添加ocelot服務(wù) 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) { //設(shè)置Ocelot中間件 app.UseOcelot().Wait(); } }ocelot.json:
{ "Routes": [ { "DownstreamPathTemplate": "/products", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9050 }, { "Host": "localhost", "Port": 9051 }, { "Host": "localhost", "Port": 9052 } ], "UpstreamPathTemplate": "/products", "UpstreamHttpMethod": [ "Get" ], "LoadBalancerOptions": { "Type": "RoundRobin" //負(fù)載均衡,輪詢機(jī)制 LeastConnection/RoundRobin/NoLoadBalancer/CookieStickySessions } }, { "DownstreamPathTemplate": "/orders", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9060 }, { "Host": "localhost", "Port": 9061 }, { "Host": "localhost", "Port": 9062 } ], "UpstreamPathTemplate": "/orders", "UpstreamHttpMethod": [ "Get" ], "LoadBalancerOptions": { "Type": "RoundRobin" //負(fù)載均衡,輪詢機(jī)制 LeastConnection/RoundRobin/NoLoadBalancer/CookieStickySessions } } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:9070" }}我們先暫時(shí)忽略Consul,將服務(wù)實(shí)例的地址都寫(xiě)在配置文件中。要知道Consul、Ocelot等組件都是可以獨(dú)立存在的。
配置文件中的Routes節(jié)點(diǎn)用來(lái)配置路由,Downstream代表下游,也就是服務(wù)實(shí)例,Upstream代表上游,也就是客戶端。我們的路徑比較簡(jiǎn)單,只有/products、/orders,路徑中如果有不固定參數(shù)則使用{}匹配。我們這個(gè)配置的意思呢就是客戶端訪問(wèn)網(wǎng)關(guān)的/orders、/products,網(wǎng)關(guān)會(huì)轉(zhuǎn)發(fā)給服務(wù)實(shí)例的/orders、/products,注意這個(gè)上游的路徑不一定要和下游一致,比如上游路徑可以配置成/api/orders,/xxx都可以。
LoadBalancerOptions節(jié)點(diǎn)用來(lái)配置負(fù)載均衡,Ocelot內(nèi)置了 LeastConnection、RoundRobin、NoLoadBalancer、CookieStickySessions 4種負(fù)載均衡策略。
BaseUrl節(jié)點(diǎn)就是配置我們ocelot網(wǎng)關(guān)將要運(yùn)行的地址。
- 運(yùn)行g(shù)ateway:
目前不考慮網(wǎng)關(guān)集群,就不放在docker里了。直接控制臺(tái)執(zhí)行:`dotnet Ocelot.APIGateway.dll --urls="http://*:9070"
用瀏覽器測(cè)試一下:
測(cè)試正常,我們通過(guò)網(wǎng)關(guān)可以正常的訪問(wèn)到服務(wù)實(shí)例。
- 接下來(lái)繼續(xù)改造客戶端代碼:
因?yàn)楦膭?dòng)太多就直接新建一個(gè)GatewayServiceHelper來(lái)做。
GatewayServiceHelper:
然后在Startup中修改一下注入的類型,別的就不用改了,這就是依賴注入的好處之一。。。
Startup.ConfigureServices():
Startup.Configure():
//程序啟動(dòng)時(shí) 獲取服務(wù)列表//serviceHelper.GetServices();運(yùn)行客戶端測(cè)試:
好了,現(xiàn)在客戶端對(duì)服務(wù)的調(diào)用都通過(guò)網(wǎng)關(guān)進(jìn)行中轉(zhuǎn),客戶端再也不用去關(guān)心那一堆服務(wù)實(shí)例的地址,只需要知道網(wǎng)關(guān)地址就可以了。另外,服務(wù)端也避免了服務(wù)地址直接暴露給客戶端。這樣做對(duì)客戶端,服務(wù)都非常友好。
至于我們的api網(wǎng)關(guān)呢,又要說(shuō)到服務(wù)發(fā)現(xiàn)的問(wèn)題了。目前我們的服務(wù)地址是寫(xiě)在ocelot.json配置文件里的,當(dāng)然這種做法在服務(wù)實(shí)例不經(jīng)常變化的情況下是沒(méi)有問(wèn)題的,一旦服務(wù)變化,需要人為的修改配置文件,這又顯得不太合理了。
當(dāng)然,強(qiáng)大的Ocelot為我們提供了服務(wù)發(fā)現(xiàn)的方案。
代碼放在:https://github.com/xiajingren/NetCoreMicroserviceDemo
總結(jié)
以上是生活随笔為你收集整理的.net core ocelot 获取路由的mothed_Net Core微服务入门全纪录(四)Ocelot网关(上)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python去除数组缺失值_动态数组的应
- 下一篇: 易语言关闭指定窗口_易语言取外部程序指定