Orleans 2.0官方文档(闫辉的个人翻译)——5.5.4 服务端配置

注意

如果要启动本地silo和本地客户端以进行开发,请查看“ 本地开发配置” 章节

服务端配置

silo是通过SiloHostBuilder和一些补充选项类,以编程方式来配置的。Orleans中的选项类遵循ASP.NET选项模式,可以通过文件,环境变量等加载。

silo的配置有几个关键方面:

  • Orleans集群信息
  • 集群提供者
  • 用于silo-to-silo和client-to-silo通信的端点
  • 应用部分

这是一个silo配置示例,它定义集群信息,使用Azure集群并配置应用程序部分:

var silo = new SiloHostBuilder()
    // Clustering information
    .Configure<ClusterOptions>(options =>
    {
        options.ClusterId = "my-first-cluster";
        options.ServiceId = "MyAwesomeOrleansService";
    })
    // Clustering provider
    .UseAzureStorageClustering(options => options.ConnectionString = connectionString)
    // Endpoints
    .ConfigureEndpoints(siloPort: 11111, gatewayPort: 30000)
    // Application parts: just reference one of the grain implementations that we use
    .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ValueGrain).Assembly).WithReferences())
    // Now create the silo!
    .Build();

我们将分析此示例中使用的步骤:

Orleans集群信息

    [...]
    // Clustering information
    .Configure<ClusterOptions>(options =>
    {
        options.ClusterId = "orleans-docker";
        options.ServiceId = "AspNetSampleApp";
    })
    [...]

这里我们做两件事:

  • ClusterId设置为"orleans-docker":这是Orleans集群的唯一ID。所有使用此ID的客户端和silo都将能够直接相互通信。不过,您可以选择为不同的部署使用不同的ClusterId
  • ServiceId设置为"AspNetSampleApp":这是应用程序的唯一ID,将由某些提供程序(如持久化提供程序)使用。此ID应保持稳定,并且不会在部署中更改

集群提供程序

    [...]
    // Clustering provider
    .UseAzureStorageClustering(options => options.ConnectionString = connectionString)
    [...]

构建在Orleans上的服务,通常部署在有一组节点的集群上,集群可以在专用硬件上,也可以在Azure中。对于开发和基本的测试,Orleans可以部署在单个节点中。当部署到有一组节点的集群时,Orleans在内部实现了一组协议,来发现和维护集群中Orleans silo的成员身份,包括检测节点故障和自动重​​新配置。

为了可靠地管理集群成员资格,Orleans使用Azure Table,SQL Server或Apache ZooKeeper进行节点同步。

在此示例中,我们使用Azure Table作为成员身份提供程序。

端点

var silo = new SiloHostBuilder()
    [...]
    // Endpoints
    .ConfigureEndpoints(siloPort: 11111, gatewayPort: 30000)
    [...]

Orleans silo有两种典型类型的端点配置:

  • silo-to-silo端点,用于同一集群中的silo之间的通信
  • client-to-silo端点(或网关),用于同一集群中的客户端和silo之间的通信

在示例中,我们使用帮助方法.ConfigureEndpoints(siloPort: 11111, gatewayPort: 30000),将用于silo-to-silo通信的端口设置为11111和用于网关的端口设置为30000。此方法将探测要侦听哪个接口。

在大多数情况下,此方法应该足够了,但如果需要,可以进一步自定义。以下是如何使用外部IP地址与某些端口转发的示例:

[...]
.Configure<EndpointOptions>(options =>
{
    // Port to use for Silo-to-Silo
    options.SiloPort = 11111;
    // Port to use for the gateway
    options.GatewayPort = 30000;
    // IP Address to advertise in the cluster
    options.AdvertisedIPAddress = IPAddress.Parse("172.16.0.42");
    // The socket used for silo-to-silo will bind to this endpoint
    options.GatewayListeningEndpoint = new IPEndPoint(IPAddress.Any, 40000);
    // The socket used by the gateway will bind to this endpoint
    options.SiloListeningEndpoint = new IPEndPoint(IPAddress.Any, 50000);

})
[...]

在内部,silo将监听0.0.0.0:400000.0.0.0:50000,但发布在成员资格提供程序中的值,将为172.16.0.42:11111172.16.0.42:30000

应用部分

    [...]
    // Application parts: just reference one of the grain implementations that we use
    .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ValueGrain).Assembly).WithReferences())
    [...];

虽然此步骤在技术上不是必需的(如果未配置,Orleans将扫描当前文件夹中的所有程序集),但建议开发人员配置此步骤。此步骤将帮助Orleans加载用户程序集和类型。这些程序集称为应用程序部件。所有的grain、grain接口和序列化器,都是使用应用程序部件来发现的。

应用程序部件是通过使用IApplicationPartsManager来配置的,可以通过IClientBuilderISiloHostBuilder中的扩展方法ConfigureApplicationParts来访问它。该ConfigureApplicationParts方法接受一个委托,即Action<IApplicationPartManager>

IApplicationPartManager支持以下常用的扩展方法:

  • AddApplicationPart(assembly) 可以使用此扩展方法添加单个程序集。
  • AddFromAppDomain()添加当前加载到AppDomain中的所有程序集。
  • AddFromApplicationBaseDirectory()加载并添加当前基本路径中的所有程序集(请参阅参考资料AppDomain.BaseDirectory)。

通过上述方法添加的程序集,可以使用以下扩展方法,对其返回的类型IApplicationPartManagerWithAssemblies,进行补充:

  • WithReferences()从添加的部件中,添加其引用的所有程序集。这会立即加载任何传递性引用的程序集。装配加载错误将被忽略。
  • WithCodeGeneration()为添加的部件生成支持代码,并将其添加到部件管理器。请注意,这需要安装Microsoft.Orleans.OrleansCodeGenerator包,通常称为运行时代码生成。

类型发现要求提供的应用程序部件包含特定属性。将构建时代码生成包(Microsoft.Orleans.CodeGenerator.MSBuildMicrosoft.Orleans.OrleansCodeGenerator.Build),添加到包含Grains,Grain Interfaces或序列化器的每个项目中,是确保存在这些属性的推荐方法。构建时代码生成仅支持C#。对于F#、Visual Basic和其他.NET语言,可以在配置期间通过上述的WithCodeGeneration()方法生成代码。有关代码生成的更多信息,请参见相应的章节

猜你喜欢

转载自blog.csdn.net/uddiqpl/article/details/86663845