0

NET Core 5 应用程序。我添加了 docker-file 和运行正常的容器。我在我的应用程序中添加了健康检查。在我的应用程序中,我刚刚添加了 swagger 并添加了 sql 健康检查。下面是我的dockerfile

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source
EXPOSE 80
EXPOSE 443
# copy csproj and restore as distinct layers
COPY *.sln .
COPY ConfigService/*.csproj ./ConfigService/
RUN dotnet restore

# copy everything else and build app
COPY ConfigService/. ./ConfigService/
WORKDIR /source/ConfigService
RUN dotnet publish -c release -o /app --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "ConfigService.dll"]

当我运行这个应用程序时它工作正常,当我打开 https://localhost:32788/swagger/index.html 它工作正常当我打开 https://localhost:32788/hc 这也工作正常但是当我打开https://localhost:32788/hc-ui 它告诉我它显示无法分配请求的地址 (localhost:32788) 下面是我在 appsettings.json 中的配置

 "HealthChecksUI": {
    "HealthChecks": [
      {
        "Name": "Health Check Service",
        "Uri": "https://localhost:32788/hc"
      }
    ],
    "Webhooks": [
      {
        "Name": "",
        "Uri": "",
        "Payload": "",
        "RestoredPayload": ""
      }
    ],
    "EvaluationTimeInSeconds": 10,
    "MinimumSecondsBetweenFailureNotifications": 60
  }

下面是配置方法中的配置

app.UseHealthChecks(path: "/hc", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions()
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });
            app.UseHealthChecksUI(delegate (Options options)
            {
                options.UIPath = "/hc-ui";
            });

我不确定为什么 https://localhost:32788/hc-ui 返回无法分配请求的地址 (localhost:32788)。由于我在 docker 内部运行,因此 Docker 将无法访问它正在运行的端口本身。有人可以帮我理解吗?任何帮助,将不胜感激。谢谢

4

1 回答 1

2

使用来自https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/tree/master/samples/HealthChecks.UIAndApiCustomization的以下配置使其与 Docker 一起使用

努盖特:

<PackageReference Include="AspNetCore.HealthChecks.UI" Version="3.1.3" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="3.1.2" />

启动.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHealthChecksUI().AddInMemoryStorage();
        services.AddHealthChecks();
        ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ... 
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks("/healthz", new HealthCheckOptions
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });

            endpoints.MapHealthChecksUI(setup =>
            {
                setup.UIPath = "/show-health-ui"; // this is ui path in your browser
                setup.ApiPath = "/health-ui-api"; // the UI ( spa app )  use this path to get information from the store ( this is NOT the healthz path, is internal ui api )
            });
            endpoints.MapControllers();
        });
    }

应用设置.json

    "HealthChecksUI": {
      "HealthChecks": [
        {
          "Name": "Http and UI on single project with customizations-1",
          "Uri": "/healthz"
        }
      ],
      "HeaderText": "Caracoles!",
      "Webhooks": [],
      "EvaluationTimeinSeconds": 10,
      "MinimumSecondsBetweenFailureNotifications": 60
    }

在此处输入图像描述

于 2020-12-01T11:22:35.057 回答