(8)學習筆記 ) ASP.NET CORE微服務 Micro-Service ---- Ocelot網關(Api GateWay)

說到如今現有微服務的幾點不足:html

1) 對於在微服務體系中、和 Consul 通信的微服務來說,使用服務名便可訪問。可是對於手 機、web 端等外部訪問者仍然須要和 N 多服務器交互,須要記憶他們的服務器地址、端 口號等。一旦內部發生修改,很麻煩,並且有時候內部服務器是不但願外界直接訪問的。git

2) 各個業務系統的人沒法自由的維護本身負責的服務器;github

3) 現有的微服務都是「我家大門常打開」,沒有作權限校驗。若是把權限校驗代碼寫到每 個微服務上,那麼開發工做量太大。web

4) 很難作限流、收費等。算法


ocelot 中文文檔:https://blog.csdn.net/sD7O95O/article/details/79623654 json

資料:http://www.csharpkit.com/apigateway.html 後端

官網:https://github.com/ThreeMammals/Ocelot api

騰訊.Net 大隊長「張善友」是項目主力開發人員之一。緩存

1、 Ocelot 基本配置

Ocelot 就是一個提供了請求路由、安全驗證等功能的 API 網關微服務。 安全

建一個空的 asp.net core 項目。

Install-Package Ocelot

項目根目錄下建立 configuration.json

ReRoutes 下就是多個路由規則

{
    "ReRoutes": [
        {
            "DownstreamPathTemplate": "/api/{url}",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 5001
                }
            ],
            "UpstreamPathTemplate": "/MsgService/{url}",
            "UpstreamHttpMethod": [
                "Get",
                "Post"
            ]
        },
        {
            "DownstreamPathTemplate": "/api/{url}",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 5003
                }
            ],
            "UpstreamPathTemplate": "/ProductService/{url}",
            "UpstreamHttpMethod": [
                "Get",
                "Post"
            ]
        }
    ]
}

 

Program.cs的CreateWebHostBuilder中

//在原基礎上添加上這倆行
//只是監聽這個端口 你本身配置也能夠
.UseUrls("http://127.0.0.1:8888")
.ConfigureAppConfiguration((hostingContext, builder) => { builder.AddJsonFile("configuration.json", false, true); })

在ConfigureAppConfiguration 中AddJsonFile是解析json配置文件的方法。

爲了確保直接在bin 下直接dotnet運行的時候能找到配置文件,因此要在vs中把配置文件的【複製到輸出目錄】設置爲【如 果若是較新則複製】。

 

Startup.cs中 經過構造函數注入一個 private IConfiguration Configuration;

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseOcelot().Wait();//不要忘了寫Wait
}

這樣當訪問http://127.0.0.1:8888/MsgService/Send?msg=aaa的時候就會訪問 http://127.0.0.1:5002/api/email/Send?msg=aaa

configuration.json中UpstreamHttpMethod表示對什麼樣的請求類型作轉發。

2、 Ocelot+Consul

上面的配置仍是把服務的ip地址寫死了,Ocelot能夠和Consul通信,經過服務名字來配置。

只要改配置文件便可

{
    "ReRoutes": [
        {
            "DownstreamPathTemplate": "/api/{url}",
            "DownstreamScheme": "http",
            "UpstreamPathTemplate": "/MsgService/{url}",
            "UpstreamHttpMethod": [
                "Get",
                "Post"
            ],
            "ServiceName": "MsgService", //服務名字
            "LoadBalancerOptions": {
                "Type": "RoundRobin" //指定一個負載均衡算法:  LeastConnection 最少的鏈接     RoundRobin 輪訓
            },
            "UseServiceDiscovery": true //使用服務發現
        }
    ],
        "GlobalConfiguration": {  //全局配置
        "ServiceDiscoveryProvider": {  //鏈接這臺Consul服務器
            "Host": "localhost",
                "Port": 8500
        }
    }
}

有多個服務就 ReRoutes 下面配置多組便可

 訪問 http://localhost:8888/MsgService/SMS/Send_MI 便可,請求報文體

{phoneNum:"110",msg:"aaaaaaaaaaaaa"}。

 表示只要是/MsgService/開頭的都會轉給後端的服務名爲" MsgService "的一臺服務器,轉發的路徑是"/api/{url}"。

LoadBalancerOptions 中"LeastConnection"表示負載均衡算法是「選擇當前最少鏈接數的服務器」,若是改成 RoundRobin 就是「輪詢」。

ServiceDiscoveryProvider 是 Consul 服務器的配置。

Ocelot 由於是流量中樞,也是能夠作集羣的。

 

 (*) 也 支 持 Eureka 進 行 服 務 的 注 冊 、 查 找

(http://ocelot.readthedocs.io/en/latest/features/servicediscovery.html),也支持訪問 Service Fabric 中的服務(http://ocelot.readthedocs.io/en/latest/features/servicefabric.html)。

3、Ocelot 其餘功能簡單介紹

一、  限流:

文檔:http://ocelot.readthedocs.io/en/latest/features/ratelimiting.html 須要和 Identity Server 一塊兒使用,其餘的限速是針對 clientId 限速,而不是針對 ip 限速。好比我調用微博的api開發了一個如鵬版新浪微博,個人 clientid 是 rpwb,而後限制了 1 秒鐘只能調用 1000 次,那麼全部用如鵬版微博這個 app 的全部用戶加在一塊兒,在一秒鐘以內,不能累計超過 1000 次。目前開放式 api 的限流都是這個套路。

若是要作針對 ip 的限速等,要本身在 Ocelot 前面架設 Nginx 來實現。

二、  請求緩存

http://ocelot.readthedocs.io/en/latest/features/caching.html 只支持 get,只要 url 不變,就會緩存。

三、  QOS(熔斷器)

http://ocelot.readthedocs.io/en/latest/features/qualityofservice.html

 

注:此文章是我看楊中科老師的.Net Core微服務第二版和.Net Core微服務第二版課件整理出來的

相關文章
相關標籤/搜索