我正在 NET6 Framework 中尝试 Ocelot 包,但不幸的是,当第一个主机不可用时,它不会“尝试”第二个主机。 示例:
"Routes": [
{
"UpstreamPathTemplate": "/api/abc",
"UpstreamHttpMethod": [ "POST" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 2,
"DurationOfBreak": 5000,
"TimeoutValue": 2000
},
"DownstreamPathTemplate": "/api/qwe",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "serverA",
"Port": 2019
},
{
"Host": "serverB",
"Port": 2019
}
],
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
因此,当 ServerA 服务离线时,我确实收到 httpstatus 502“无法建立连接,因为目标计算机主动拒绝它”,但我希望 Ocelot 会自动尝试 ServerB。
我需要在 Ocelot 上配置其他东西才能实现此行为吗?
非常感谢。
你能试试这个吗?
var conf = new OcelotPipelineConfiguration()
{
PreErrorResponderMiddleware = async (ctx, next) =>
{
if (ctx.HttpContext.Request.Path.Equals(new PathString("/")))
{
await ctx.HttpContext.Response.WriteAsync("ok");
}
else
{
await next.Invoke();
}
}
};
app.UseOcelot(conf).Wait();