代码之家  ›  专栏  ›  技术社区  ›  Ragnar

API网关Ocelot不重定向

  •  0
  • Ragnar  · 技术社区  · 6 年前

    使用Ocelot构建微服务体系结构我开始在一个单独的解决方案中构建测试服务。一切正常,我可以得到 https://localhost:5101/service/stats/collected

    然后在另一个解决方案中,我正在制作一个新的webapi项目。然后我跟着 入门 在奥克洛特 official website .

    配置.json文件以将其用作GW如果我试图 https://localhost:5001/api/stats/collected 我不明白为什么?

    这里是APIGW的主要文件:

    ocelot.json文件

    {
      "ReRoutes": [
        {
          "DownstreamPathTemplate": "/service/stats/collected",
          "DownstreamScheme": "https",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": 5101
            }
          ],
          "UpstreamPathTemplate": "/api/stats/collected"
        }
      ],
      "GlobalConfiguration": {
        "BaseUrl": "https://localhost:5001"
      }
    }
    

    using System.IO;
    using System.Net;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Ocelot.DependencyInjection;
    using Ocelot.Middleware;
    
    namespace APIGateway.Base
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                new WebHostBuilder()
                    .UseKestrel(
                        options =>
                    {
                        options.Listen(IPAddress.Loopback, 5001, listenOptions =>
                        {
                            listenOptions.UseHttps("localhost.pfx", "qwerty123");
                        });
                        options.AddServerHeader = false;
                    }
                        )
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        config
                            .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                            .AddJsonFile("appsettings.json", true, true)
                            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                            .AddJsonFile("ocelot.json")
                            .AddEnvironmentVariables();
                    })
                    .ConfigureServices(s => {
                        s.AddOcelot();
                    })
                    .ConfigureLogging((hostingContext, logging) =>
                    {
                        //add your logging
                    })
                    .UseIISIntegration()
                    .Configure(app =>
                    {
                        app.UseOcelot().Wait();
                    })
                    .Build()
                    .Run();
            }
        }
    }
    

    启动.cs

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace StatsService.Base
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
                app.UseMvc();
            }
        }
    }
    

    更新:

    我通过注释 options UseKestrel 让我的GW工作。 我怎样才能设置这个在我的GW和服务之间有一个安全的连接?本地主机和产品?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Kay Zumbusch    5 年前

    当您使用“localhost”作为主机名时,我猜您使用的是localhost的自签名证书。在这种情况下,您可能需要添加

    "DangerousAcceptAnyServerCertificateValidator": true
    

    到重新路由配置(请参见 https://ocelot.readthedocs.io/en/latest/features/configuration.html#ssl-errors ).

    其他选择是:

    • 使用有效的证书(对于localhost很难实现)
    推荐文章