目录
介绍
基本原理
集成方式
快速开始
创建订单服务
创建产品服务
创建网关
运行验证
最后
上篇. Net 微服务实践 (一): 微服务框架选型 我们对微服务框架整体做了介绍, 接下来我们从网关 Ocelot 开始, 一一开始实践
介绍
Ocelot 是一个用. NET Core 实现并且开源的 API 网关, 它功能强大, 包括了: 路由, 请求聚合, 服务发现, 认证, 鉴权, 限流熔断, 并内置了负载均衡器与 Service Fabric,Butterfly,Tracing 集成. 这些功能只都只需要简单的配置即可完成, 下面我们会对这些功能的配置一一进行说明.
基本原理
简单的来说 Ocelot 是一堆的 ASP.NET core middleware 组成的一个管道. 当它拿到请求之后会用一个 request builder 来构造一个 HttpRequestMessage 发到下游的真实服务器, 等下游的服务返回 response 之后再由一个 middleware 将它返回的 HttpResponseMessage 映射到 HttpResponse 对象并返回给客户端.
集成方式
基本集成
用一台 web service 来 host Ocelot, 在这里有一个 JSON 配置文件, 里面设置了所有对当前这个网关的配置. 它会接收所有的客户端请求, 并路由到对应的下游服务器进行处理, 再将请求结果返回. 而这个上下游请求的对应关系也被称之为路由.
集成 IdentityServer
当我们涉及到认证和鉴权的时候, 我们可以跟 Identity Server 进行结合. 当网关需要请求认证信息的时候会与 Identity Server 服务器进行交互来完成.
网关集群
只有一个网关是很危险的, 也就是我们通常所讲的单点, 只要它挂了, 所有的服务全挂. 这显然无法达到高可用, 所以我们也可以部署多台 Ocelot 网关. 当然这个时候在多台网关前, 你还需要一台负载均衡器
Consul 服务发现
在 Ocelot 已经支持简单的负载功能, 也就是当下游服务存在多个结点的时候, Ocelot 能够承担起负载均衡的作用. 但是它不提供健康检查, 服务的注册也只能通过手动在配置文件里面添加完成. 这不够灵活并且在一定程度下会有风险. 这个时候我们就可以用 Consul 来做服务发现, 它能与 Ocelot 完美结合.
集成 Service Fabric
快速开始
创建订单服务
新建一个新建 ASP.NET core Web API 项目, 命名为 order-API, 模拟订单服务
在 order-API 项目中, 添加一个 order API
- [ApiController]
- public class OrderController : ControllerBase
- {
- // GET: API/Product
- [Route("api/orders")]
- [HttpGet]
- public IEnumerable<string> Get()
- {
- return new string[] { "刘明的订单", "王天的订单" };
- }
- }
服务启动地址配置为 http://localhost:5001
创建产品服务
新建一个新建 ASP.NET core Web API 项目, 命名为 product-API, 模拟产品服务
在 product-API 项目中, 添加一个 product API
- [ApiController]
- public class ProductController : ControllerBase
- {
- // GET: API/Product
- [Route("api/products")]
- [HttpGet]
- public IEnumerable<string> Get()
- {
- return new string[] { "笔记本", "口罩" };
- }
- }
服务启动地址配置为 http://localhost:5002
创建网关
新建一个新建 ASP.NET core Web API 项目, 命名为 ocelot-gateway, 这是网关
项目结构图
Nuget 添加 Ocelot package
添加 Ocelot 配置文件, 命名为 Ocelot.JSON (放在 appsettings.JSON 平级目录)
. 我们暂且不管配置文件内容的含义, 先快速把网关运行起来
- {
- "ReRoutes": [
- {
- "DownstreamPathTemplate": "/api/orders",
- "DownstreamScheme": "http",
- "DownstreamHostAndPorts": [
- {
- "Host": "localhost",
- "Port": 5001
- }
- ],
- "UpstreamPathTemplate": "/api/orders",
- "UpstreamHttpMethod": [ "Get" ]
- },
- {
- "DownstreamPathTemplate": "/api/products",
- "DownstreamScheme": "http",
- "DownstreamHostAndPorts": [
- {
- "Host": "localhost",
- "Port": 5002
- }
- ],
- "UpstreamPathTemplate": "/api/products",
- "UpstreamHttpMethod": [ "Get" ]
- }
- ],
- "GlobalConfiguration": {
- "BaseUrl": "http://localhost:5000"
- }
- }
修改 Program.cs 文件, 添加 Ocelot 配置文件
- Host.CreateDefaultBuilder(args)
- .ConfigureAppConfiguration((hostingContext, config) =>
- {
- config
- .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
- .AddJsonFile("appsettings.json", true, true)
- .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
- .AddJsonFile("Ocelot.json",false,true)
- .AddEnvironmentVariables();
- })
修改 Startup.cs, 在 ConfigureServices 中注入 Ocelot 组件
services.AddOcelot()
修改 Startup.cs, 在 Configure 中添加 Ocelot 中间件
App.UseOcelot().Wait();
-- 服务启动地址配置为 http://localhost:5000
运行验证
使用 dotnet run 命令分别运行 ocelot-gateway, order-API, product-API
运行后网关地址为 http://localhost:5000/
获取订单的 API 地址为 http://localhost:5001/API/orders
获取产品的 API 地址为 http://localhost:5002/API/products
使用 Postman 调用 http://localhost:5000/API/orders, http://localhost:5000/API/orders, 会发现返回了订单数据
使用 Postman 调用 http://localhost:5000/API/products, http://localhost:5000/API/products, 会发现返回了产品数据
最后
本篇我们介绍了 Ocelot, 它的实现原理, 以及如何快速创建 ocelot 的 helo world 项目, 下面我们会介绍 Ocelot 的主要特性: 路由
示例代码下载地址: https://github.com/lcyhjx/ocelot-demo/tree/master
来源: https://www.cnblogs.com/lcyhjx/p/12649936.html