private static IServiceProxyManager Init(string host, int port, string localHost, int?localPort) { var proxyName = "XNodeDemoService"; LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Information); var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory); //new MsgPackSerializer(LoggerManager.ClientLoggerFactory); var nodeClientParametersList = new List <NodeClientParameters>() { new NodeClientParameters() { Host = host, Port = port, LocalHost = localHost, LocalPort = localPort, Serializer = serializer, Communication = new DotNettyClient(host, port), LoginHandler = new DefaultLoginHandler(new DefaultLoginHandlerConfig() { AccountName = "Test01", AccountKey = "123456" }, serializer) } }; var nodeClientContainer = new DefaultNodeClientContainer(); foreach (var c in nodeClientParametersList) { nodeClientContainer.Add(new DefaultNodeClient(c)); } var serviceProxy = new ServiceProxy(proxyName, null, null, nodeClientContainer) .AddService <ICustomerService>() .AddService <OrderService>() .EnableAll(); var serviceProxyManager = new ServiceProxyManager(); serviceProxyManager.Regist(serviceProxy); serviceProxyManager.ConnectAsync().Wait(); var builder = new ContainerBuilder(); builder.Register(c => new ServiceProxyInterceptor(serviceProxyManager)); builder.RegisterType <CustomerService>() .As <ICustomerService>() .EnableInterfaceInterceptors() //接口拦截 .InterceptedBy(typeof(ServiceProxyInterceptor)) .SingleInstance(); builder.RegisterType <OrderService>() //.As<IOrderService>() .EnableClassInterceptors() //类拦截 .InterceptedBy(typeof(ServiceProxyInterceptor)) .SingleInstance(); container = builder.Build(); return(serviceProxyManager); }
private static IServiceProxyManager InitWithConfig() { string path = Path.Combine(Directory.GetCurrentDirectory(), "config_simple.json"); var configRoot = new ConfigurationBuilder() .AddJsonFile(path) .Build(); var globalConfig = configRoot.GetGlobalConfig(); GlobalSettings.Apply(globalConfig); var clientConfig = configRoot.GetClientConfig(); LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Error); //var serializer = new MsgPackSerializer(LoggerManager.ClientLoggerFactory); var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory); var serviceCaller = new ServiceCallerBuilder() .UseDefault() .Build(); var serviceProxyManager = new ServiceProxyManager(); foreach (var serviceProxyConfig in clientConfig.ServiceProxies) { var serviceProxy = new ServiceProxy( serviceProxyConfig.ProxyName, serviceProxyConfig?.Services, serviceCaller) .AddServices(serviceProxyConfig.ProxyTypes) .AddClients( new NodeClientBuilder() .ConfigConnections(serviceProxyConfig.Connections) .ConfigSerializer(serializer) .ConfigLoginHandler(new DefaultLoginHandler(configRoot.GetDefaultLoginHandlerConfig(serviceProxyConfig.ProxyName), serializer)) .UseDotNetty() .Build() ); serviceProxyManager.Regist(serviceProxy); } serviceProxyManager.ConnectAsync().Wait(); var builder = new ContainerBuilder(); builder.Register(c => new ServiceProxyInterceptor(serviceProxyManager)); builder.RegisterType <SimpleService>() .As <ISimpleService>() .EnableInterfaceInterceptors() //接口拦截 .InterceptedBy(typeof(ServiceProxyInterceptor)) .SingleInstance(); container = builder.Build(); return(serviceProxyManager); }
private static IServiceProxyManager StartClient(IConfigurationRoot configRoot) { var clientConfig = configRoot.GetClientConfig(); var serviceProxyManager = new ServiceProxyManager(); if (clientConfig.ServiceProxies == null || clientConfig.ServiceProxies.Count == 0) { return(serviceProxyManager); } //var serializer = new MsgPackSerializer(LoggerManager.ClientLoggerFactory); var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory); var serviceCaller = new ServiceCallerBuilder() .UseDefault() .Build(); 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) .ConfigLoginHandler(new DefaultLoginHandler(configRoot.GetDefaultLoginHandlerConfig(config.ProxyName), serializer)) .UseDotNetty() .Build() ); serviceProxyManager.Regist(serviceProxy); } serviceProxyManager.ConnectAsync().Wait(); return(serviceProxyManager); }
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(); }
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(); }
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(); }
public static void Test(string host, int port, string localHost, int?localPort) { System.Console.ReadLine(); var proxyName = "XNodeDemoService"; LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Information); var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory); var nodeClientParametersList = new List <NodeClientParameters>() { new NodeClientParameters() { Host = host, Port = port, LocalHost = localHost, LocalPort = localPort, Serializer = serializer, Communication = new DotNettyClient(host, port, localHost, localPort) } }; var nodeClientContainer = new DefaultNodeClientContainer(); foreach (var c in nodeClientParametersList) { nodeClientContainer.Add(new DefaultNodeClient(c)); } var serviceList = new List <Type>() { typeof(ICustomerService), typeof(IOrderService) }; var serviceProxyManager = new ServiceProxyManager(); serviceProxy = new ServiceProxy(proxyName, null, null, nodeClientContainer) .AddService <ICustomerService>() .AddService <OrderService>() .EnableAll(); serviceProxyManager.Regist(serviceProxy); serviceProxyManager.ConnectAsync().Wait(); CallGetServiceName().Wait(); CallAddCustomer().Wait(); CallGetCustomer().Wait(); CallQueryCustomer().Wait(); CallRemoveCustomer().Wait(); CallQueryCustomer().Wait(); CallRemoveAllCustomer().Wait(); CallQueryCustomer().Wait(); CallAddOrder().Wait(); CallQueryOrder().Wait(); System.Console.ReadLine(); serviceProxyManager.CloseAsync().ContinueWith(task => { if (task.Exception != null) { System.Console.WriteLine(task.Exception.InnerException.Message); return; } System.Console.WriteLine("Closed"); }); }
private static IServiceProxyManager InitWithConfig(string host, int port, string localHost, int?localPort) { string path = Path.Combine(Directory.GetCurrentDirectory(), "config.json"); var configRoot = new ConfigurationBuilder() .AddJsonFile(path) .Build(); var clientConfig = configRoot.GetClientConfig(); LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Information); var serviceProxyTypeList = new List <Type>() { typeof(ICustomerService), typeof(OrderService) }; new ZipkinBootstrapper("XNodeDemoClient", System.Net.IPAddress.Parse(localHost), (short)(localPort != null ? localPort.Value : 0)) .ZipkinAt("192.168.87.131") .WithSampleRate(1.0) .Start(); //var serializer = new MsgPackSerializer(LoggerManager.ClientLoggerFactory); var serializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory); var serviceCaller = new ServiceCallerBuilder() .Append(new ZipkinCaller(serializer)) .UseRetry() .UseDefault() .Build(); var serviceProxyManager = new ServiceProxyManager(); foreach (var serviceProxyConfig in clientConfig.ServiceProxies) { var serviceProxy = new ServiceProxy( serviceProxyConfig.ProxyName, serviceProxyConfig?.Services, serviceCaller) .AddServices(serviceProxyConfig.ProxyTypes) .AddClients( new NodeClientBuilder() .ConfigConnections(serviceProxyConfig.Connections) .ConfigSerializer(serializer) .ConfigLoginHandler(new DefaultLoginHandler(configRoot.GetDefaultLoginHandlerConfig(serviceProxyConfig.ProxyName), serializer)) .ConfigPassiveClosedStrategy(new DefaultPassiveClosedStrategy(configRoot.GetDefaultPassiveClosedStrategyConfig(serviceProxyConfig.ProxyName), LoggerManager.ClientLoggerFactory)) .UseDotNetty() .Build() ); serviceProxyManager.Regist(serviceProxy); } serviceProxyManager.ConnectAsync().Wait(); var builder = new ContainerBuilder(); builder.Register(c => new ServiceProxyInterceptor(serviceProxyManager)); builder.RegisterType <CustomerService>() .As <ICustomerService>() .EnableInterfaceInterceptors() //接口拦截 .InterceptedBy(typeof(ServiceProxyInterceptor)) .SingleInstance(); builder.RegisterType <OrderService>() //.As<IOrderService>() .EnableClassInterceptors() //类拦截 .InterceptedBy(typeof(ServiceProxyInterceptor)) .SingleInstance(); container = builder.Build(); return(serviceProxyManager); }
static void Main(string[] args) { System.Console.InputEncoding = Encoding.UTF8; System.Console.OutputEncoding = Encoding.UTF8; var dir = Directory.GetCurrentDirectory(); var fileName = "config.json"; string path = Path.Combine(dir, fileName); var configRoot = new ConfigurationBuilder() .AddJsonFile(path) .Build(); var name = configRoot.GetValue <string>("name"); var globalConfig = configRoot.GetGlobalConfig(); GlobalSettings.Apply(globalConfig); var serviceProxyManager = new ServiceProxyManager(); container = GetAutofacContainer(serviceProxyManager); #region Client配置 var clientConfig = configRoot.GetClientConfig(); LoggerManager.ClientLoggerFactory.AddConsole(LogLevel.Information); var clientSerializer = new ProtoBufSerializer(LoggerManager.ClientLoggerFactory); var serviceCaller = new ServiceCallerBuilder() //.Append(new ZipkinCaller(clientSerializer)) .UseRetry() .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(clientSerializer) .ConfigLoginHandler(new DefaultLoginHandler(configRoot.GetDefaultLoginHandlerConfig(config.ProxyName), clientSerializer)) .UseDotNetty() .Build() ); serviceProxyManager.Regist(serviceProxy); } } #endregion #region Server配置 LoggerManager.ServerLoggerFactory.AddConsole(LogLevel.Information); var loginValidator = new DefaultLoginValidator(configRoot.GetDefaultLoginValidatorConfig(), LoggerManager.ServerLoggerFactory); var serviceAuthorizer = new DefaultServiceAuthorizer(configRoot.GetDefaultServiceAuthorizeConfig(), LoggerManager.ServerLoggerFactory); var serverConfig = configRoot.GetServerConfig(); var nodeServer = new NodeServerBuilder() .ApplyConfig(serverConfig) .ConfigSerializer(new ProtoBufSerializer(LoggerManager.ServerLoggerFactory)) .ConfigLoginValidator(loginValidator) //.AddServiceProcessor(new ZipkinProcessor()) .AddServiceProcessor(new ServiceAuthorizeProcessor(serviceAuthorizer)) .UseDotNetty(serverConfig.ServerInfo) .UseAutofac(container) .Build(); #endregion #region 配置监视 IFileProvider fileProvider = new PhysicalFileProvider(dir); ChangeToken.OnChange(() => fileProvider.Watch(fileName), () => { configRoot.Reload(); serviceAuthorizer.LoadConfig(configRoot.GetDefaultServiceAuthorizeConfig()); loginValidator.LoadConfig(configRoot.GetDefaultLoginValidatorConfig()); }); #endregion #region 启动 System.Console.WriteLine("Please enter: 1-Start services and test, 2-Only start services."); var isTest = System.Console.ReadLine(); nodeServer.StartAsync().Wait(); try { serviceProxyManager.ConnectAsync().Wait(); } catch (AggregateException ex) { foreach (var e in ex.InnerExceptions) { if (e is NetworkException netEx) { System.Console.WriteLine($"Connect has net error. Host={netEx.Host}, Port={netEx.Port}, Message={netEx.Message}"); } else { throw e; } } } #endregion #region Test if (isTest == "1") { //new ZipkinBootstrapper(name) // .ZipkinAt("192.168.87.131") // .WithSampleRate(1.0) // .Start(); CallAddCustomer().Wait(); CallGetCustomer(); CallQueryCustomer().Wait(); CallAddOrder().Wait(); CallQueryOrder().Wait(); CallGetOrders().Wait(); CallRemoveCustomer(); CallQueryCustomer().Wait(); CallRemoveAllCustomer(); CallQueryCustomer().Wait(); CallSaveCustomerPhoto().Wait(); CallGetCustomerPhoto().Wait(); //Test(); } else { //new ZipkinBootstrapper(name, System.Net.IPAddress.Parse(serverConfig.ServerInfo.Host), (short)serverConfig.ServerInfo.Port) // .ZipkinAt("192.168.87.131") // .WithSampleRate(1.0) // .Start(); } #endregion #region 关闭 System.Console.ReadLine(); nodeServer.StopAsync(); serviceProxyManager.CloseAsync(); #endregion System.Console.ReadLine(); }