• [Hei-Ocelot-Gateway ].Net Core Api网关Ocelot的开箱即用版本


    Containerizing ASP.net core API Gateways

    写在前面

    很多neter都有在用Ocelot做Api网关,但是Ocelot又不像kong或者其他网关一样,开箱即用。它需要你单独开一个web项目来部署,这样很多同学都在做重复的事了。

    这里[Hei.Ocelot.ApiGateway] 就把这件事给做了,以后有同学要用的话可以单独拉下代码来部署,或者docker/k8s直接部署就好了(这是我的计划,后续怎么做可能要看我自己的需求,我们公司内部部分项目也用);

    --大家也可以当成一个ocelot的demo哈,毕竟没什么代码量。

    基于此,本文目标读者是对Ocelot有初步了解的同学。

    项目地址:https://github.com/gebiWangshushu/Hei.Ocelot.ApiGateway

    怎样跑起来

    1597215263573

    项目结构很简单:

    Hei.Ocelot.ApiGateway 是主角,是我配置好的Ocelot网关;

    Hei.Api 是网关测试用的Api;

    Hei.IdentityServer 是测试用的IdentityServer,给部分自己没准备好IdentityServer的同学体验的;

    裸机(Host)直接部署

    直接clone项目下来,按需分别跑起来就行;

    docker、docker-compose部署

    1、clone项目下来,配置好 /Hei.Ocelot.ApiGateway/config 下的appsettings.yml;

    2、把这个整个config目录拷贝到 /home/heidemo/config (因为我demo里面挂载在这个目录);

    3、去项目根目录执行docker-compose up (docker-compose.yml就在根目录,你可以注释掉你不想启用的service)

    k8s部署

    1、deploy.yml下载到本地,修改文件后面的ConfigMap节点,这部分是配置,含义跟其他部署方式一样;

    2、执行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里的
    

    使用

    1、先去IdentityServer申请token

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

    1597203533753

    2、去Hei-Ocelot-Gateway 查询配置

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

    红框中的就是步骤1申请的token。

    1597203753334

    3、更新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
    

    2、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": {}
    }
    

    3、测试

    我们再次访问,http://172.16.3.117:5000/user 的受保护路由 http://172.16.3.117:5000/protect/user

    1597219742675

    申请token

    1597220844358

    再次访问

    1597220881342

    服务发现

    Ocelot 支持Consul和Eureka做服务发现,基本能满足我们日常需求;

    Consul

    开启配置

    1、appsetting.yml

    GlobalConfiguration:
     ServiceDiscoveryProvider:
      Host: 172.16.3.119 #这是我配置在其他机器的consul agent,生产用的一般会在本机配个agent
      Port: 8500
      Type: Consul
    

    2、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

    开启配置

    1、appsetting.yml

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

    2、ocelot配置

    GlobalConfiguration:
      ServiceDiscoveryProvider:
       Type: Eureka
    

    3、测试

    略,eureka环境给我删了,今天懒得搭了,如果需求强烈,我加上。

    K8s支持

    ocelot是支持k8s的,如果你启用k8s,那它在k8s集群里的角色比较接近于“ocelot-ingress”吧;然后我用的是

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

    16.0.0 有点问题,直接用AddKubernetes()访问总会报错,我换了种方式实现;

    开启配置

    开启前你肯定要搭建好k8s集群了;

    1、ocelot配置

    GlobalConfiguration:
      ServiceDiscoveryProvider:
       Type: Kube
       NameSpace: dotnetcore #这是我自己部署的HeiApi的命名空间,你的如果你的api有多个命名空间可以在路由里配置
    

    2、ocelot新增路由

     {
        "DownstreamPathTemplate": "/{url}",
        "DownstreamScheme": "http",
        "UpstreamPathTemplate": "/kube/{url}",
        "ServiceName": "hei-ocelot-api",  
        #"Namespace": "dev",  #比如这里你的这个路由对应的serverName不是dotnetcore,你可以这样配置
        "UpstreamHttpMethod": [ "Get" ]
      }
    

    3、测试

    我们来访问我们刚刚新增的路由对应地址: 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 (喜欢的话给我点个星~~)

  • 相关阅读:
    CCF-CSP认证 C++题解目录
    LeetCode周赛#205
    LeetCode双周赛#34
    Codeforces Round #667 (Div. 3) B、C、D、E 题解
    Codeforces Round #656 (Div. 3) 题解
    牛客小白月赛#26 题解
    LeetCode周赛#204 题解
    LeetCode周赛#203 题解
    牛客小白月赛#27 题解
    N阶上楼梯问题——动态规划(递推求解)
  • 原文地址:https://www.cnblogs.com/xiaxiaolu/p/13514942.html
Copyright © 2020-2023  润新知