public void Run(ILoggerFactory loggerFactory, IServiceProxyManager serviceProxyManager, IContainer container) { loggerFactory.AddConsole(LogLevel.Information); var logger = loggerFactory.CreateLogger <XNodeBootstrap>(); var dir = Directory.GetCurrentDirectory(); var fileName = "xnode.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); #region Server XNode.Logging.LoggerManager.ServerLoggerFactory = loggerFactory; var loginValidator = new DefaultLoginValidator(configRoot.GetDefaultLoginValidatorConfig(), XNode.Logging.LoggerManager.ServerLoggerFactory); var serverConfig = configRoot.GetServerConfig(); var nodeServer = new NodeServerBuilder() .ApplyConfig(serverConfig) .ConfigSerializer(new ProtoBufSerializer(XNode.Logging.LoggerManager.ServerLoggerFactory)) .ConfigLoginValidator(loginValidator) .UseAutofac(container) .UseDotNetty(serverConfig.ServerInfo) .Build(); nodeServer.StartAsync().Wait(); #endregion #region Client var clientConfig = configRoot.GetClientConfig(); if (clientConfig.ServiceProxies == null || clientConfig.ServiceProxies.Count == 0) { return; } XNode.Logging.LoggerManager.ClientLoggerFactory = loggerFactory; var serializer = new ProtoBufSerializer(XNode.Logging.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().ContinueWith(task => { if (task.Exception != null) { if (task.Exception is AggregateException) { foreach (var e in task.Exception.InnerExceptions) { if (e is NetworkException netEx) { logger.LogError(netEx, $"XNode client connect has net error. Host={netEx.Host}, Port={netEx.Port}, Message={netEx.Message}"); } } } else { logger.LogError(task.Exception, $"XNode client connect has error. Message={task.Exception.Message}"); } throw task.Exception; } }); #endregion }
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(); }