如何将ASP.NET Core 3.1 Ocelot路由实现应用于长尾关键词处理?
- 内容介绍
- 文章标签
- 相关推荐
本文共计619个文字,预计阅读时间需要3分钟。
1. 路径+上一章节已介绍Ocelot,大家已了解其功能。Ocelot主要功能是接收客户端传入的HTTP请求,并将其转发到下游服务。目前Ocelot仅支持另一种HTTP请求形式的此功能。
1.路由
前一个章节我们已经介绍过Ocelot,相信大家也了解到,Ocelot的主要功能是接收客户端等传入的HTTP请求,并将其转发到下游服务。Ocelot当前仅以另一个localhost:9001/api/customers "DownstreamPathTemplate": "/api/customers", "DownstreamScheme": "localhost:9000/customers 实际是转发到了localhost:9001/api/customers的服务 "UpstreamPathTemplate": "/customers", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/api/customers/{id}", "DownstreamScheme": "*:9000") .ConfigureAppConfiguration((hostingContext, config) => { //添加Ocelot配置文件 config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("configuration.json") .AddEnvironmentVariables(); }) .ConfigureServices(s => { //添加Ocelot服务 s.AddOcelot(); }) .Configure(a => { //使用Ocelot中间件 a.UseOcelot().Wait(); });
Ocelot的配置如上代码基本完成了,下面我们看看一个基础Ocelot路由正常工作流程。
2.3CustomersAPIServices和ProductsAPIServices项目
CustomersAPIServices项目的CustomersController有如下两个方法:
[Route("api/[controller]")] public class CustomersController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Catcher Wong", "James Li" }; } [HttpGet("{id}")] public string Get(int id) { return $"Catcher Wong - {id}"; } }
ProductsAPIServices项目的ProductsController有如下一个方法:
[Route("api/[controller]")] public class ProductsController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Surface Book 2", "Mac Book Pro" }; } }
2.4运行测试
上面这三个下游路由地址根据configuration.json配置文件都分别配置了上游分发地址,我们把这三个项目根据配置信息分别在IIS上部署起来,当然你们也可以使用dotnet run命令分别启动这个三个项目。APIGateway、CustomersAPIServices、ProductsAPIServices项目绑定主机地址分别是localhost:9000、localhost:9001、localhost:9002。
当我们在浏览器上打开localhost:9000/customers时候,会发现浏览器上输出如下信息:
为什么输入网关主机地址,返回过来却是客户主机处理结果?那是因为当客户端访问上游服务localhost:9000/customers时候,Ocelot会根据配置信息中下游模版把请求分发到localhost:9001/api/Customers/Get去处理,然后返回结果。
而当我们打开localhost:9000/customers/1时候,也会输出如下信息:
配置信息中上游模版/customers/{id}对应下游模版/api/customers/{id},当我们请求的路径为localhost:9000/customers/1时候,Ocelot会根据配置信息分发到对应的下游路由localhost:9001/api/Customers/Get/1去处理,然后返回结果。
同理,当客户端访问上游服务localhost:9000/products时候,Ocelot也会分发到对应的下游路由localhost:9002/api/Products去处理,然后返回结果:
根据上面测试结果,也就是说我们的Ocelot已经在起作用了,而且根据上下游路由进行了映射。当然该章节也只是简单介绍Ocelot路由功能,而configuration.json配置中有些属性还没有进行描述,例如负载均衡、限流,熔断等等。下面我会继续根据GitHub贡献者开源项目继续讲解其功能。
参考文献:
Ocelot官网
到此这篇关于ASP.NET Core3.1 Ocelot路由的实现的文章就介绍到这了,更多相关ASP.NET Core Ocelot路由内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计619个文字,预计阅读时间需要3分钟。
1. 路径+上一章节已介绍Ocelot,大家已了解其功能。Ocelot主要功能是接收客户端传入的HTTP请求,并将其转发到下游服务。目前Ocelot仅支持另一种HTTP请求形式的此功能。
1.路由
前一个章节我们已经介绍过Ocelot,相信大家也了解到,Ocelot的主要功能是接收客户端等传入的HTTP请求,并将其转发到下游服务。Ocelot当前仅以另一个localhost:9001/api/customers "DownstreamPathTemplate": "/api/customers", "DownstreamScheme": "localhost:9000/customers 实际是转发到了localhost:9001/api/customers的服务 "UpstreamPathTemplate": "/customers", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/api/customers/{id}", "DownstreamScheme": "*:9000") .ConfigureAppConfiguration((hostingContext, config) => { //添加Ocelot配置文件 config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("configuration.json") .AddEnvironmentVariables(); }) .ConfigureServices(s => { //添加Ocelot服务 s.AddOcelot(); }) .Configure(a => { //使用Ocelot中间件 a.UseOcelot().Wait(); });
Ocelot的配置如上代码基本完成了,下面我们看看一个基础Ocelot路由正常工作流程。
2.3CustomersAPIServices和ProductsAPIServices项目
CustomersAPIServices项目的CustomersController有如下两个方法:
[Route("api/[controller]")] public class CustomersController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Catcher Wong", "James Li" }; } [HttpGet("{id}")] public string Get(int id) { return $"Catcher Wong - {id}"; } }
ProductsAPIServices项目的ProductsController有如下一个方法:
[Route("api/[controller]")] public class ProductsController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Surface Book 2", "Mac Book Pro" }; } }
2.4运行测试
上面这三个下游路由地址根据configuration.json配置文件都分别配置了上游分发地址,我们把这三个项目根据配置信息分别在IIS上部署起来,当然你们也可以使用dotnet run命令分别启动这个三个项目。APIGateway、CustomersAPIServices、ProductsAPIServices项目绑定主机地址分别是localhost:9000、localhost:9001、localhost:9002。
当我们在浏览器上打开localhost:9000/customers时候,会发现浏览器上输出如下信息:
为什么输入网关主机地址,返回过来却是客户主机处理结果?那是因为当客户端访问上游服务localhost:9000/customers时候,Ocelot会根据配置信息中下游模版把请求分发到localhost:9001/api/Customers/Get去处理,然后返回结果。
而当我们打开localhost:9000/customers/1时候,也会输出如下信息:
配置信息中上游模版/customers/{id}对应下游模版/api/customers/{id},当我们请求的路径为localhost:9000/customers/1时候,Ocelot会根据配置信息分发到对应的下游路由localhost:9001/api/Customers/Get/1去处理,然后返回结果。
同理,当客户端访问上游服务localhost:9000/products时候,Ocelot也会分发到对应的下游路由localhost:9002/api/Products去处理,然后返回结果:
根据上面测试结果,也就是说我们的Ocelot已经在起作用了,而且根据上下游路由进行了映射。当然该章节也只是简单介绍Ocelot路由功能,而configuration.json配置中有些属性还没有进行描述,例如负载均衡、限流,熔断等等。下面我会继续根据GitHub贡献者开源项目继续讲解其功能。
参考文献:
Ocelot官网
到此这篇关于ASP.NET Core3.1 Ocelot路由的实现的文章就介绍到这了,更多相关ASP.NET Core Ocelot路由内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

