Ocelot 16.0 无法匹配路由:404

  解决方案: 版本较高的Ocelot中,配置路由使用的应该是Routes,而不是ReRoutes。具体配置可以继续阅读下文

 

   在学习使用Ocelot的时候,跟着学习的步骤一步步走过来,(没有看官方文档),在调用的时候一直出现404。(ocelot使用的版本是16.0 环境是 .netCore3.1)

具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//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

具体修改代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//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"
      //}
    }
  ]
}

 修改了此名称之后,就可以正常使用了。所以调用第三方的插件,还是要看文档啊!!!

posted @   keke..lele  阅读(613)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示