从单体到微脑:在 .NET 中构建可扩展的 AI 推理

发布: (2026年2月8日 GMT+8 04:00)
12 分钟阅读
原文: Dev.to

Source: Dev.to

请提供您希望翻译的正文内容,我将为您翻译成简体中文并保留原有的格式、Markdown 语法以及技术术语。谢谢!

Source:

AI 推理微服务:从单体到分布式云原生

从单体应用设计转向分布式、云原生架构,是过去十年软件工程中最重要的范式转变之一。但当这种架构转变与 人工智能 的计算密集型特性相碰撞时会怎样?

答案是一个复杂但高度弹性的生态系统——AI 推理微服务

在本指南中,我们将探讨容器化 AI 工作负载并有效编排它们所需的基础理论,使用现代 C# 和 .NET 模式。


为什么要把微服务应用于 AI?

要理解为何要把微服务应用于 AI,必须审视传统软件部署与模型执行之间的固有摩擦。

  • 传统应用 – 为成千上万的并发用户提供静态逻辑。
  • AI 推理服务 – 是无状态的、计算成本高的,且常常需要特定硬件依赖(如 GPU),这些资源稀缺且昂贵。

类比:高端餐厅

单体架构微服务架构
主厨(AI 模型) 试图包揽一切:接单、烹饪、装盘、收拾。如果主厨在订单高峰期不堪重负,整个餐厅就会停摆。如果主厨需要一把专用刀(特定的 GPU 驱动),整个厨房会停下来直到找到刀为止。专职团队 – 专门的炒菜厨师、酱汁厨师和装盘师。炒菜厨师拥有专用炉灶(GPU 节点)。如果炒菜厨师超负荷,我们可以快速再雇佣一位炒菜厨师(水平扩展),而不会影响酱汁厨师的工作。

通过将推理逻辑隔离到独立的容器化服务中,我们可以实现:

  • 故障隔离
  • 硬件专用化
  • 独立可伸缩性

解决 “在我的机器上可以运行” 的问题

AI 模型依赖一条脆弱的依赖链:

  • 操作系统
  • Python 运行时(或 .NET 运行时)
  • 特定库版本(例如 PyTorch、TensorFlow)

Docker 提供了将代码、依赖和系统工具打包成单一不可变产物——容器镜像 的机制。

  • 不可变性 对 AI 至关重要:如果我们更新了某个库,就构建一个新镜像并替换旧镜像,确保生产环境中运行的模型在数学上与实验室中测试的模型完全相同。

编排数百乃至数千个容器

一旦 AI 代理被打包进容器,我们就需要一个编排器——Kubernetes(K8s),来在服务器集群上管理它们。

  • 充当我们容器船只的 港口管理局
  • 如果某个 GPU 节点故障,K8s 会自动把 AI Pod 移动到健康节点。
  • 如果流量激增,它会启动更多 Pod(ReplicaSets)。

.NET 用于推理和编排

虽然 Python 主导模型训练阶段,但 C# 和 .NET 正日益成为推理和编排层的关键技术:

  • 高性能、跨平台
  • 强大的类型系统,适合构建复杂、可靠的分布式系统

微服务的核心原则之一是能够 在不破坏系统的前提下替换实现。我们通过 接口 来定义推理的契约,从而实现这一点。

// The contract defined in the "Domain" layer
public interface IInferenceAgent
{
    Task GenerateResponseAsync(string prompt);
}

// Concrete implementation for a cloud‑based LLM
public class AzureOpenAIAgent : IInferenceAgent { /* ... */ }

// Concrete implementation for a local, containerized model
public class LocalLlamaAgent : IInferenceAgent { /* ... */ }

在容器化环境中,配置是动态的。现代 .NET 的 依赖注入 系统是将这些外部配置与代码连接起来的粘合剂——我们不再使用 new 来实例化代理,而是通过依赖注入请求它。

structor.


使用 IAsyncEnumerable 进行流式推理

AI 推理,尤其是大语言模型(LLMs),本质上是一个流式过程:用户发送提示,模型逐个生成 token。

C# 的 IAsyncEnumerable 使我们能够在 token 生成后立即将其从模型服务流式传输到客户端,从而降低 首次 Token 时间 (TTFT)


实际场景:情感分析服务

设想为全球电商平台构建一个情感分析服务。我们需要 实时 对商品评论进行分类。

  • 在用户浏览器中直接运行这种高强度计算是不可行的。
  • 阻塞主 Web 应用线程同样不可取。

因此,我们部署一个专用的 微服务 来处理推理工作负载。

Code Example: Containerized AI Inference Microservice (ASP.NET Core 8.0)

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;
using System.Text.Json.Serialization;

// 1. Define the Data Contracts (Records are immutable and ideal for DTOs)
public record InferenceRequest([property: JsonPropertyName("text")] string Text);
public record InferenceResult(
    [property: JsonPropertyName("label")] string Label,
    [property: JsonPropertyName("confidence")] double Confidence);

// 2. Define the AI Service Interface
public interface IInferenceService
{
    Task<PredictResult> PredictAsync(string text, CancellationToken cancellationToken);
}

// 3. Implement the AI Service (Simulated for this example)
public class MockInferenceService : IInferenceService
{
    private readonly ILogger _logger;
    private bool _modelLoaded = false;

    public MockInferenceService(ILogger logger) => _logger = logger;

    // Lifecycle method to simulate expensive model loading
    public void Initialize()
    {
        _logger.LogInformation("Loading AI model into memory...");
        Thread.Sleep(2000); // Simulate 2‑second load time
        _modelLoaded = true;
        _logger.LogInformation("AI Model loaded and ready.");
    }

    public async Task<InferenceResult> PredictAsync(string text, CancellationToken cancellationToken)
    {
        if (!_modelLoaded)
        {
            _logger.LogWarning("Model not loaded yet; initializing now.");
            Initialize();
        }

        // Simulated inference logic
        await Task.Delay(500, cancellationToken); // Simulate latency
        var random = new Random();
        var confidence = Math.Round(random.NextDouble(), 2);
        var label = confidence > 0.5 ? "Positive" : "Negative";

        return new InferenceResult(label, confidence);
    }
}

// 4. Register services and configure the minimal API
var builder = WebApplication.CreateBuilder(args);

// Add logging, DI, and the mock inference service as a singleton
builder.Services.AddLogging();
builder.Services.AddSingleton<IInferenceService, MockInferenceService>();

var app = builder.Build();

app.MapPost("/infer", async (InferenceRequest request,
                            IInferenceService inferenceService,
                            HttpContext httpContext) =>
{
    var result = await inferenceService.PredictAsync(request.Text, httpContext.RequestAborted);
    return Results.Json(result);
})
.WithName("Infer")
.Produces(StatusCodes.Status200OK)
.Accepts(contentType: "application/json");

app.Run();

注意: MockInferenceService 仅模拟模型加载和推理过程。在实际生产环境中,你需要用加载真实模型的具体实现来替代它(例如通过 ONNX Runtime、TorchSharp,或调用远程 LLM 接口)。


结束语

通过将 AI 推理逻辑容器化、利用 Kubernetes 进行编排,并采用现代 .NET 模式(DI、接口、IAsyncEnumerable),你可以构建 容错、水平可扩展且硬件感知 的 AI 微服务,使其能够无缝集成到云原生生态系统中。此方法弥合了 AI 对计算资源的高需求与微服务架构在运维上的优雅之间的鸿沟。

lic async Task PredictAsync(string text, CancellationToken cancellationToken)
{
    if (!_modelLoaded) throw new InvalidOperationException("Model not initialized.");

    // Simulate inference latency (GPU/CPU computation)
    await Task.Delay(100, cancellationToken); 

    // Mock Logic: Simple keyword‑based classification
    string label = text.Contains("great", StringComparison.OrdinalIgnoreCase) ||
                   text.Contains("love", StringComparison.OrdinalIgnoreCase)
        ? "Positive"
        : text.Contains("bad", StringComparison.OrdinalIgnoreCase) ||
          text.Contains("hate", StringComparison.OrdinalIgnoreCase)
          ? "Negative"
            : "Neutral";

    double confidence = label == "Neutral" ? 0.65 : 0.95;

    _logger.LogInformation("Inference completed for text: '{Text}' -> {Label}", text, label);
    return new InferenceResult(label, confidence);
}
}

// 4. The Application Entry Point
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // CRITICAL: Register as Singleton. 
        // We want to load the model ONCE and reuse it for all requests.
        builder.Services.AddSingleton<IInferenceService, MockInferenceService>();

        var app = builder.Build();

        // Lifecycle Hook: Initialize the Model before accepting traffic
        var inferenceService = app.Services.GetRequiredService<IInferenceService>();
        if (inferenceService is MockInferenceService mockService)
        {
            mockService.Initialize();
        }

        // Define the API Endpoint
        app.MapPost("/api/inference", async (HttpContext context, IInferenceService inferenceService) =>
        {
            try
            {
                var request = await JsonSerializer.DeserializeAsync<InferenceRequest>(
                    context.Request.Body,
                    cancellationToken: context.RequestAborted);

                if (request is null || string.IsNullOrWhiteSpace(request.Text))
                {
                    context.Response.StatusCode = 400;
                    return;
                }

                var result = await inferenceService.PredictAsync(request.Text, context.RequestAborted);
                context.Response.ContentType = "application/json";
                await JsonSerializer.SerializeAsync(
                    context.Response.Body,
                    result,
                    cancellationToken: context.RequestAborted);
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync($"Internal Server Error: {ex.Message}");
            }
        });

        // Bind to 0.0.0.0 for Docker container compatibility
        app.Run("http://0.0.0.0:8080");
    }
}

关键概念与最佳实践

  • DTOs – 使用 C# record 类型来表示不可变的数据传输对象。
    使用 JsonPropertyName 保持 JSON 的 camel‑case,同时在 C# 中保持 PascalCase。

  • Singleton 生命周期 – 对于持有大型 AI 模型的服务至关重要。
    单例只加载一次模型即可服务成千上万的请求,避免 TransientScoped 生命周期带来的开销。

  • 生命周期初始化 – 在 app.Run() 之前调用 Initialize()
    这可以消除“冷启动”问题,即首次请求因模型加载而超时。

  • 0.0.0.0 绑定 – Docker 容器所必需;绑定到 localhost 会导致容器外部无法访问服务。

  • 处理可变工作负载 – AI 推理具有突发性。
    使用 Kubernetes HPA 监控 CPU/GPU 利用率或每秒请求数(RPS)。当 GPU 使用率 > 80 % 时,HPA 会启动更多 pod(即“厨师”)。

  • 冷启动缓解 – 加载一个 700 亿参数的模型可能需要数分钟。
    解决方案:预热 pod 或保持最小副本数 (minReplicas = 1) 以使模型常驻内存。

  • 常见陷阱

    • Transient 生命周期 – 每个请求重新加载模型会导致内存溢出(OOM)和高延迟。
    • 在处理程序内部的启动逻辑 – 会导致首位用户超时。
    • 阻塞的同步代码Thread.Sleep 会阻塞线程池;应始终使用 async/await
    • 优雅关闭 – 尊重 CancellationToken,以便 Kubernetes 能在不中断进行中的推理的情况下终止 pod。
  • 架构要点 – 将容器化、编排以及现代 C# 模式相结合,可将脆弱的单体应用转变为弹性、云原生的 AI 服务,高效利用昂贵的 GPU 资源。


讨论

Prompts

  1. Cold Starts – 根据你的经验,在生产环境中管理大型模型时,哪一个更难处理:

    • 将模型加载到内存所需的时间,
    • 从镜像仓库拉取容器镜像所需的时间?
  2. API Style Preference – 你更倾向于使用上面展示的 Minimal API 方式,还是坚持使用传统的 MVC 控制器来提供 AI 服务?为什么?


此处展示的概念和代码直接取自电子书 “Cloud‑Native AI & Microservices: Containerizing Agents and Scaling Inference.”(Leanpub,Amazon)中阐述的完整路线图。

0 浏览
Back to Blog

相关文章

阅读更多 »

UX/UI 排版

Typography 是指什么?- 使用哪种字体 - 在什么位置多大 - 多粗 - 行间距 - …