示例#1
0
文件: Program.cs 项目: hlzhang123/Ray
        private static async Task <IClusterClient> StartClientWithRetries(int initializeAttemptsBeforeFailing = 5)
        {
            int            attempt = 0;
            IClusterClient client;

            while (true)
            {
                try
                {
                    client = new ClientBuilder()
                             .UseLocalhostClustering()
                             .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IAccount).Assembly).WithReferences())
                             .ConfigureLogging(logging => logging.AddConsole())
                             .ConfigureServices((servicecollection) =>
                    {
                        SubManager.Parse(servicecollection, typeof(AccountCoreHandler).Assembly); //注册handle
                        servicecollection.AddSingleton <IClientFactory, ClientFactory>();         //注册Client获取方法
                        servicecollection.AddSingleton <ISerializer, ProtobufSerializer>();       //注册序列化组件
                        servicecollection.AddRabbitMQ <MessageInfo>();                            //注册RabbitMq为默认消息队列
                        servicecollection.PostConfigure <RabbitConfig>(c =>
                        {
                            c.UserName    = "******";
                            c.Password    = "******";
                            c.Hosts       = new[] { "192.168.125.230:5672" };
                            c.MaxPoolSize = 100;
                            c.VirtualHost = "test";
                        });
                    })
                             .Build();

                    await client.ConnectAndFill();

                    Console.WriteLine("Client successfully connect to silo host");
                    break;
                }
                catch (SiloUnavailableException)
                {
                    attempt++;
                    Console.WriteLine($"Attempt {attempt} of {initializeAttemptsBeforeFailing} failed to initialize the Orleans client.");
                    if (attempt > initializeAttemptsBeforeFailing)
                    {
                        throw;
                    }
                    await Task.Delay(TimeSpan.FromSeconds(4));
                }
            }

            return(client);
        }