
在云原生应用开发的领域中,可观测性与安全是确保应用稳定运行和数据安全的关键因素。.NET 11 推出的.NET Aspire 框架为开发者提供了将可观测性与安全深度融合的能力,使得云原生应用在复杂的云环境中既能高效运行,又能抵御各种安全威胁。
dotnet add package Microsoft.AspNetCore.App
dotnet add package Microsoft.Extensions.Diagnostics.HealthChecks在 Startup.cs 中配置指标收集:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHealthChecks();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json";
var result = new
{
status = report.Status.ToString(),
checks = report.Entries.Select(entry => new
{
name = entry.Key,
status = entry.Value.Status.ToString(),
description = entry.Value.Description
})
};
await context.Response.WriteAsJsonAsync(result);
}
});
});
}
}- **分布式追踪**:在项目中集成分布式追踪功能。安装 `OpenTelemetry` 相关包:dotnet add package OpenTelemetry.Api
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.Console在 Program.cs 中配置分布式追踪:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using OpenTelemetry;
using OpenTelemetry.Trace;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureServices((hostContext, services) =>
{
services.AddOpenTelemetryTracing(builder =>
{
builder
.AddSource("MyApp")
.SetSampler(new AlwaysOnSampler())
.AddConsoleExporter();
});
});
}
}在控制器中添加追踪逻辑:
using Microsoft.AspNetCore.Mvc;
using OpenTelemetry.Trace;
[ApiController]
[Route("[controller]")]
public class TraceController : ControllerBase
{
private readonly Tracer _tracer;
public TraceController(TracerProvider tracerProvider)
{
_tracer = tracerProvider.GetTracer("MyApp");
}
[HttpGet]
public IActionResult Get()
{
using var span = _tracer.StartActiveSpan("GetRequest");
// 模拟业务逻辑
return Ok("Trace example");
}
}Microsoft.AspNetCore.Authentication.OpenIdConnect 包:dotnet add package Microsoft.AspNetCore.Authentication.OpenIdConnect在 Startup.cs 中配置身份验证和授权:
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddOpenIdConnect(options =>
{
options.Authority = "https://your - identity - server";
options.ClientId = "your - client - id";
options.ClientSecret = "your - client - secret";
options.ResponseType = "code";
options.SaveTokens = true;
});
services.AddAuthorization();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}- **容器与网络安全**:创建 `Dockerfile` 构建容器镜像,并配置网络安全组规则。FROM mcr.microsoft.com/dotnet/aspnet:11.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /src
COPY ["YourProject.csproj", "."]
RUN dotnet restore "./YourProject.csproj"
COPY. /src
WORKDIR "/src/YourProject"
RUN dotnet build "YourProject.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "YourProject.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish.
ENTRYPOINT ["dotnet", "YourProject.dll"]在云平台(如 Azure)中配置网络安全组规则,只允许特定 IP 地址访问容器应用的端口。
.NET Aspire 在.NET 11 中为云原生应用的可观测性与安全提供了深度融合的解决方案。通过深入理解其原理并在实战中合理应用,开发者能够构建出可观测性强、安全可靠的云原生应用。在实践过程中,注意避免可观测性和安全方面的潜在问题,充分发挥.NET Aspire 的优势,满足云原生应用在复杂云环境中的运行需求。
#标签:#.NET 11 #.NET Aspire #云原生应用 #可观测性 #安全融合