ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Aspire Azure Data Explorer(Kusto)托管集成实战指南:建模、模拟器与连接配置

Aspire Azure Data Explorer(Kusto)托管集成实战指南:建模、模拟器与连接配置 Aspire Azure Data ExplorerKusto托管集成实战指南建模、模拟器与连接配置【免费下载链接】aspireAspire is the tool for code-first, extensible, observable dev and deploy.项目地址: https://gitcode.com/GitHub_Trending/as/aspire本文基于Aspire.Hosting.Azure.Kusto集成包讲解如何在 .NET Aspire 解决方案中建模、配置并编排 Azure Data ExplorerKusto资源从AddAzureKustoCluster集群建模、AddReadWriteDatabase数据库挂载到RunAsEmulator本地 Kustainer 容器模拟以及WithReference消费侧连接属性的注入规则。读者完成后将掌握 C# 与 TypeScriptpolyglot AppHost两种写法理解连接字符串的生成机制、健康检查与数据库初始化流程并能直接在本仓库的 示例源码 与 测试用例 中验证每个行为。集成包概览与适用场景Azure Data Explorer简称 ADX / Kusto是一种快速、可扩展的数据探索与遥测分析服务。在 Aspire 中Aspire.Hosting.Azure.Kusto集成允许你把 Kusto 集群cluster与读写数据库read-write database作为一等资源加入应用模型application model实现建模以代码方式声明 Kusto 集群及其子数据库无需手写 Azure 资源描述。配置本地开发时以容器模拟器Kustainer运行无需真实 Azure 订阅发布时自动生成 Azure 预置provisioning逻辑。编排通过WithReference向消费项目注入连接属性并自动注册健康检查与生命周期管理。可观测资源在 Dashboard 中展示并提供“在 Kusto Explorer 中打开”等快捷命令。该集成属于 Aspire 的 Azure Hosting 家族位于 src/Aspire.Hosting.Azure.Kusto其公开 API 契约维护在 api/Aspire.Hosting.Azure.Kusto.cs仓库内还配套了完整的单元测试目录 tests/Aspire.Hosting.Azure.Kusto.Tests 与 polyglot AppHost 示例 tests/PolyglotAppHosts/Aspire.Hosting.Azure.Kusto。快速开始安装集成在 AppHost 项目目录下使用 Aspire CLI 添加集成包aspire add Aspire.Hosting.Azure.Kusto该命令会把Aspire.Hosting.Azure.Kusto包引用写入 AppHost 项目项目清单见 Aspire.Hosting.Azure.Kusto.csproj之后即可在Program.cs中使用命名空间Aspire.Hosting下的扩展方法。使用示例C# 与 TypeScriptC# AppHost在 AppHost 中创建一个 Kusto 集群资源声明一个读写数据库并让另一个资源引用它var db builder.AddAzureKustoCluster(kusto) .RunAsEmulator() .AddReadWriteDatabase(mydb); var myService builder.AddProjectProjects.MyService() .WithReference(db);链路说明AddAzureKustoCluster(kusto)创建集群资源资源名同时作为引用时的连接名connection name。.RunAsEmulator()让该集群在本地以 Kustainer 容器运行仅开发/运行模式生效发布模式自动跳过见下文。.AddReadWriteDatabase(mydb)在集群下挂载一个读写数据库数据库名默认与资源名一致。.WithReference(db)把连接属性以环境变量形式注入MyService项目。TypeScript AppHostAspire 支持 polyglot AppHostTypeScript 等语言编写应用宿主API 与 C# 一一对应。仓库中 tests/PolyglotAppHosts/Aspire.Hosting.Azure.Kusto/TypeScript/apphost.mts 给出了完整示例const kusto await builder.addAzureKustoCluster(kusto).runAsEmulator({ configureContainer: async (emulator) { await emulator.withHostPort(8088); } }); const defaultDatabase await kusto.addReadWriteDatabase(samples); const customDatabase await kusto.addReadWriteDatabase(analytics, { databaseName: AnalyticsDb }); await defaultDatabase.withCreationScript(.create database Samples ifnotexists); await customDatabase.withCreationScript(.create database AnalyticsDb ifnotexists);可以看到 TypeScript 版同样支持runAsEmulator、addReadWriteDatabase第二个参数可显式指定databaseName与withCreationScript并可通过configureContainer回调进一步定制容器例如withHostPort(8088)指定宿主端口。Java 版示例位于 tests/PolyglotAppHosts/Aspire.Hosting.Azure.Kusto/Java/AppHost.java。连接属性Connection Properties使用WithReference引用 Kusto 资源时Aspire 会把以下连接属性以环境变量形式提供给消费项目。集群资源Cluster Resource属性名描述Uri集群端点 URI通常形如https://cluster-name.region.kusto.windows.net/使用模拟器时则为 HTTP 端点数据库资源Database Resource属性名描述Uri集群端点 URI继承自父集群DatabaseName数据库名称每个属性以[RESOURCE]_[PROPERTY]的命名规则暴露为环境变量。例如名为mydb的资源其Uri属性对应MYDB_URIDatabaseName属性对应MYDB_DATABASENAME。源码级印证这一行为的实现依据位于资源类的IResourceWithConnectionString.GetConnectionProperties()方法集群资源 AzureKustoClusterResource.cs 中 yield 返回一对(Uri, UriExpression)其中UriExpression在模拟器模式下解析为容器的http端点在 Azure 模式下解析为预置输出的clusterUri数据库资源 AzureKustoReadWriteDatabaseResource.cs 先转发父集群的全部属性即Uri再追加(DatabaseName, DatabaseName)。对应测试见 tests/Aspire.Hosting.Azure.Kusto.Tests/AzureKustoConnectionPropertiesTests.cs其中分别验证了集群、数据库、模拟器模式下连接属性的组合与命名。连接字符串数据库资源的ConnectionStringExpression由父集群连接字符串拼接InitialCatalogDatabaseName生成Kusto SDK 的KustoConnectionStringBuilder语法例如https://cluster-endpoint/;Initial Catalogmydb集群自身的连接字符串在模拟器模式下即容器http端点如http://localhost:8080在 Azure 模式下为clusterUri预置输出。模拟器模式RunAsEmulator 深入解析RunAsEmulator让 Kusto 资源以本地容器Kustainer形式运行适合开发与集成测试。行为细节来自源码AzureKustoBuilderExtensions.cs 中RunAsEmulator的实现要点发布模式短路若builder.ApplicationBuilder.ExecutionContext.IsPublishMode为真直接返回不会创建任何容器配置发布时改用 Azure 预置。打标通过WithAnnotation(new EmulatorResourceAnnotation())标记为模拟器资源便于工具链识别。HTTP 端点为集群资源添加名为http的 HTTP 端点目标端口为容器内 8080AzureKustoEmulatorContainerDefaults.DefaultTargetPort连接字符串逻辑据此识别模拟器模式。容器替身创建AzureKustoEmulatorResource包装原集群资源见 AzureKustoEmulatorResource.cs并配置镜像为mcr.microsoft.com/azuredataexplorer/kustainer-linux默认 tag 见 AzureKustoEmulatorContainerImageTags.cs同时设置环境变量ACCEPT_EULAY。可定制configureContainer回调可进一步配置容器例如挂载卷、绑定目录、覆盖镜像等。常用容器定制WithHostPort(int port)设置宿主端口对应http端点。TypeScript 示例中即用withHostPort(8088)把模拟器暴露到宿主 8088 端口。WithVolume/WithBindMount持久化数据库数据。模拟器默认持久化路径为容器内/kustodata/dbs/见 AzureKustoEmulatorContainerDefaults.cs挂载该路径可在容器重启后保留数据。WithImage覆盖默认的 Kustainer 镜像与 tag。以上定制行为均有对应测试覆盖见 tests/Aspire.Hosting.Azure.Kusto.Tests/AddAzureKustoTests.cs如RunAsEmulator_WithVolume_ShouldConfigureVolumeAnnotation、RunAsEmulator_WithCustomImage_ShouldUseSpecifiedValues、RunAsEmulator_ShouldConfigureHttpEndpoint等。集群健康检查集群资源注册了健康检查AzureKustoHealthCheckAzureKustoHealthCheck.cs对集群执行控制命令.show version有返回即视为健康超时设置 30 秒。消费资源若使用WaitFor会等待 Kusto 可用后再启动。数据库初始化与 WithCreationScript模拟器模式下集群就绪后OnResourceReady会自动为每个子数据库执行建库脚本AzureKustoBuilderExtensions.cs 中AddKustoHealthChecksAndLifecycleManagement默认脚本由 AzureKustoEmulatorContainerDefaults.cs 的DefaultCreateDatabaseCommand生成等价于.create database DB_NAME persist (/kustodata/dbs/DB_NAME/md, /kustodata/dbs/DB_NAME/data) ifnotexists自定义脚本通过WithCreationScript(string script)传入 KQL 脚本可创建表、导入数据等脚本以AzureKustoCreateDatabaseScriptAnnotation注解存储AzureKustoCreateDatabaseScriptAnnotation.cs多个脚本时取最后一个。TypeScript 示例中withCreationScript(.create database Samples ifnotexists)即为此用法。仅模拟器生效源码中明确自动建库只在!server.IsEmulator为假即模拟器模式时执行Azure 模式下数据库由预置流程创建因此自定义脚本不会执行。生产环境请通过预置provisioning管理 schema。韧性重试建库命令通过AzureKustoEmulatorResiliencePipelines.Default执行AzureKustoEmulatorResiliencePipelines.cs最多重试 10 次、指数退避、仅对非永久性异常ICloudPlatformException.IsPermanent false重试应对模拟器冷启动期间连接未就绪的情况。对应的初始化逻辑测试见 tests/Aspire.Hosting.Azure.Kusto.Tests/AddAzureKustoTests.cs如AddReadWriteDatabase_WithCreationScript_ShouldApplyScript系列测试。数据库健康检查每个数据库资源还会注册数据库级健康检查对数据库执行 KQL 查询print message Hello, World!能读到结果即健康。这样WaitFor(db)可精确等待“数据库已可用”而不仅是集群存活。发布到 Azure预置Provisioning行为当RunAsEmulator未调用或处于发布模式时AddAzureKustoCluster会为集群配置 Azure 预置AzureKustoBuilderExtensions.cs 中configureInfrastructure回调使用KustoCluster预置资源默认 SKU 为Standard_E2a_v4、容量 2KustoSkuName.StandardE2aV4KustoSkuTier.StandardCapacity 2并打上aspire-resource-name标签。输出clusterUri集群 URI与name两个预置输出ProvisioningOutput供连接字符串与角色分配引用。集群下每个数据库转换为KustoReadWriteDatabase预置实体并挂到集群下。若指定了已有资源existing resource注解则复用已存在的KustoCluster而非新建见 AzureKustoClusterResource.cs 的AddAsExistingResource。角色分配引用数据库的消费主体如托管身份默认被授予数据库User角色KustoDatabasePrincipalRole.User通过KustoDatabasePrincipalAssignment完成见 AzureKustoClusterResource.cs 的AddRoleAssignments并在 AzureKustoBuilderExtensions.cs 的 XML 注释中说明。非模拟器模式下健康检查与查询使用DefaultAzureCredential认证GetConnectionStringBuilder中的WithAadAzureTokenCredentialsAuthentication。Dashboard 内的快捷操作集群资源通过AddKustoCustomCommands注册了两个自定义命令AzureKustoBuilderExtensions.csOpen in Kusto Explorer (Desktop)调用KustoClientToolLauncher启动桌面版 Kusto Explorer。仅 Windows 且资源处于 Running 状态时显示/可用。Open in Kusto Explorer (Web)调用KustoClientToolLauncher启动 Web 版 Kusto Explorer。模拟器模式下隐藏Web Explorer 只自动连接受信域模拟器端点不在其中启动失败时通过交互服务弹出含连接字符串的 Markdown 提示供手动打开。这使得运维人员无需离开 Dashboard 即可进入查询工具检查数据。消费侧接入示例在消费项目如 API 服务中读取注入的环境变量即可构造 Kusto 客户端var uri Environment.GetEnvironmentVariable(MYDB_URI); var databaseName Environment.GetEnvironmentVariable(MYDB_DATABASENAME); // 使用 Kusto.Data SDK 构建 KustoConnectionStringBuilder 并执行查询注意环境变量前缀由资源名大写化而来资源名mydb→ 前缀MYDB_。若在 AppHost 中为数据库显式指定了与资源名不同的databaseName注入的DatabaseName属性以databaseName参数为准TypeScript 示例中addReadWriteDatabase(analytics, { databaseName: AnalyticsDb })即产生ANALYTICS_DATABASENAMEAnalyticsDb的效果。小结Aspire.Hosting.Azure.Kusto让 Kusto 集群与数据库成为 Aspire 应用模型中的一等公民AddAzureKustoClusterAddReadWriteDatabase完成建模RunAsEmulator在开发环境提供开箱即用的 Kustainer 容器与自动建库、健康检查、韧性重试WithReference按[RESOURCE]_[PROPERTY]规则注入Uri/DatabaseName连接属性发布时则自动生成 Azure 预置与数据库User角色分配。无论使用 C# 还是 TypeScript AppHostAPI 形态保持一致。想深入了解实现细节可继续阅读集成入口与扩展方法AzureKustoBuilderExtensions.cs集群资源模型AzureKustoClusterResource.cs数据库资源模型AzureKustoReadWriteDatabaseResource.cs模拟器与持久化默认值AzureKustoEmulatorContainerDefaults.cs、AzureKustoEmulatorContainerImageTags.cs行为测试tests/Aspire.Hosting.Azure.Kusto.Tests/AddAzureKustoTests.cs、tests/Aspire.Hosting.Azure.Kusto.Tests/AzureKustoConnectionPropertiesTests.cspolyglot 示例tests/PolyglotAppHosts/Aspire.Hosting.Azure.Kusto/TypeScript/apphost.mts【免费下载链接】aspireAspire is the tool for code-first, extensible, observable dev and deploy.项目地址: https://gitcode.com/GitHub_Trending/as/aspire创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表