在反向代理中同时访问两个目的地时遇到问题

问题描述 投票:0回答:1

我有两个控制器,我想同时点击两个控制器的API,但我的代码只能随机点击一次。

我怎样才能实现这个目标?

茶控制器

[Route("api/[controller]")]
[ApiController]
public class TeaController : ControllerBase
{
    [HttpGet("Test")]
    public IActionResult Test()
    {
       return Ok("Tea");
    }
}

饼干控制器

  [Route("api/[controller]")]
    [ApiController]
    public class BiscuitController : ControllerBase
    {
        [HttpGet("Test")]
        public IActionResult Test()
        {
            return Ok("Biscuit");
        }
    }
}

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "UseCodeBasedConfig": "true",
  "ReverseProxy": {
    "Routes": {
      "ReverseProxy-route": {
        "ClusterId": "ReverseProxy-cluster",
        "Match": {
          "Path": "ReverseProxy/{**catch-all}"
        },
        "Transforms": [
          {
            "PathPattern": "{**catch-all}"
          }

        ]
      }

    },
    "Clusters": {
      "ReverseProxy-cluster": {
        "Destinations": {
          "LoadBalancingPolicy": "Random",
          "ReverseProxy-cluster/destination1": {
            "Address": "https://localhost:7044/api/biscuit"
          },
          "ReverseProxy-cluster/destination2": {
            "Address": "https://localhost:7044/api/tea"
          }
        }
      }
    }
  }
}

program.cs

var builder = WebApplication.CreateBuilder(args);

var proxyBuilder = builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapReverseProxy();
});


app.Run();
c# .net asp.net-core reverse-proxy
1个回答
0
投票

您将

LoadBalancingPolicy
指定为
Random
,两个控制器都是两个目的地之一。
Random
不出所料:

随机选择目的地。

对于给定的内置策略,似乎没有办法“同时打击两者”。我建议您查看一下政策,看看哪些政策适合您的需求。

© www.soinside.com 2019 - 2024. All rights reserved.