使用Azure时,从Orleans 1.5迁移到2.0
在Orleans 2.0中,silo和客户端的配置发生了变化。在Orleans 1.5中,我们曾经有一个整体式的对象来处理所有配置文件,提供程序也被添加到该配置对象中。在Orleans 2.0中,配置过程是围绕着SiloHostBuilder
组织的,类似于在ASP.NET Core中使用WebHostBuilder
。
在Orleans 1.5中,Azure的配置如下所示:
var config = AzureSilo.DefaultConfiguration();
config.AddMemoryStorageProvider();
config.AddAzureTableStorageProvider("AzureStore", RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
AzureSilo
类公开了一个名为DefaultConfiguration()的静态方法,用于加载配置XML文件。这种配置silo的方式已弃用,但仍通过旧版支持包,获得支持。
在Orleans 2.0中,配置完全是程序化的。新的配置API如下所示:
//Load the different settings from the services configuration
var proxyPort = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["OrleansProxyEndpoint"].IPEndpoint.Port;
var siloEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["OrleansSiloEndpoint"].IPEndpoint;
var connectionString = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString");
var deploymentId = RoleEnvironment.DeploymentId;
var builder = new SiloHostBuilder()
//Set service ID and cluster ID
.Configure<ClusterOptions>(options =>
{
options.ClusterId = deploymentId;
options.ServiceIs = "my-app";
})
// Set silo name
.Configure<SiloOptions>(options => options.SiloName = this.Name)
//Then, we can configure the different endpoints
.ConfigureEndpoints(siloEndpoint.Address, siloEndpoint.Port, proxyPort)
//Then, we set the connection string for the storage
.UseAzureStorageClustering(options => options.ConnectionString = connectionString)
//If reminders are needed, add the service, the connection string is required
.UseAzureTableReminderService(connectionString)
//If Queues are needed, add the service, set the name and the Adapter, the one shown here
//is the one provided with Orleans, but it can be a custom one
.AddAzureQueueStreams<AzureQueueDataAdapterV2>("StreamProvider",
configurator => configurator.Configure(configure =>
{
configure.ConnectionString = connectionString;
}))
//If Grain Storage is needed, add the service and set the name
.AddAzureTableGrainStorage("AzureTableStore");
AzureSilo到ISiloHost
在Orleans 1.5中,AzureSilo
类是在Azure工作者角色中承载silo的推荐方法。这仍然通过Microsoft.Orleans.Hosting.AzureCloudServices
NuGet包获得支持。
public class WorkerRole : RoleEntryPoint
{
AzureSilo silo;
public override bool OnStart()
{
// Do other silo initialization – for example: Azure diagnostics, etc
return base.OnStart();
}
public override void OnStop()
{
silo.Stop();
base.OnStop();
}
public override void Run()
{
var config = AzureSilo.DefaultConfiguration();
config.AddMemoryStorageProvider();
config.AddAzureTableStorageProvider("AzureStore", RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
// Configure storage providers
silo = new AzureSilo();
bool ok = silo.Start(config);
silo.Run(); // Call will block until silo is shutdown
}
}
Orleans 2.0提供了一个更灵活、更模块化的API,通过SiloHostBuilder
和ISiloHost
,来配置和承载silo。
public class WorkerRole : RoleEntryPoint
{
private ISiloHost host;
private ISiloHostBuilder builder;
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
public override void Run()
{
try
{
this.RunAsync(this.cancellationTokenSource.Token).Wait();
runCompleteEvent.WaitOne();
}
finally
{
this.runCompleteEvent.Set();
}
}
public override bool OnStart()
{
//builder is the SiloHostBuilder from the first section
// Build silo host, so that any errors will restart the role instance
this.host = this.builder.Build();
base.OnStart();
}
public override void OnStop()
{
this.cancellationTokenSource.Cancel();
this.runCompleteEvent.WaitOne();
this.host.StopAsync().Wait();
base.OnStop();
}
private Task RunAsync(CancellationToken cancellationToken)
{
return this.host.StartAsync(cancellationToken);
}
}