static void Main(string[] args) { Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; //配置服务端日志工厂 LoggerManager.ServerLoggerFactory.AddConsole(LogLevel.Error); //加载配置文件 string path = Path.Combine(Directory.GetCurrentDirectory(), "config.json"); var configRoot = new ConfigurationBuilder() .AddJsonFile(path) .Build(); var serverConfig = configRoot.GetServerConfig(); var loginValidator = new DefaultLoginValidator(configRoot.GetDefaultLoginValidatorConfig(), LoggerManager.ServerLoggerFactory); //创建LoginValidator实例 //配置服务 var nodeServer = new NodeServerBuilder() .ApplyConfig(serverConfig) .ConfigSerializer(new ProtoBufSerializer(LoggerManager.ServerLoggerFactory)) .ConfigLoginValidator(loginValidator) //配置LoginValidator .ConfigServiceProvider(GetServiceProvider()) .UseDotNetty(serverConfig.ServerInfo) .Build(); //启动服务 nodeServer.StartAsync().Wait(); Console.ReadLine(); //关闭服务 nodeServer.StopAsync(); }
private static INodeServer StartServer(IConfigurationRoot configRoot, IContainer container) { var serviceAuthorizer = new DefaultServiceAuthorizer(configRoot.GetDefaultServiceAuthorizeConfig(), LoggerManager.ServerLoggerFactory); var loginValidator = new DefaultLoginValidator(configRoot.GetDefaultLoginValidatorConfig(), LoggerManager.ServerLoggerFactory); var serverConfig = configRoot.GetServerConfig(); var nodeServer = new NodeServerBuilder() .ApplyConfig(serverConfig) //.ConfigSerializer(new MsgPackSerializer(LoggerManager.ServerLoggerFactory)) .ConfigSerializer(new ProtoBufSerializer(LoggerManager.ServerLoggerFactory)) .ConfigLoginValidator(loginValidator) .AddServiceProcessor(new ServiceAuthorizeProcessor(serviceAuthorizer)) .UseDotNetty(serverConfig.ServerInfo) .UseAutofac(container) .Build(); nodeServer.OnStarted += (arg) => { System.Console.WriteLine("Server started:"); System.Console.WriteLine($"Host={arg.Host}, Port={arg.Port}"); var list = arg.Routes.Where(r => r.Enabled).Select(r => new { r.ServiceName }).Distinct(); foreach (var item in list) { System.Console.WriteLine(item.ServiceName); } }; nodeServer.OnStopped += (arg) => { System.Console.WriteLine("Server stopped:"); System.Console.WriteLine($"Host={arg.Host}, Port={arg.Port}"); var list = arg.Routes.Where(r => r.Enabled).Select(r => new { r.ServiceName }).Distinct(); foreach (var item in list) { System.Console.WriteLine(item.ServiceName); } }; nodeServer.StartAsync().Wait(); return(nodeServer); }
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 }
public static void Test(string host, int port) { LoggerManager.ServerLoggerFactory.AddConsole(LogLevel.Information); string path = Path.Combine(Directory.GetCurrentDirectory(), "config.json"); var configRoot = new ConfigurationBuilder() .AddJsonFile(path) .Build(); var serviceAuthorizer = new DefaultServiceAuthorizer(configRoot.GetDefaultServiceAuthorizeConfig(), LoggerManager.ServerLoggerFactory); var loginValidator = new DefaultLoginValidator(configRoot.GetDefaultLoginValidatorConfig(), LoggerManager.ServerLoggerFactory); IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory()); ChangeToken.OnChange(() => fileProvider.Watch("config.json"), () => { configRoot.Reload(); serviceAuthorizer.LoadConfig(configRoot.GetDefaultServiceAuthorizeConfig()); loginValidator.LoadConfig(configRoot.GetDefaultLoginValidatorConfig()); }); new ZipkinBootstrapper("XNodeDemoServer", System.Net.IPAddress.Parse(host), (short)port) .ZipkinAt("192.168.87.131") .WithSampleRate(1.0) .Start(); //var serviceProvider = CreateDefaultServiceProvider(); //var serviceProvider = CreateAutofacServiceProvider(); //serviceProvider.LoggerFactory = LoggerManager.ServerLoggerFactory; var serverConfig = configRoot.GetServerConfig(); var container = GetAutofacContainer(); //var serializer = new MsgPackSerializer(LoggerManager.ServerLoggerFactory); var serializer = new ProtoBufSerializer(LoggerManager.ServerLoggerFactory); var nodeServer = new NodeServerBuilder() //.ConfigServerInfo(host, port) .ApplyConfig(serverConfig) //.ConfigServiceProvider(serviceProvider) .ConfigSerializer(serializer) .ConfigLoginValidator(loginValidator) .AddServiceProcessor(new ZipkinProcessor()) .AddServiceProcessor(new ServiceAuthorizeProcessor(serviceAuthorizer)) .UseDotNetty(serverConfig.ServerInfo) .UseAutofac(container) .Build(); //UseServicePublish(configRoot, serializer, nodeServer); nodeServer.OnStarted += (arg) => { System.Console.WriteLine("Server started:"); System.Console.WriteLine($"Host={arg.Host}, Port={arg.Port}"); var list = arg.Routes.Where(r => r.Enabled).Select(r => new { r.ServiceName }).Distinct(); foreach (var item in list) { System.Console.WriteLine(item.ServiceName); } //nodeServer.Disable(10002); //nodeServer.Enable(10002); }; nodeServer.OnStopped += (arg) => { System.Console.WriteLine("Server stopped:"); System.Console.WriteLine($"Host={arg.Host}, Port={arg.Port}"); var list = arg.Routes.Where(r => r.Enabled).Select(r => new { r.ServiceName }).Distinct(); foreach (var item in list) { System.Console.WriteLine(item.ServiceName); } }; nodeServer.StartAsync().Wait(); System.Console.ReadLine(); nodeServer.StopAsync().Wait(); System.Console.WriteLine("Stop"); }
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(); }