Orleans 2.0 官方文档 —— 9.5.2 迁移-> 在Azure云服务中,从Orleans 1.5 迁移到2.0

使用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.AzureCloudServicesNuGet包获得支持。

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,通过SiloHostBuilderISiloHost,来配置和承载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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值