1

我正在尝试在我的网关应用程序中使用 Yarp 来路由我的应用程序。然而,一旦它开始,我得到“路由'0'需要指定主机或路径。将路径设置为'/{**catchall}'以匹配所有请求。”

这是我的 AppSettings 文件:

 "ReverseProxy": {
    "Routes": [
      {
        "SampleService": {
          "ClusterId": "SampleService-cluster1",
          "Match": {
            "Host": "localhost",
            "Path": "sample/{**catchall}"
          }
        },
        "NotificationService": {
          "ClusterId": "NotificationService-cluster",
          "Match": {
            "Host": "localhost",
            "Path": "api/NotificationService/{**catchall}"
          }
        }
      }
    ],
    "Clusters": {
      "SampleService-cluster1": {
        "Destinations": { "SampleService-cluster1/destination1": { "Address": "http://127.0.0.1:6500" } }
      },
      "NotificationService-cluster": {
        "Destinations": { "NotificationService-cluster/destination1": { "Address": "http://*:6020" } }
      }
    }
  }

配置服务:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpClient();
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
                {
                    builder.AllowAnyOrigin()
                           .AllowAnyMethod()
                           .AllowAnyHeader();
                });
            });

            services.AddControllers();

            services.AddTelemetryConsumer<ForwarderTelemetry>();

            services.AddReverseProxy()
                .LoadFromConfig(Configuration.GetSection("ReverseProxy"));
        }

我得到了这个: System.InvalidOperationException

那么知道如何解决这个问题吗?

4

1 回答 1

1

v1.0.0-preview11对配置方式进行了重大更改Routes。您需要更新您的设置。

"ReverseProxy": {
  "Routes": {
    "SampleService": {
      "ClusterId": "SampleService-cluster1",
      "Match": {
        "Host": "localhost",
        "Path": "sample/{**catchall}"
      }
    },
    "NotificationService": {
      "ClusterId": "NotificationService-cluster",
      "Match": {
        "Host": "localhost",
        "Path": "api/NotificationService/{**catchall}"
      }
    }
  },
  "Clusters": {
    "SampleService-cluster1": {
      "Destinations": { "SampleService-cluster1/destination1": { "Address": "http://127.0.0.1:6500" } }
    },
    "NotificationService-cluster": {
      "Destinations": { "NotificationService-cluster/destination1": { "Address": "http://*:6020" } }
    }
  }
}
于 2021-12-25T11:22:15.943 回答