我在ocelot中看不到swagger中的微服务端点

问题描述 投票:0回答:1

cs文件代码

using API;
using Confluent.Kafka;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

var producerConfig = new ProducerConfig();
builder.Configuration.GetSection("Kafka").Bind(producerConfig);
builder.Configuration.AddJsonFile("ocelot.json", true, true);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();


builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddSwaggerForOcelot(builder.Configuration);
builder.Services.AddSwaggerGen();

builder.Services.AddSingleton<ProducerConfig>(producerConfig);

var app = builder.Build();
app.UseDeveloperExceptionPage();


app.UseRouting();
app.UseEndpoints(endpoints =>  // Define the endpoint map
{
    endpoints.MapControllers();
});


//app.UseHttpsRedirection();
//app.UseAuthorization();

app.UseSwagger()
   .UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger.API  v1"))
   .UseSwaggerForOcelotUI()
   .UseOcelot().Wait();

app.Run();

这是我的配置方法 这也是我的 docker-compose.yml 文件代码

services:
  gateway-api:
    container_name: gateway-api
    ports:
    - "52791:5000"
    build:
      context: .
      dockerfile: API/Dockerfile
    environment:
      - ASPNETCORE_URLS = "http://*:5000",
      - ASPNETCORE_HTTPS_PORTS = "5050",
      - ASPNETCORE_HTTP_PORTS = "5000"
  points-service:
    container_name: points-service
    depends_on:
    - "gateway-api" 
    ports:
    - "52792:5001"
    environment:
    - ASPNETCORE_URLS="http://*:5001",
    - ASPNETCORE_HTTPS_PORTS="5051",
    - ASPNETCORE_HTTP_PORTS="5001"
    build:
      context: .
      dockerfile: Services/PointsService/PointsService.Api/Dockerfile
And appsettings.json code

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Ocelot": "Debug" 
    }
  },
  "Kafka": {
    "BootstrapServers": "broker.local:19092",
    "SecurityProtocol": "Ssl",
    "SslCaLocation": "ssl/ca-cert.pem",
    "SslCertificateLocation": "ssl/client-cert.pem",
    "SslKeyLocation": "ssl/client-key.pem",
    "SslKeyPassword": "123456"
  },
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:5000"
      }
    }
  },
  "AllowedHosts": "*"
}

这里还有 ocelot.json 文件

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "points-service",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/api/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "SwaggerKey": "points"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://gateway-api:5000"
  },
  "SwaggerEndPoints": [
    {
      "Key": "points",
      "Config": [
        {
          "Name": "Points API",
          "Version": "v1",
          "Url": "http://points-service:5001/swagger/v1/swagger.json"
        }
      ]
    }
  ]
}

我可以调用 gateway-api 端点,而不会出现任何将 ocelot 重定向到微服务的问题。 但是 swagger 没有看到服务端点,我检查了日志并看到了这个警告。

warn: Ocelot.Responder.Middleware.ResponderMiddleware[0]
2024-10-30 21:04:21 requestId: 0HN7P2MKOVR6R:00000001, previousRequestId: No PreviousRequestId, message: 'Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /, verb: GET. errors found in ResponderMiddleware. Setting error response for request path:/, request method: GET'

我也从容器运行这个命令,它工作正常。 卷曲 http://points-service:5001/swagger/v1/swagger.json 请帮我解决这个问题,我是豹猫新手,不明白我做错了什么。

c# asp.net-core swagger ocelot
1个回答
0
投票

已解决问题 使用Swagger() .UseSwaggerUI() 不能一起使用。 使用SwaggerForOcelotUI()。 他们互相覆盖。

© www.soinside.com 2019 - 2024. All rights reserved.