Exemple #1
0
        public static void Test(string host, int port, string localHost, int?localPort)
        {
            System.Console.ReadLine();

            //var serviceProxyManager = Init(host, port, localHost, localPort);
            var serviceProxyManager = InitWithConfig(host, port, localHost, localPort);

            //var serviceProxyManager = InitWithZookeeper(host, port, localHost, localPort);

            System.Console.ReadLine();

            var beginTime = DateTime.Now;

            for (var i = 0; i < 1; i++)
            {
                CallGetServiceName();
                CallAddCustomer().Wait();
                CallGetCustomer();
                CallQueryCustomer().Wait();
                CallRemoveCustomer();
                CallQueryCustomer().Wait();
                CallRemoveAllCustomer();
                CallQueryCustomer().Wait();
            }

            CallAddOrder().Wait();
            CallQueryOrder().Wait();

            var endTime = DateTime.Now;

            System.Console.WriteLine($"Start time: {beginTime.ToString("yyyy-MM-dd HH:mm:ss")}");
            System.Console.WriteLine($"End time: {endTime.ToString("yyyy-MM-dd HH:mm:ss")} Used time: {endTime - beginTime}");

            System.Console.ReadLine();

            if (serviceSubscriber != null)
            {
                serviceSubscriber.Dispose();
            }

            serviceProxyManager.CloseAsync().ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    System.Console.WriteLine(task.Exception.InnerException.Message);
                    return;
                }
                System.Console.WriteLine("Closed");
            }).ContinueWith(task => BootstrapManager.Disable());
        }
Exemple #2
0
        public static void Test()
        {
            System.Console.ReadLine();

            var serviceProxyManager = InitWithConfig();

            System.Console.ReadLine();

            DoTest();

            System.Console.ReadLine();

            serviceProxyManager.CloseAsync().ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    System.Console.WriteLine(task.Exception.InnerException.Message);
                    return;
                }
                System.Console.WriteLine("Closed");
            }).ContinueWith(task => BootstrapManager.Disable());
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input enter to begin.");
            Console.ReadLine();

            Console.InputEncoding  = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;

            //配置客户端日志工厂
            LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Error);

            //加载配置文件
            string path       = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
            var    configRoot = new ConfigurationBuilder()
                                .AddJsonFile(path)
                                .Build();

            var clientConfig = configRoot.GetClientConfig();

            var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory);

            var serviceProxyManager = new ServiceProxyManager();

            //创建Autofac容器并注册服务类型
            var container = GetAutofacContainer(serviceProxyManager);

            if (clientConfig.ServiceProxies != null)
            {
                //注册服务代理
                foreach (var config in clientConfig.ServiceProxies)
                {
                    var serviceProxy = new ServiceProxy(
                        config.ProxyName,
                        config?.Services)
                                       .AddServices(config.ProxyTypes)
                                       .AddClients(
                        new NodeClientBuilder()
                        .ConfigConnections(config.Connections)
                        .ConfigSerializer(serializer)
                        .ConfigLoginHandler(new DefaultLoginHandler(configRoot.GetDefaultLoginHandlerConfig(config.ProxyName), serializer))             //配置登录处理器
                        .UseDotNetty()
                        .Build()
                        );
                    serviceProxyManager.Regist(serviceProxy);
                }
            }

            try
            {
                //连接服务
                serviceProxyManager.ConnectAsync().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    if (e is NetworkException netEx)
                    {
                        Console.WriteLine($"Connect has net error. Host={netEx.Host}, Port={netEx.Port}, Message={netEx.Message}");
                    }
                    else
                    {
                        throw e;
                    }
                }
            }

            try
            {
                //调用服务
                var sampleService = container.Resolve <ISampleService>();
                var result        = sampleService.Welcome("XNode");
                Console.WriteLine(result);
            }
            catch (RequestTimeoutExcption ex)
            {
                Console.WriteLine($"Timeout: RequestId={ex.Request.Id}");
            }
            catch (ServiceCallException ex)
            {
                Console.WriteLine($"Service call exception: ExceptionId={ex.ExceptionId}, ExceptionMessage={ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }

            Console.ReadLine();

            //关闭服务连接
            serviceProxyManager.CloseAsync();

            //关闭DotNetty事件循环
            BootstrapManager.Disable();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input enter to begin.");
            Console.ReadLine();

            Console.InputEncoding  = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;

            //配置客户端日志工厂
            LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Error);

            //加载配置文件
            string path       = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
            var    configRoot = new ConfigurationBuilder()
                                .AddJsonFile(path)
                                .Build();

            var clientConfig = configRoot.GetClientConfig();

            var serviceProxyManager = new ServiceProxyManager();

            //创建Autofac容器并注册服务类型
            var container = GetAutofacContainer(serviceProxyManager);

            //配置服务发现
            var zookeeperConfig       = configRoot.GetZookeeperConfig();
            var zookeeperClientConfig = configRoot.GetZookeeperClientConfig();

            var serializerList = new List <ISerializer>()
            {
                new MsgPackSerializer(LoggerManager.ClientLoggerFactory),
                new ProtoBufSerializer(LoggerManager.ClientLoggerFactory)
            };

            var serviceProxyFactory = ServiceProxyCreator.CreateDefaultServiceProxyFactory(null);
            var nodeClientFactory   = NodeClientManager.CreateDefaultNodeClientFactory(zookeeperClientConfig, serializerList, LoggerManager.ClientLoggerFactory);

            var serviceSubscriber = new ServiceSubscriber(zookeeperConfig,
                                                          LoggerManager.ClientLoggerFactory,
                                                          new ServiceProxyCreator(LoggerManager.ClientLoggerFactory, serviceProxyFactory, zookeeperClientConfig.Services),
                                                          new NodeClientManager(LoggerManager.ClientLoggerFactory, nodeClientFactory))
                                    .Subscribe(container.GetNodeServiceProxyTypes())
                                    .RegistTo(serviceProxyManager);

            try
            {
                //连接服务
                serviceProxyManager.ConnectAsync().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    if (e is NetworkException netEx)
                    {
                        Console.WriteLine($"Connect has net error. Host={netEx.Host}, Port={netEx.Port}, Message={netEx.Message}");
                    }
                    else
                    {
                        throw e;
                    }
                }
            }

            try
            {
                //调用服务
                var sampleService = container.Resolve <ISampleService>();
                var result        = sampleService.Welcome("XNode");
                Console.WriteLine(result);
            }
            catch (RequestTimeoutExcption ex)
            {
                Console.WriteLine($"Timeout: RequestId={ex.Request.Id}");
            }
            catch (ServiceCallException ex)
            {
                Console.WriteLine($"Service call exception: ExceptionId={ex.ExceptionId}, ExceptionMessage={ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }

            Console.ReadLine();

            //关闭服务订阅
            serviceSubscriber.Dispose();

            //关闭服务连接
            serviceProxyManager.CloseAsync().Wait();

            //关闭DotNetty事件循环
            BootstrapManager.Disable();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input enter to start customer server.");
            Console.ReadLine();

            Console.InputEncoding  = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;

            //配置服务端日志工厂,为了能看到服务调用细节,此处将日志级别设置为Information
            LoggerManager.ServerLoggerFactory.AddConsole(LogLevel.Information);

            //配置客户端日志工厂,为了能看到服务调用细节,此处将日志级别设置为Information
            LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Information);

            //加载配置文件
            string path       = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
            var    configRoot = new ConfigurationBuilder()
                                .AddJsonFile(path)
                                .Build();

            var serviceProxyManager = new ServiceProxyManager();
            var container           = GetAutofacContainer(serviceProxyManager);

            #region 客户端配置

            var clientConfig = configRoot.GetClientConfig();

            var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory);

            var serviceCaller = new ServiceCallerBuilder()
                                .Append(new ZipkinCaller(serializer)) //添加ZipkinCaller
                                .UseDefault()
                                .Build();

            if (clientConfig.ServiceProxies != null)
            {
                foreach (var config in clientConfig.ServiceProxies)
                {
                    var serviceProxy = new ServiceProxy(
                        config.ProxyName,
                        config?.Services,
                        serviceCaller)
                                       .AddServices(config.ProxyTypes)
                                       .AddClients(
                        new NodeClientBuilder()
                        .ConfigConnections(config.Connections)
                        .ConfigSerializer(serializer)
                        .UseDotNetty()
                        .Build()
                        );
                    serviceProxyManager.Regist(serviceProxy);
                }
            }

            #endregion

            #region  务端配置

            var serverConfig = configRoot.GetServerConfig();

            //配置服务
            var nodeServer = new NodeServerBuilder()
                             .ApplyConfig(serverConfig)
                             .ConfigSerializer(new ProtoBufSerializer(LoggerManager.ServerLoggerFactory))
                             .AddServiceProcessor(new ZipkinProcessor()) //添加ZipkinProcessor
                             .UseAutofac(container)
                             .UseDotNetty(serverConfig.ServerInfo)
                             .Build();

            #endregion

            //Zipkin配置
            new ZipkinBootstrapper("CustomerServer")
            .ZipkinAt("192.168.108.131")
            .WithSampleRate(1.0)
            .Start();

            try
            {
                //连接服务
                serviceProxyManager.ConnectAsync().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    if (e is NetworkException netEx)
                    {
                        Console.WriteLine($"Connect has net error. Host={netEx.Host}, Port={netEx.Port}, Message={netEx.Message}");
                    }
                    else
                    {
                        throw e;
                    }
                }
            }

            //启动服务
            nodeServer.StartAsync().Wait();

            Console.ReadLine();

            //关闭服务连接
            serviceProxyManager.CloseAsync();

            //关闭DotNetty事件循环
            BootstrapManager.Disable();

            //关闭服务
            nodeServer.StopAsync();
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input enter to begin.");
            Console.ReadLine();

            Console.InputEncoding  = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;

            //配置客户端日志工厂
            LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Error);

            //加载配置文件
            string path       = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
            var    configRoot = new ConfigurationBuilder()
                                .AddJsonFile(path)
                                .Build();

            var serviceProxyManager = new ServiceProxyManager();
            var container           = GetAutofacContainer(serviceProxyManager);

            #region 客户端配置

            var clientConfig = configRoot.GetClientConfig();

            var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory);

            var serviceCaller = new ServiceCallerBuilder()
                                .Append(new ZipkinCaller(serializer)) //添加ZipkinCaller
                                .UseDefault()
                                .Build();

            if (clientConfig.ServiceProxies != null)
            {
                foreach (var config in clientConfig.ServiceProxies)
                {
                    var serviceProxy = new ServiceProxy(
                        config.ProxyName,
                        config?.Services,
                        serviceCaller)
                                       .AddServices(config.ProxyTypes)
                                       .AddClients(
                        new NodeClientBuilder()
                        .ConfigConnections(config.Connections)
                        .ConfigSerializer(serializer)
                        .UseDotNetty()
                        .Build()
                        );
                    serviceProxyManager.Regist(serviceProxy);
                }
            }

            #endregion

            //Zipkin配置
            new ZipkinBootstrapper("Client")
            .ZipkinAt("192.168.108.131")
            .WithSampleRate(1.0)
            .Start();

            try
            {
                //连接服务
                serviceProxyManager.ConnectAsync().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    if (e is NetworkException netEx)
                    {
                        Console.WriteLine($"Connect has net error. Host={netEx.Host}, Port={netEx.Port}, Message={netEx.Message}");
                    }
                    else
                    {
                        throw e;
                    }
                }
            }

            try
            {
                //调用服务
                var customerService = container.Resolve <ICustomerService>();
                var customer        = customerService.GetCustomers(1).Result;
                Console.WriteLine($"Id = {customer.Id}, Name = {customer.Name}");
                Console.WriteLine("Orders:");
                foreach (var order in customer.Orders)
                {
                    Console.WriteLine($"OrderId = {order.Id}");
                    Console.WriteLine($"Detail:");
                    foreach (var detail in order.Detail)
                    {
                        Console.WriteLine($"GoodsId = {detail.GoodsId}, GoodsName = {detail.GoodsName}, Price = {detail.Price}, Amount = {detail.Amount}");
                    }
                    Console.WriteLine("-----------------------------------");
                }
            }
            catch (RequestTimeoutExcption ex)
            {
                Console.WriteLine($"Timeout: RequestId={ex.Request.Id}");
            }
            catch (ServiceCallException ex)
            {
                Console.WriteLine($"Service call exception: ExceptionId={ex.ExceptionId}, ExceptionMessage={ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }

            Console.ReadLine();

            //关闭服务连接
            serviceProxyManager.CloseAsync();

            //关闭DotNetty事件循环
            BootstrapManager.Disable();
        }