ocelot asp.net core 微服务 gateway介绍
https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html
1. 新建asp.net core webapi空项目 AProject, nuget引用ocelot插件
2. 新建asp.net core webapi示例项目BProject,并实现todoitemscontroller get方法,http://localhost:22/api/todoitems
3. 在AProject项目,新建ocelot.json
{ "Routes": [ { //下游请求 "DownstreamPathTemplate": "/api/todoitems/", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 22 } ], //上游请求 "UpstreamPathTemplate": "/todoitems/", "UpstreamHttpMethod": [ "Get" ] } ],
//Gateway 的地址 "GlobalConfiguration": { "BaseUrl": "http://localhost:5211" } }
在AProject项目,Program.cs 文件里 写以下代码
using Ocelot.DependencyInjection; using Ocelot.Middleware; new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { config .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile("ocelot.json") .AddEnvironmentVariables(); }) .ConfigureServices(s => { s.AddOcelot(); }) .ConfigureLogging((hostingContext, logging) => { //add your logging }) .UseIISIntegration() .Configure(app => { app.UseOcelot().Wait(); }) .Build() .Run();
启动AProject、BProject项目, 此时访问:
http://localhost:5211/todoitems/ 会跳到http://localhost:22/api/todoitems/
标签:core,网关,asp,示例,ocelot,net,todoitems,localhost From: https://www.cnblogs.com/haoliansheng/p/16982676.html