예제 #1
0
파일: Program.cs 프로젝트: wxlevel/Ray
        private static async Task <int> RunMainAsync()
        {
            try
            {
                var servicecollection = new ServiceCollection();
                SubManager.Parse(servicecollection, typeof(AccountCoreHandler).Assembly); //注册handle
                servicecollection.AddSingleton <IClientFactory, ClientFactory>();         //注册Client获取方法
                servicecollection.AddSingleton <ISerializer, ProtobufSerializer>();       //注册序列化组件
                servicecollection.AddRabbitMQ <MessageInfo>();                            //注册RabbitMq为默认消息队列
                servicecollection.AddLogging(logging => logging.AddConsole());
                servicecollection.PostConfigure <RabbitConfig>(c =>
                {
                    c.UserName    = "******";
                    c.Password    = "******";
                    c.Hosts       = new[] { "127.0.0.1:5672" };
                    c.MaxPoolSize = 100;
                    c.VirtualHost = "/";
                });
                var provider = servicecollection.BuildServiceProvider();
                using (var client = await StartClientWithRetries())
                {
                    var manager = provider.GetService <ISubManager>();
                    await manager.Start(new[] { "Core", "Read", "Rep" });

                    var aActor = client.GetGrain <IAccount>(1);
                    var bActor = client.GetGrain <IAccount>(2);
                    while (true)
                    {
                        Console.WriteLine("Press Enter for times...");
                        var length    = int.Parse(Console.ReadLine());
                        var stopWatch = new Stopwatch();
                        stopWatch.Start();
                        var tasks = new Task[length * 2];
                        Parallel.For(0, length, i =>
                        {
                            tasks[i * 2]     = aActor.AddAmount(1000);  //1用户充值1000
                            tasks[i * 2 + 1] = aActor.Transfer(2, 500); //转给2用户500
                        });
                        await Task.WhenAll(tasks);

                        stopWatch.Stop();
                        Console.WriteLine($"{length * 2}次操作完成,耗时:{stopWatch.ElapsedMilliseconds}ms");
                        await Task.Delay(200);

                        Console.WriteLine($"End:1的余额为{await aActor.GetBalance()},2的余额为{await bActor.GetBalance()}");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(1);
            }
        }
예제 #2
0
        private static async Task <IClusterClient> StartClientWithRetries(int initializeAttemptsBeforeFailing = 5)
        {
            var            siloAddress = IPAddress.Loopback;
            var            gatewayPort = 30000;
            int            attempt     = 0;
            IClusterClient client;

            while (true)
            {
                try
                {
                    client = new ClientBuilder()
                             .ConfigureCluster(options => { options.ClusterId = "helloworldcluster"; })
                             .UseStaticClustering(options => options.Gateways.Add((new IPEndPoint(siloAddress, gatewayPort)).ToGatewayUri()))
                             .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IAccount).Assembly).WithReferences())
                             .ConfigureLogging(logging => logging.AddConsole())
                             .ConfigureServices((servicecollection) =>
                    {
                        SubManager.Parse(servicecollection, typeof(AccountCoreHandler).Assembly);       //注册handle
                        servicecollection.AddSingleton <IOrleansClientFactory, OrleansClientFactory>(); //注册Client获取方法
                        servicecollection.AddSingleton <ISerializer, ProtobufSerializer>();             //注册序列化组件
                        servicecollection.AddRabbitMQ <MessageInfo>();                                  //注册RabbitMq为默认消息队列
                        servicecollection.PostConfigure <RabbitConfig>(c =>
                        {
                            c.UserName    = "******";
                            c.Password    = "******";
                            c.Hosts       = new[] { "127.0.0.1:5672" };
                            c.MaxPoolSize = 100;
                            c.VirtualHost = "/";
                        });
                    })
                             .Build();

                    await client.Connect();

                    OrleansClientFactory.Init(client);
                    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);
        }
예제 #3
0
        private static async Task <int> RunMainAsync()
        {
            var servicecollection = new ServiceCollection();

            SubManager.Parse(servicecollection, typeof(AccountCoreHandler).Assembly); //注册handle
            servicecollection.AddSingleton <IClientFactory, ClientFactory>();         //注册Client获取方法
            servicecollection.AddSingleton <ISerializer, ProtobufSerializer>();       //注册序列化组件
            servicecollection.AddRabbitMQ <MessageInfo>();                            //注册RabbitMq为默认消息队列
            servicecollection.AddLogging(logging => logging.AddConsole());
            servicecollection.PostConfigure <RabbitConfig>(c =>
            {
                c.UserName    = "******";
                c.Password    = "******";
                c.Hosts       = new[] { "192.168.125.230:5672" };
                c.MaxPoolSize = 100;
                c.VirtualHost = "/";
            });
            var provider = servicecollection.BuildServiceProvider();

            try
            {
                using (var client = await StartClientWithRetries())
                {
                    var manager = provider.GetService <ISubManager>();
                    await manager.Start(new[] { "Core", "Read", "Rep" });

                    while (true)
                    {
                        // var actor = client.GetGrain<IAccount>(0);
                        // Console.WriteLine("Press Enter for times...");
                        Console.WriteLine("start");
                        var length    = 1000;// int.Parse(Console.ReadLine());
                        var stopWatch = new Stopwatch();
                        stopWatch.Start();
                        await Task.WhenAll(Enumerable.Range(0, length).Select(x => client.GetGrain <IAccount>(0).AddAmount(1000).AsTask()));

                        stopWatch.Stop();
                        Console.WriteLine($"{length }次操作完成,耗时:{stopWatch.ElapsedMilliseconds}ms");
                        await Task.Delay(200);

                        Console.WriteLine($"余额为{await client.GetGrain<IAccount>(0).GetBalance()}");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(1);
            }
        }
예제 #4
0
        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().SetMinimumLevel(LogLevel.Warning))
                             .ConfigureServices((servicecollection) =>
                    {
                        SubManager.Parse(servicecollection, typeof(AccountToDbHandler).Assembly);       //注册handle
                        servicecollection.AddSingleton <IOrleansClientFactory, OrleansClientFactory>(); //注册Client获取方法
                        servicecollection.AddSingleton <ISerializer, ProtobufSerializer>();             //注册序列化组件
                        servicecollection.AddRabbitMQ <MessageInfo>();                                  //注册RabbitMq为默认消息队列
                        servicecollection.PostConfigure <RabbitConfig>(c =>
                        {
                            c.UserName    = "******";
                            c.Password    = "******";
                            c.Hosts       = new[] { "127.0.0.1:5672" };
                            c.MaxPoolSize = 100;
                            c.VirtualHost = "/";
                        });
                    })
                             .Build();

                    await client.Connect();

                    OrleansClientFactory.Init(client);
                    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);
        }
예제 #5
0
파일: Program.cs 프로젝트: wxlevel/Ray
        private static async Task <int> RunMainAsync()
        {
            var servicecollection = new ServiceCollection();

            SubManager.Parse(servicecollection, typeof(AccountCoreHandler).Assembly); //注册handle
            servicecollection.AddSingleton <IClientFactory, ClientFactory>();         //注册Client获取方法
            servicecollection.AddSingleton <ISerializer, ProtobufSerializer>();       //注册序列化组件
            servicecollection.AddRabbitMQ <MessageInfo>();                            //注册RabbitMq为默认消息队列
            servicecollection.AddLogging(logging => logging.AddConsole());
            servicecollection.PostConfigure <RabbitConfig>(c =>
            {
                c.UserName    = "******";
                c.Password    = "******";
                c.Hosts       = new[] { "127.0.0.1:5672" };
                c.MaxPoolSize = 100;
                c.VirtualHost = "/";
            });
            var provider = servicecollection.BuildServiceProvider();

            try
            {
                using (var client = await StartClientWithRetries())
                {
                    var manager = provider.GetService <ISubManager>();
                    await manager.Start(new[] { "Core", "Read", "Rep" });

                    while (true)
                    {
                        Console.WriteLine("Press Enter to count...X100");
                        var        actorAcount = int.Parse(Console.ReadLine()) * 100;
                        IAccount[] actors      = new IAccount[actorAcount];
                        for (int i = 0; i < actorAcount; i++)
                        {
                            actors[i] = client.GetGrain <IAccount>(i);
                        }
                        Console.WriteLine("Press Enter for times...");
                        var length    = int.Parse(Console.ReadLine());
                        var stopWatch = new Stopwatch();
                        stopWatch.Start();

                        for (int i = 0; i < length; i++)
                        {
                            var transfer = false;
                            await Task.WhenAll(Enumerable.Range(0, actorAcount).Select(async x =>
                            {
                                if (!transfer)
                                {
                                    await actors[x].AddAmount(1000);
                                    transfer = true;
                                }
                                else
                                {
                                    await actors[x - 1].Transfer(x, 500);
                                    transfer = false;
                                }
                            }));
                        }
                        stopWatch.Stop();
                        Console.WriteLine($"{length * actorAcount}次操作完成,耗时:{stopWatch.ElapsedMilliseconds}ms");
                        await Task.Delay(200);

                        for (int i = 0; i < actorAcount; i++)
                        {
                            Console.WriteLine($"End:{i}的余额为{await actors[i].GetBalance()}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(1);
            }
        }