[Hei-Ocelot-Gateway ].Net Core Api網關Ocelot的開箱即用版本

Containerizing ASP.net core API Gateways

寫在前面

不少neter都有在用Ocelot作Api網關,可是Ocelot又不像kong或者其餘網關同樣,開箱即用。它須要你單獨開一個web項目來部署,這樣不少同窗都在作重複的事了。html

這裏[Hei.Ocelot.ApiGateway] 就把這件事給作了,之後有同窗要用的話能夠單獨拉下代碼來部署,或者docker/k8s直接部署就行了(這是個人計劃,後續怎麼作可能要看我本身的需求,咱們公司內部部分項目也用);git

--你們也能夠當成一個ocelot的demo哈,畢竟沒什麼代碼量。github

基於此,本文目標讀者是對Ocelot有初步瞭解的同窗。web

項目地址:https://github.com/gebiWangshushu/Hei.Ocelot.ApiGatewaydocker

怎樣跑起來

1597215263573

項目結構很簡單:api

Hei.Ocelot.ApiGateway 是主角,是我配置好的Ocelot網關;安全

Hei.Api 是網關測試用的Api;app

Hei.IdentityServer 是測試用的IdentityServer,給部分本身沒準備好IdentityServer的同窗體驗的;運維

裸機(Host)直接部署ide

直接clone項目下來,按需分別跑起來就行;

docker、docker-compose部署

一、clone項目下來,配置好 /Hei.Ocelot.ApiGateway/config 下的appsettings.yml;

二、把這個整個config目錄拷貝到 /home/heidemo/config (由於我demo裏面掛載在這個目錄);

三、去項目根目錄執行docker-compose up (docker-compose.yml就在根目錄,你能夠註釋掉你不想啓用的service)

k8s部署

一、deploy.yml下載到本地,修改文件後面的ConfigMap節點,這部分是配置,含義跟其餘部署方式同樣;

二、執行kubectl apply -f deploy.yml

我本身部署的

Hei.Ocelot.ApiGateway 網關地址:http://172.16.3.117:5000

Hei.Api地址:http://172.16.3.117:5003

Hei.IdentityServer地址:http://172.16.3.117:5100

經過網關訪問下個人HeiApi:

http://172.16.3.117:5000/user、http://172.16.3.117:5000/WeatherForecast

1597218730749

OK,美

咱們講下各個功能怎麼開啓,隨便簡單聊聊怎麼用。

啓用Admin Api 管理配置

Ocelot 有一堆的配置https://ocelot.readthedocs.io/en/latest/features/configuration.html,Ocelot 支持在運行時動態改配置,Ocelot 提供了對應的Rest Api 修改即時生效。否則每次改一點點配置都要找運維挺麻煩的;

對應的Rest Api是用IdentityServer保護的,能夠直接配置用已搭建好的IdentityServer或者用Ocelot內置的IdentityServer,用來作這個Api的受權。咱們實現的是前者;

開啓配置

appsetting.yml加上如下配置便可啓用:

Administration:
 Path: /administration #這裏是admin api的目錄
 IdentityServer:
  Authority: http://172.16.3.117:5100 #IdentityServer地址
  ApiName: ocelot #這些是我配置好在IdentityServer裏的
  RequireHttpsMetadata: false
  ApiSecret: secret #這些是我配置好在IdentityServer裏的

使用

一、先去IdentityServer申請token

POST http://172.16.3.117:5100/connect/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

1597203533753

二、去Hei-Ocelot-Gateway 查詢配置

GET http://172.16.3.117:5100/administration/configuration HTTP/1.1
Authorization: Bearer token

紅框中的就是步驟1申請的token。

1597203753334

三、更新Hei-Ocelot-Gateway 更新配置

POST http://172.16.3.117:5100/administration/configuration HTTP/1.1
Authorization: Bearer token

1597204124724

我發現這個admin Api配置好的配置,重啓後又會復原爲初始化狀態,不知道是否是Bug。生產謹慎使用或有管理工具每次更新備份好再用。

集成IdentityServer作服務受權

你的網關後面有不少服務,某些服務安全性較高的話可接入IdentityServer作服務受權。

開啓配置

appsetting.yml

IdentityProvider:
 - Authority: http://172.16.3.117:5100
   ApiName: ocelot
   ApiSecret: secret
   RequireHttpsMetadata: false

二、ocelot路由配置

而後使用前面搭建好的Admin Api,或者你用的是配置文件,加上如下Routes:

{
	"DownstreamPathTemplate": "/{url}",
	"DownstreamScheme": "http",
	"DownstreamHostAndPorts": [{
		"Host": "172.16.3.117",
		"Port": 5003
	}],
	"UpstreamPathTemplate": "/protect/{url}",
	"UpstreamHttpMethod": ["Get", "Post", "Put"],
	"AuthenticationOptions": {
		"AuthenticationProviderKey": "ocelot",
		"AllowedScopes": []
	},
	"RouteClaimsRequirement": {}
}

三、測試

咱們再次訪問,http://172.16.3.117:5000/user 的受保護路由 http://172.16.3.117:5000/protect/user

1597219742675

申請token

1597220844358

再次訪問

1597220881342

服務發現

Ocelot 支持Consul和Eureka作服務發現,基本能知足咱們平常需求;

Consul

開啓配置

一、appsetting.yml

GlobalConfiguration:
 ServiceDiscoveryProvider:
  Host: 172.16.3.119 #這是我配置在其餘機器的consul agent,生產用的通常會在本機配個agent
  Port: 8500
  Type: Consul

二、ocelot路由配置

首先要求大家的服務要註冊到Consul,這裏我本身註冊了一個叫MessageApi的服務;

加上如下Routes:

{
    "DownstreamPathTemplate": "/api/{url}",
    "DownstreamScheme": "http",
    "UpstreamPathTemplate": "/consul/{url}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put" ],
    "ServiceName": "MessageApi",
    "LoadBalancerOptions": {
    "Type": "LeastConnection"
    }
}

這樣你訪問網關 http://172.16.3.117:5000/consul/ 就能訪問到對應服務了;

Eureka

開啓配置

一、appsetting.yml

Eureka:
 Client:
  ServiceUrl: http://localhost:8761/eureka/ #你的eureka
  ShouldRegisterWithEureka: false
  ShouldFetchRegistry: true

二、ocelot配置

GlobalConfiguration:
  ServiceDiscoveryProvider:
   Type: Eureka

三、測試

略,eureka環境給我刪了,今天懶得搭了,若是需求強烈,我加上。

K8s支持

ocelot是支持k8s的,若是你啓用k8s,那它在k8s集羣裏的角色比較接近於「ocelot-ingress」吧;而後我用的是

<PackageReference Include="Ocelot.Provider.Kubernetes" Version="16.0.1" />

16.0.0 有點問題,直接用AddKubernetes()訪問總會報錯,我換了種方式實現;

開啓配置

開啓前你確定要搭建好k8s集羣了;

一、ocelot配置

GlobalConfiguration:
  ServiceDiscoveryProvider:
   Type: Kube
   NameSpace: dotnetcore #這是我本身部署的HeiApi的命名空間,你的若是你的api有多個命名空間能夠在路由裏配置

二、ocelot新增路由

{
    "DownstreamPathTemplate": "/{url}",
    "DownstreamScheme": "http",
    "UpstreamPathTemplate": "/kube/{url}",
    "ServiceName": "hei-ocelot-api",  
    #"Namespace": "dev",  #好比這裏你的這個路由對應的serverName不是dotnetcore,你能夠這樣配置
    "UpstreamHttpMethod": [ "Get" ]
  }

三、測試

咱們來訪問咱們剛剛新增的路由對應地址: http://172.16.1.30:31500/kube/user (之因此換了地址是由於我剛剛172。16.3.117那臺機沒搭k8s環境)

1597229456658

你們也看到服務發現和k8s(在ocelot這裏也是一種新式的服務發現)都在配置GlobalConfiguration:ServiceDiscoveryProvider: 下面,那Consul和eureka和k8s是互斥的,都有配置的話優先級consul>eureka>k8s

總結

我大概看着本身的需求實現了部分須要單獨引用拓展包才能啓用的功能,可是還有部分功能未有實現,好比Caching、Tracing這些(你們能夠修改測好後直接提pr,我不是懶得寫而是測試麻煩,懶哈哈)

一樣,不須要引用包,單獨配置就能夠啓用的功能,都一一保留着,好比

  • 限流
  • 服務熔斷降級
  • 求求合併
  • 請求頭轉換等等

參考

https://ocelot.readthedocs.io/en/latest/

項目地址

https://github.com/gebiWangshushu/Hei.Ocelot.ApiGateway (喜歡的話給我點個星~~)

相關文章
相關標籤/搜索