예제 #1
0
        public static void Start(params string[] args)
        {
            var builder = new MicroBuilder();

            builder.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Information);
                builder.AddConsole();
            });
            var services = builder
                           .AddMicroClient(builder =>
            {
                builder
                .AddJsonCodec()
                //.AddMessagePackCodec()
                //.AddProtoBufCodec()
                .AddSession()
                //.AddHttpProtocol()
                .AddTcpProtocol()
                //.AddWebSocketProtocol()
                //.AddGrpcProtocol()
                //.AddNacos(opt =>
                //{
                //    opt.Host = "http://192.168.0.231:8848/";
                //    opt.Tenant = "ef950bae-865b-409b-9c3b-bc113cf7bf37";
                //})
                //.AddConsul("http://192.168.0.231:8500")
                .AddDefaultRouter(r =>
                {
                    r.Regist("Spear.Tests.Contracts_v1", new ServiceAddress("127.0.0.1", 5003)
                    {
                        Service  = "192.168.10.217",
                        Protocol = ServiceProtocol.Tcp,
                        Codec    = ServiceCodec.Json,
                        Gzip     = false
                    });
                })
                ;
            });

            services.AddSingleton <IService, ServieA>();
            services.AddSingleton <IService, ServieB>();
            var provider = services.BuildServiceProvider();

            CurrentIocManager.CreateLogger <Client>().LogInformation("test");

            var logger = provider.GetService <ILogger <Client> >();

            logger.LogInformation("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }
                //SingleTest(provider, message, logger);
                CodeTimerTest(provider, message, logger);
            }
        }
예제 #2
0
파일: Client.cs 프로젝트: zhangbo27/spear
        public static void Start(params string[] args)
        {
            var services = new MicroBuilder()
                           .AddMicroClient(builder =>
            {
                builder
                .AddJsonCodec()
                .AddMessagePackCodec()
                //.AddProtoBufCodec()
                .AddSession()
                .AddHttpProtocol()
                .AddTcpProtocol()
                .AddWebSocketProtocol()
                .AddGrpcProtocol()
                //.AddNacos(opt =>
                //{
                //    opt.Host = "http://192.168.0.231:8848/";
                //    opt.Tenant = "ef950bae-865b-409b-9c3b-bc113cf7bf37";
                //})
                .AddConsul("http://192.168.0.231:8500")
                ;
            });

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Information);
                builder.AddConsole();
            });
            services.AddSingleton <DefaultAdapter>();
            services.AddSingleton <IService, ServieA>();
            services.AddSingleton <IService, ServieB>();
            var provider = services.BuildServiceProvider();

            LogManager.AddAdapter(provider.GetService <DefaultAdapter>());
            LogManager.Logger <Client>().Info("test");

            var logger = provider.GetService <ILogger <Client> >();

            logger.LogInformation("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }
                //SingleTest(provider, message, logger);
                CodeTimerTest(provider, message, logger);
            }
        }
예제 #3
0
        private static void Main(string[] args)
        {
            var  port = -1;
            bool?gzip = null;

            if (args.Length > 0)
            {
                int.TryParse(args[0], out port);
            }
            var protocol = ServiceProtocol.Tcp;
            var codec    = ServiceCodec.Json;

            if (args.Length > 1)
            {
                protocol = args[1].CastTo(ServiceProtocol.Tcp);
            }
            if (args.Length > 2)
            {
                codec = args[2].CastTo(ServiceCodec.Json);
            }
            if (args.Length > 3)
            {
                gzip = args[3].CastTo(false);
            }

            //ConfigHelper.Instance.UseLocal("_config");

            Console.WriteLine("shay".Config <string>());

            var services = new MicroBuilder();

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Information);
                builder.AddFilter("System", level => level >= LogLevel.Warning);
                builder.AddFilter("Microsoft", level => level >= LogLevel.Warning);
                builder.AddConsole();
            });

            services
            .AddMicroService(builder =>
            {
                switch (codec)
                {
                case ServiceCodec.Json:
                    builder.AddJsonCodec();
                    break;

                case ServiceCodec.MessagePack:
                    builder.AddMessagePackCodec();
                    break;

                case ServiceCodec.ProtoBuf:
                    builder.AddProtoBufCodec();
                    break;
                }

                builder
                .AddSession()
                //.AddNacos()
                //.AddConsul()
                .AddDefaultRouter();
                ;

                switch (protocol)
                {
                case ServiceProtocol.Tcp:
                    builder.AddTcpProtocol();
                    break;

                case ServiceProtocol.Http:
                    builder.AddHttpProtocol();
                    break;

                case ServiceProtocol.Ws:
                    builder.AddWebSocketProtocol();
                    break;

                case ServiceProtocol.Grpc:
                    builder.AddGrpcProtocol();
                    break;
                }
            })
            .AddMicroClient(builder =>
            {
                //支持多编解码&多协议
                builder
                .AddJsonCodec()
                .AddMessagePackCodec()
                .AddProtoBufCodec()
                .AddHttpProtocol()
                .AddTcpProtocol()
                .AddWebSocketProtocol()
                .AddGrpcProtocol()
                .AddSession()
                .AddDefaultRouter()
                //.AddConsul("http://192.168.0.231:8500")
                ;
            });
            services.AddSingleton <ITestContract, TestService>();
            services.AddScoped <AccountService>();

            _provider = services.BuildServiceProvider();
            //_provider.UseNacosConfig();

            _provider.UseMicroService(address =>
            {
                var m = "micro".Config <ServiceAddress>();
                if (m == null)
                {
                    return;
                }
                address.Service = m.Service;
                address.Host    = m.Host;
                address.Port    = port > 80 ? port : m.Port;
                if (address.Port < 80)
                {
                    address.Port = 5000;
                }
                address.Weight = m.Weight;
                address.Gzip   = gzip ?? m.Gzip;
            });
            AppDomain.CurrentDomain.ProcessExit += async(sender, eventArgs) => await Shutdown();

            Console.CancelKeyPress += async(sender, eventArgs) =>
            {
                await Shutdown();

                eventArgs.Cancel = true;
            };
            var proxy    = _provider.GetService <IProxyFactory>();
            var contract = proxy.Create <ITestContract>();
            var result   = contract.Get("shay");

            Console.WriteLine(result.Result);
            while (true)
            {
                var cmd = Console.ReadLine();
                if (cmd == "exit")
                {
                    break;
                }
            }
        }
예제 #4
0
파일: Client.cs 프로젝트: ztxyzu/spear
        public static void Start(params string[] args)
        {
            var services = new MicroBuilder()
                           .AddMicroClient(builder =>
            {
                builder.AddJsonCoder()
                .AddSession()
                .AddHttpProtocol()
                .AddTcpProtocol()
                .AddNacos(opt =>
                {
                    opt.Host   = "http://192.168.0.231:8848/";
                    opt.Tenant = "ef950bae-865b-409b-9c3b-bc113cf7bf37";
                })
                //.AddConsul("http://192.168.0.231:8500")
                ;
            });

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Warning);
                builder.AddConsole();
            });
            services.AddSingleton <DefaultAdapter>();
            services.AddSingleton <IService, ServieA>();
            services.AddSingleton <IService, ServieB>();
            var provider = services.BuildServiceProvider();

            LogManager.AddAdapter(provider.GetService <DefaultAdapter>());
            LogManager.Logger <Client>().Info("test");

            var logger = provider.GetService <ILogger <Client> >();

            logger.LogInformation("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }
                var msgArgs = message.Split(new[] { ",", ";", " " }, StringSplitOptions.RemoveEmptyEntries);
                int repeat = 1, thread = 1;
                var isNotice = true;
                if (msgArgs.Length > 1)
                {
                    repeat = msgArgs[1].CastTo(1);
                }
                if (msgArgs.Length > 2)
                {
                    thread = msgArgs[2].CastTo(1);
                }
                if (msgArgs.Length > 3)
                {
                    isNotice = msgArgs[3].CastTo(true);
                }
                message = msgArgs[0];
                //var watch = Stopwatch.StartNew();
                //var proxy = provider.GetService<IProxyFactory>();
                //var service = proxy.Create<ITestContract>();
                //if (isNotice)
                //{
                //    service.Notice(message).GetAwaiter().GetResult();
                //}
                //else
                //{
                //    var msg = service.Get(message).Result;
                //    logger.Debug(msg);
                //}
                //watch.Stop();
                //logger.Info(watch.ElapsedMilliseconds);
                Task.Run(async() =>
                {
                    var proxy   = provider.GetService <IProxyFactory>();
                    var service = proxy.Create <ITestContract>();
                    //if (message.StartsWith("s"))
                    //{
                    //    var accessor = provider.GetService<IPrincipalAccessor>();
                    //    accessor.SetSession(new MicroSessionDto
                    //    {
                    //        UserId = RandomHelper.Random().Next(10000, 99999),
                    //        UserName = RandomHelper.RandomLetters(5),
                    //        Role = "admin"
                    //    });
                    //}

                    var result = await CodeTimer.Time("micro test", repeat, async() =>
                    {
                        try
                        {
                            var name = RandomHelper.RandomLetters(5);
                            var m    = $"hello {name}";
                            if (RandomHelper.Random().Next(10) > 5)
                            {
                                m           += " loged";
                                var accessor = provider.GetService <IPrincipalAccessor>();
                                accessor.SetSession(new MicroSessionDto
                                {
                                    UserId   = name,
                                    UserName = name,
                                    Role     = name
                                });
                            }
                            if (isNotice)
                            {
                                await service.Notice(m);
                            }
                            else
                            {
                                var msg = await service.Get(m);
                                logger.LogInformation(msg);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, ex.Message);
                            throw;
                        }
                    }, thread);
                    Console.WriteLine(result.ToString());
                });
            }
        }
예제 #5
0
        private static void Main(string[] args)
        {
            var port = -1;

            if (args.Length > 0)
            {
                int.TryParse(args[0], out port);
            }
            var protocol = ServiceProtocol.Tcp;

            if (args.Length > 1)
            {
                Enum.TryParse(args[1], out protocol);
            }

            ConfigManager.Instance.UseLocal("_config");

            Console.WriteLine("shay".Config <string>());

            var services = new MicroBuilder();

            services.AddMicroService(builder =>
            {
                builder
                .AddJsonCoder()
                .AddSession()
                .AddNacos()
                //.AddConsul()
                ;
                switch (protocol)
                {
                case ServiceProtocol.Tcp:
                    builder.AddTcpProtocol();
                    break;

                case ServiceProtocol.Http:
                    builder.AddHttpProtocol();
                    break;
                }
            });
            services.AddSingleton <ITestContract, TestService>();
            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Information);
                builder.AddConsole();
            });
            _provider = services.BuildServiceProvider();
            _provider.UseNacosConfig();

            _provider.UseMicroService(address =>
            {
                var m = "micro".Config <ServiceAddress>();
                if (m == null)
                {
                    return;
                }
                address.Service = m.Service;
                address.Host    = m.Host;
                address.Port    = port > 80 ? port : m.Port;
                if (address.Port < 80)
                {
                    address.Port = 5000;
                }
                address.Weight = m.Weight;
            });
            AppDomain.CurrentDomain.ProcessExit += async(sender, eventArgs) => await Shutdown();

            Console.CancelKeyPress += async(sender, eventArgs) =>
            {
                await Shutdown();

                eventArgs.Cancel = true;
            };
            var config = "oss:keyId".Config <string>();

            _provider.GetService <ILogger <Program> >().LogInformation(config);
            Console.ReadLine();
        }