配置Ocelot后.NET7本地WeatherForecast接口返回503错误求助
.NET7 API集成Ocelot后本地WeatherForecast接口返回503错误的解决方法
需求背景
在仅包含WeatherForecast控制器的.NET7简易API项目中集成Ocelot,实现:
- 所有以
/OtherRequest开头的请求路由到外部APIrespondingdomain.com /WeatherForecast请求由本地控制器处理- 应用运行在5287端口
现有代码与配置
Program.cs代码
using Microsoft.Extensions.Configuration; using Ocelot.DependencyInjection; using Ocelot.Middleware; var builder = WebApplication.CreateBuilder(args); // 注册服务 builder.Configuration.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true).Build(); builder.Services.AddOcelot(); builder.Services.AddControllers(); var app = builder.Build(); // 配置HTTP管道 app.UseAuthorization(); app.UseOcelot().Wait(); app.MapControllers(); app.Run();
初始Ocelot.json配置
{ "Routes": [ { "DownstreamPathTemplate": "/OtherRequest/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "respondingdomain.com", "Port": 443 } ], "DelegatingHandlers": [ "OcelotLoggingHandler" ], "UpstreamPathTemplate": "/OtherRequest/{everything}", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ], "RouteIsCaseSensitive": false }, { "DownstreamPathTemplate": "/weatherforecast", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5287 } ], "UpstreamPathTemplate": "/weatherforecast", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ], "RouteIsCaseSensitive": false } ] }
尝试添加GlobalConfiguration后的Ocelot.json
{ "Routes": [ { "DownstreamPathTemplate": "/OtherRequest/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "respondingdomain.com", "Port": 443 } ], "DelegatingHandlers": [ "OcelotLoggingHandler" ], "UpstreamPathTemplate": "/OtherRequest/{everything}", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE"], "RouteIsCaseSensitive": false }, { "DownstreamPathTemplate": "/weatherforecast", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5287 } ], "UpstreamPathTemplate": "/weatherforecast", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ], "RouteIsCaseSensitive": false } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:5287" } }
WeatherForecast控制器代码
namespace ApiManager.Ocelot.Controllers { [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } [HttpGet] public IEnumerable<WeatherForecast> Get() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray(); } } }
问题现象
集成Ocelot后,/WeatherForecast请求返回503错误,但未使用Ocelot时该接口可正常响应。
解决方法
核心问题分析
为本地接口配置Ocelot路由后,Ocelot会尝试将请求转发到localhost:5287,此时应用本身正在占用该端口,会出现循环请求或端口冲突,导致503错误。同时,未实现的OcelotLoggingHandler委托处理器也可能引发异常。
具体调整步骤
删除本地接口的Ocelot路由配置
本地接口不需要Ocelot转发,直接由MapControllers()处理即可,删除Ocelot.json中关于/weatherforecast的路由节点。移除未实现的委托处理器
暂时删除DelegatingHandlers配置,排除不必要的干扰。保持中间件顺序
UseOcelot()需放在MapControllers()之前,Ocelot会处理匹配到的外部路由请求,未匹配的请求会自动传递给后续的控制器中间件。
修改后的Ocelot.json
{ "Routes": [ { "DownstreamPathTemplate": "/OtherRequest/{everything}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "respondingdomain.com", "Port": 443 } ], "UpstreamPathTemplate": "/OtherRequest/{everything}", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ], "RouteIsCaseSensitive": false } ] }
调整后,/WeatherForecast请求会直接由本地控制器处理,/OtherRequest开头的请求则会被Ocelot转发到指定外部API。
内容的提问来源于stack exchange,提问作者Brokrammer
相关产品推荐
相关产品推荐

