解決方案: 版本較高的Ocelot中,配置路由使用的應該是Routes,而不是ReRoutes。具體配置可以繼續閱讀下文
在學習使用Ocelot的時候,跟着學習的步驟一步步走過來,(沒有看官方文檔),在調用的時候一直出現404。(ocelot使用的版本是16.0 環境是 .netCore3.1)
具體代碼如下:
//program配置 public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(c => { c.AddJsonFile("configuration.json", optional: false, reloadOnChange: true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } //startUp配置 public class Startup { public Startup(IConfiguration configuration) { } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOcelot(); /// services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //使用新的請求處理管道 app.UseOcelot(); //刪除舊的請求處理管道 //if (env.IsDevelopment()) //{ // app.UseDeveloperExceptionPage(); //} //app.UseHttpsRedirection(); //app.UseRouting(); //app.UseAuthorization(); //app.UseEndpoints(endpoints => //{ // endpoints.MapControllers(); //}); } } //configuration.json 文件內容 { "ReRoutes": [ { "DownstreamPathTemplate": "/api/{url}", //服務地址--url爲變量 "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5859 } ], "UpstreamPathTemplate": "/T5859/{url}", //網關地址--url爲變量 "UpstreamHttpMethod": [ "Get", "Post" ] //, //"GlobalConfiguration": { // "BaseUrl": "http://localhost:1600" //} } ] }
調用具體地址出現如下圖結果:
後面被迫去查看官網的文檔,看了半天也沒看出問題。最後逐字查看,查到了原因。 版本較高的Ocelot中,配置路由使用的應該是Routes,而不是ReRoutes
具體修改代碼如下:
//configuration.json 文件內容
{
"Routes": [
{
"DownstreamPathTemplate": "/api/{url}", //服務地址--url爲變量
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5859
}
],
"UpstreamPathTemplate": "/T5859/{url}", //網關地址--url爲變量
"UpstreamHttpMethod": [ "Get", "Post" ] //,
//"GlobalConfiguration": {
// "BaseUrl": "http://localhost:1600"
//}
}
]
}
修改了此名稱之後,就可以正常使用了。所以調用第三方的插件,還是要看文檔啊!!!