我有两个控制器,我想同时点击两个控制器的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();
LoadBalancingPolicy
指定为 Random
,两个控制器都是两个目的地之一。 Random
不出所料:
随机选择目的地。
对于给定的内置策略,似乎没有办法“同时打击两者”。我建议您查看一下政策,看看哪些政策适合您的需求。