首页 > web前端 > js教程 > 正文

.NET Aspire 多语言

PHPz
发布: 2024-09-08 22:34:12
原创
327 人浏览过

在本文中,我想探讨如何利用 .NET Aspire 等工具来改进我们构建和部署分布式应用程序的方式,即使我们使用 .NET 以外的语言和框架也是如此。

什么是 .NET Aspire

.NET Aspire 是一个固执己见的、云就绪堆栈,旨在改善构建可观察、生产就绪的分布式应用程序的体验。它是通过处理特定云原生问题的 NuGet 包集合来交付的。

.NET Aspire 旨在帮助您:

  • 编排:.NET Aspire 提供了用于运行和连接多项目应用程序及其本地开发环境依赖项的功能。
  • 集成:.NET Aspire 集成是适用于常用服务(例如 Redis 或 Postgres)的 NuGet 包,具有​​标准化接口,确保它们与您的应用程序一致且无缝地连接。
  • 工具:.NET Aspire 附带适用于 Visual Studio、Visual Studio Code 和 dotnet CLI 的项目模板和工具体验,可帮助您创建 .NET Aspire 项目并与之交互。

.NET Aspire 的核心可以在您从 .NET 模板开始使用 Aspire 时创建的 AppHost 项目中找到。 AppHost 项目定义了我们的分布式应用程序的组件,从服务(例如后端或前端)到资源(例如数据库或缓存)。
该项目可以生成一个清单,从基础设施的角度描述我们的应用程序。因此,Azure 开发人员 CLI 可以解释它以将应用程序部署到 Azure,而无需编写任何基础设施即代码。

使用不同的语言

当然,当我们使用分布式应用程序和微服务时,我们可能会遇到不同团队喜欢用不同语言编写代码的情况。当然,.NET Aspire 属于 .NET 生态系统。尽管如此,它的设计是能够与不同的语言和框架集成。

分布式计算器

让我们考虑一个简单的例子。我们有一个分布式计算器,它由不同的服务组成:前端服务和每个操作的后端。

  • 前置:反应
  • 减法:dotnet
  • 添加:去
  • 乘法:python
  • 部门:nodejs

我们还有一个 redis 缓存来存储计算器的状态。

.NET Aspire Multilanguage

在这种情况下,我们可以使用 .NET Aspire 来构建分布式计算器,利用它附带的所有工具和集成。 Aspire 为某些语言提供本机支持,但我们也可以使用容器扩展它以支持其他语言。

在撰写本文时,.NET Aspire 支持以下语言:

  • 多种 JS 框架(React、Angular、Vue、NodeJS)
  • Python

以下是我们如何配置 AppHost 项目中的所有后端服务:

戈兰

Golang 本身不支持,因此我们将其添加为容器。请注意,我们可以决定在运行 AppHost 来发布清单或进行本地开发的场景中使用不同的图像。

// Configure Adder in Go
var add = (builder.ExecutionContext.IsPublishMode
    ? builder.AddContainer("addapp", "acrt6xtihl2b3uxe.azurecr.io/addapp")
    : builder.AddContainer("addapp", "addapp"))
        .WithHttpEndpoint(targetPort: 6000, env: "APP_PORT", name: "http")
        .WithOtlpExporter()
        .WithEnvironment("OTEL_SERVICE_NAME", "addapp")
        .PublishAsContainer();
var addEnpoint = add.GetEndpoint("http");
登录后复制

Python

原生支持Python,因此我们可以使用AddPythonProject方法来配置乘数服务。请按照本教程正确配置Python项目。

// Configure Multiplier in Python
var multiply = builder.AddPythonProject("multiplyapp", "../../python-multiplier", "app.py")
    .WithHttpEndpoint(targetPort: 5001, env: "APP_PORT", name: "http")
    .WithEnvironment("OTEL_SERVICE_NAME", "multiplyapp")
    .PublishAsDockerFile();
登录后复制

NodeJS

原生支持NodeJS,因此我们可以使用AddNodeApp方法来配置divider服务。

// Configure Divider in NodeJS
var divide = builder.AddNodeApp(name: "divideapp", scriptPath: "app.js", workingDirectory: "../../node-divider")
    .WithHttpEndpoint(targetPort: 4000, env: "APP_PORT", name: "http")
    .WithEnvironment("OTEL_SERVICE_NAME", "divideapp")
    .PublishAsDockerFile();
登录后复制

。网

这并不奇怪,.NET Aspire 原生支持 .NET,因此我们可以使用 AddProject 方法来配置减法器服务。

// Configure Subtractor in .NET
var subtract = builder.AddProject<Projects.dotnet_subtractor>("subtractapp")
    .WithReference(insights)
    .WithEnvironment("OTEL_SERVICE_NAME", "subtractapp");
登录后复制

雷迪斯

可以使用 AddRedis 方法轻松配置 Redis 缓存,或者在本场景中通过 AddDaprStateStore 方法使用 Dapr。

// Configure Dapr State Store
var stateStore = builder.AddDaprStateStore("statestore");
登录后复制

Dapr不是本文的重点,但值得一提的是.NET Aspire可以与Dapr结合使用来构建分布式应用程序。有关 Dapr 的更多信息,请参考官方文档。

反应

最后,我们可以使用 AddNpmApp 方法配置前端服务。

// Configure Frontend in React
builder.AddNpmApp(name: "calculator-front-end", workingDirectory: "../../react-calculator")
    .WithDaprSidecar(new DaprSidecarOptions
    {
        AppPort = 3000,
        AppProtocol = "http",
        DaprHttpPort = 3500
    })
    .WithEnvironment("DAPR_HTTP_PORT", "3500")
    .WithReference(addEnpoint)
    .WithReference(multiply)
    .WithReference(divide)
    .WithReference(subtract)
    .WithReference(stateStore)
    .WithReference(insights)
    .WithHttpEndpoint(targetPort: 3000, env: "PORT")
    .WithExternalHttpEndpoints()
    .WithEnvironment("OTEL_SERVICE_NAME", "calculator-front-end")
    .PublishAsDockerFile();
登录后复制

由于我们引用了所有先前配置的服务,因此我们可以轻松地将它们连接到前端服务。当我们需要从前端调用加法器时,我们可以使用 Aspire 注入的环境变量轻松实现:

app.post('/calculate/add', async (req, res) => {
  try {
      const serviceUrl = process.env.services__addapp__http__0;

      const appResponse = await axios.post(`${serviceUrl}/add`, req.body);

      // Return expected string result to client
      return res.send(`${appResponse.data}`); 
  } catch (err) {
      console.log(err);
  }
});
登录后复制

Further considerations

The entire application can be deployed to Azure using the Azure Developer CLI. The CLI will read the manifest generated by the AppHost project and deploy the application to Azure, creating all the necessary resources. To learn how to integrate Aspire with the Azure Developer CLI, please reference the official tutorial.

All the code for the distributed calculator can be found in the Aspire Multilanguage repository.

以上是.NET Aspire 多语言的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!