Akka.NET と .NET Aspire の構成
このスキルを使用するタイミング
以下の場合にこのスキルを使用します:
- .NET Aspire オーケストレーションを使用した新しい Akka.NET プロジェクトのセットアップ
- クラスターブートストラップとディスカバリーを使用した Akka.Cluster の構成
- SQL Server を使用した Akka.Persistence の統合
- クラスター管理のための Akka.Management のセットアップ
- ローカル開発におけるマルチレプリカアクターシステムの構成
- Aspire を使用した Kubernetes への Akka.NET アプリケーションのデプロイ
関連スキル
akka-net-management- Akka.Management、Cluster Bootstrap、およびディスカバリープロバイダー(Kubernetes、Azure、Config)の詳細microsoft-extensions-configuration- 構成検証のための IValidateOptions パターンakka-net-best-practices- テスト可能なアクターシステムのためのクラスター/ローカルモードの抽象化aspire-integration-testing- 実際のインフラストラクチャを使用した Aspire アプリケーションのテスト
基本原則
- Microsoft.Extensions.Configuration による設定 - appsettings.json からバインドされた強く型付けされた設定クラスを使用します(
microsoft-extensions-configurationスキルを参照) - DI 統合のための Akka.Hosting - シームレスな ASP.NET Core 統合のために Akka.Hosting ライブラリを使用します
- オーケストレーションのための Aspire - Aspire にサービス依存関係、ネットワーキング、環境構成を管理させます
- ヘルスチェック - クラスタリング、永続化、準備状態に対して常にヘルスチェックを構成します
- 関心事の分離 - アクター定義、構成、および Aspire オーケストレーションを別々のレイヤーに保持します
- 起動時に構成を検証 -
IValidateOptions<T>と.ValidateOnStart()を使用して、構成ミスを早期に検出します
プロジェクト構造
YourSolution/
├── src/
│ ├── YourApp.Actors/ # アクター定義とビジネスロジック
│ │ ├── YourActor.cs
│ │ └── YourApp.Actors.csproj
│ ├── YourApp/ # ASP.NET Core Web アプリケーション
│ │ ├── Config/
│ │ │ ├── AkkaConfiguration.cs # Akka セットアップ拡張メソッド
│ │ │ └── AkkaSettings.cs # 構成モデル
│ │ ├── Program.cs
│ │ ├── appsettings.json
│ │ └── YourApp.csproj
│ └── YourApp.AppHost/ # Aspire オーケストレーション
│ ├── Program.cs
│ ├── AkkaManagementExtensions.cs
│ └── YourApp.AppHost.csproj
必要な NuGet パッケージ
アクタープロジェクト用 (YourApp.Actors.csproj)
<ItemGroup>
<PackageReference Include="Akka.Cluster.Hosting" />
<PackageReference Include="Akka.Streams" />
</ItemGroup>
Web アプリケーション用 (YourApp.csproj)
<ItemGroup>
<PackageReference Include="Akka.Hosting" />
<PackageReference Include="Akka.Cluster.Hosting" />
<PackageReference Include="Akka.Persistence.Sql.Hosting" />
<PackageReference Include="Akka.Management" />
<PackageReference Include="Akka.Management.Cluster.Bootstrap" />
<PackageReference Include="Akka.Discovery.KubernetesApi" />
<PackageReference Include="Akka.Discovery.Azure" />
<PackageReference Include="Akka.Discovery.Config.Hosting" />
<PackageReference Include="Petabridge.Cmd.Host" />
<PackageReference Include="Petabridge.Cmd.Cluster" />
</ItemGroup>
AppHost 用 (YourApp.AppHost.csproj)
<Sdk Name="Aspire.AppHost.Sdk" Version="$(AspireVersion)" />
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
<PackageReference Include="Aspire.Hosting.Azure.Storage" />
<PackageReference Include="Aspire.Hosting.SqlServer" />
</ItemGroup>
構成モデル (AkkaSettings.cs)
強く型付けされた構成クラスを作成します:
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Akka.Cluster.Hosting;
using Akka.Remote.Hosting;
using Petabridge.Cmd.Host;
namespace YourApp.Config;
public class AkkaSettings
{
public string ActorSystemName { get; set; } = "YourSystem";
public bool LogConfigOnStart { get; set; } = false;
public RemoteOptions RemoteOptions { get; set; } = new()
{
PublicHostName = Dns.GetHostName(),
HostName = "0.0.0.0",
Port = 8081
};
public ClusterOptions ClusterOptions { get; set; } = new()
{
SeedNodes = [$