# 建立API网关(Build an API Gateway)笔记
推荐:
- Envoy
- Ocelot
See also:
- Istio
相关概念Concept
- proxy server
代理可以为整个通信带来更多的功能:拦截、统计、缓存、分发、跳板、注入......
- Authentication(认证):who r u? 你是谁,确认是不是合法用户。常用的认证方式有用户名密码认证。
- Authorization(授权):allow or deny? 你是否有某个权限。当用户需要使用某个功能的时候,系统需要校验用户是否拥有这个功能的权限。
- Service Mesh
- Sidecar
- Envoy mesh: Upstream Hosts+Envoy(Listener)
- Host:能够进行网络通信的实体(如服务器上的应用程序)
- Downstream:下游主机连接到Envoy,发送请求并接收响应
- Upstream:上游主机接收来自Envoy连接和请求并返回响应
- Listener:可以被下游客户端连接的命名网络(如端口、unix套接字)
- Cluster:Envoy连接到的一组逻辑上相似的上游主机
- Mesh:以提供一致的网络拓扑的一组主机
- Runtime configuration:与Envoy一起部署的外置实时配置系统
DotNet Core备注:
- UseAuthorization在asp.net core 2.0中是没有的。
- 在3.0之后微软明确的把授权功能提取到Authorization中间件里,需要在UseAuthentication之后再次UseAuthorization。否则,当使用授权功能比如使用[Authorize]属性时系统会报错。
部分文字来源:CSDN 简书 著作权归作者所有。
Envoy
Envoy is an open source edge and service proxy, designed for cloud-native applications. (官网定义)
参考网址
网上教程
- Envoy 实践(jianshu)
Ocelot
What is Ocelot
Ocelot is a ASP.NET Core (Supports .Net Core 3.1) API Gateway. It's a NuGet package, which can be added to any ASP.NET Core application to make it a API Gateway.
Ocelot API Gateway supports all the feature that any standard API Gateway does.
API Gateway with Ocelot
---> xxx Microservice
Internet Request -->API Gateway--|---> User Microservice
---> Report Microservice
Features
Features of API Gateway:
- 路由 Routing
- 请求聚合 Request Aggregation
- 认证 Authentication
- 授权 Authorization
- 限流 Rate Limiting
- 缓存 Caching
- 负载均衡 Load Balancing
ocelot在.net core 3.1实践
示例:OcelotDemo
前端API接口:localhost:5021/api/weather
后台调用:localhost:5001/weatherforecast
(out of box)
功能实现 Focus on:
- Routing
- Ahthentication
- Response Caching
- Rate limiting
来源:DotNet Core Central(Youtube)
- How to build an API Gateway in ASP.NET Core Using Ocelot(Build API Gateway in a few minutes)
- ASP.NET Core Authentication with JWT (JSON Web Token)
网上教程:
- Ocelot简易教程目录(cnblogs)
Istio
连接、安全加固、控制和观察服务(微服务)的开放平台。 An open platform to connect, secure, control and observe services.
Istio 的主要功能
- 连接(Connect):智能控制服务之间的调用流量,能够实现灰度升级、AB 测试和红黑部署等功能
- 安全加固(Secure):自动为服务之间的调用提供认证、授权和加密。
- 控制(Control):应用用户定义的 policy,保证资源在消费者中公平分配。
- 观察(Observe):查看服务运行期间的各种数据,比如日志、监控和 tracing,了解服务的运行情况。
入门阅读:
- Istio是啥?一文带你彻底了解 (来源:uml.org.cn)