public static IServiceHostBuilder UseClient(this IServiceHostBuilder hostBuilder) { return(hostBuilder.MapServices(mapper => { var serviceEntryManager = mapper.Resolve <IServiceEntryManager>(); var addressDescriptors = serviceEntryManager.GetEntries().Select(i => { i.Descriptor.Metadatas = null; return new ServiceSubscriber { Address = new[] { new IpAddressModel { Ip = Dns.GetHostEntry(Dns.GetHostName()) .AddressList.FirstOrDefault <IPAddress> (a => a.AddressFamily.ToString().Equals("InterNetwork")).ToString() } }, ServiceDescriptor = i.Descriptor }; }).ToList(); mapper.Resolve <IServiceSubscribeManager>().SetSubscribersAsync(addressDescriptors); mapper.Resolve <IModuleProvider>().Initialize(); })); }
public static IServiceHostBuilder UseServer(this IServiceHostBuilder hostBuilder, string ip, int port) { return(hostBuilder.MapServices(mapper => { mapper.Resolve <IServiceCommandManager>().SetServiceCommandsAsync(); var serviceEntryManager = mapper.Resolve <IServiceEntryManager>(); var addressDescriptors = serviceEntryManager.GetEntries().Select(i => new ServiceRoute { Address = new[] { new IpAddressModel { Ip = ip, Port = port } }, ServiceDescriptor = i.Descriptor }).ToList(); mapper.Resolve <IServiceRouteManager>().SetRoutesAsync(addressDescriptors); var serviceHost = mapper.Resolve <Runtime.Server.IServiceHost>(); Task.Factory.StartNew(async() => { await serviceHost.StartAsync(new IPEndPoint(IPAddress.Parse(ip), port)); }).Wait(); })); }
public static IServiceHostBuilder UseServer(this IServiceHostBuilder hostBuilder, string ip, int port, string token = "True") { return(hostBuilder.MapServices(mapper => { mapper.Resolve <IServiceCommandManager>().SetServiceCommandsAsync(); var serviceEntryManager = mapper.Resolve <IServiceEntryManager>(); bool enableToken; if (!bool.TryParse(token, out enableToken)) { string serviceToken = token; } else { if (enableToken) { token = Guid.NewGuid().ToString("N"); } else { token = null; } } var addressDescriptors = serviceEntryManager.GetEntries().Select(i => new ServiceRoute { Address = new[] { new IpAddressModel { Ip = ip, Port = port, Token = token } }, ServiceDescriptor = i.Descriptor }).ToList(); mapper.Resolve <IServiceRouteManager>().SetRoutesAsync(addressDescriptors); var serviceHost = mapper.Resolve <Runtime.Server.IServiceHost>(); Task.Factory.StartNew(async() => { await serviceHost.StartAsync(new IPEndPoint(IPAddress.Parse(ip), port)); }).Wait(); })); }
public static IServiceHostBuilder UseServer(this IServiceHostBuilder hostBuilder, string ip, int port, string token = "True") { return(hostBuilder.MapServices(mapper => { BuildServiceEngine(mapper); mapper.Resolve <IServiceCommandManager>().SetServiceCommandsAsync(); var serviceEntryManager = mapper.Resolve <IServiceEntryManager>(); string serviceToken = mapper.Resolve <IServiceTokenGenerator>().GeneratorToken(token); int _port = AppConfig.ServerOptions.Port == 0 ? port : AppConfig.ServerOptions.Port; string _ip = AppConfig.ServerOptions.Ip ?? ip; _port = AppConfig.ServerOptions.IpEndpoint?.Port ?? _port; _ip = AppConfig.ServerOptions.IpEndpoint?.Address.ToString() ?? _ip; if (_ip.IndexOf(".") < 0 || _ip == "" || _ip == "0.0.0.0") { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && (_ip == "" || _ip == "0.0.0.0" || _ip == adapter.Name)) { IPInterfaceProperties ipxx = adapter.GetIPProperties(); UnicastIPAddressInformationCollection ipCollection = ipxx.UnicastAddresses; foreach (UnicastIPAddressInformation ipadd in ipCollection) { if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { _ip = ipadd.Address.ToString(); } } } } } var mappingIp = AppConfig.ServerOptions.MappingIP ?? _ip; var mappingPort = AppConfig.ServerOptions.MappingPort; if (mappingPort == 0) { mappingPort = _port; } if (AppConfig.ServerOptions.Protocol == CommunicationProtocol.Tcp || AppConfig.ServerOptions.Protocol == CommunicationProtocol.None) { new ServiceRouteWatch(mapper.Resolve <CPlatformContainer>(), () => { var addressDescriptors = serviceEntryManager.GetEntries().Select(i => { i.Descriptor.Token = serviceToken; return new ServiceRoute { Address = new[] { new IpAddressModel { Ip = mappingIp, Port = mappingPort, ProcessorTime = Math.Round(Convert.ToDecimal(Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds), 2, MidpointRounding.AwayFromZero), } }, ServiceDescriptor = i.Descriptor }; }).ToList(); mapper.Resolve <IServiceRouteManager>().SetRoutesAsync(addressDescriptors); }); } mapper.Resolve <IModuleProvider>().Initialize(); var serviceHosts = mapper.Resolve <IList <Runtime.Server.IServiceHost> >(); Task.Factory.StartNew(async() => { foreach (var serviceHost in serviceHosts) { await serviceHost.StartAsync(_ip, _port); } mapper.Resolve <IServiceEngineLifetime>().NotifyStarted(); }).Wait(); })); }
/// <summary> /// 指定宿主机环境 /// </summary> /// <param name="hostBuilder"></param> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="token"></param> /// <returns></returns> public static IServiceHostBuilder UseServer(this IServiceHostBuilder hostBuilder, string address, int port, string token = "True") { return(hostBuilder.MapServices(mapper => { BuildServiceEngine(mapper); mapper.Resolve <IServiceCommandManager>().SetServiceCommandsAsync(); var serviceToken = mapper.Resolve <IServiceTokenGenerator>().GeneratorToken(token); var _port = AppConfig.ServerOptions.Port == 0 ? port : AppConfig.ServerOptions.Port; var _address = AppConfig.ServerOptions.Ip ?? address; var _ip = AddressHelper.GetIpFromAddress(_address); _port = AppConfig.ServerOptions.IpEndpoint?.Port ?? _port; _ip = AppConfig.ServerOptions.IpEndpoint?.Address.ToString() ?? _ip; if (_ip.IndexOf(".") < 0 || _ip == "" || _ip == "0.0.0.0") { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && (_ip == "" || _ip == "0.0.0.0" || _ip == adapter.Name)) { IPInterfaceProperties ipxx = adapter.GetIPProperties(); UnicastIPAddressInformationCollection ipCollection = ipxx.UnicastAddresses; foreach (UnicastIPAddressInformation ipadd in ipCollection) { if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { _ip = ipadd.Address.ToString(); } } } } } var mappingIp = AppConfig.ServerOptions.MappingIP ?? _ip; var mappingPort = AppConfig.ServerOptions.MappingPort; if (mappingPort == 0) { mappingPort = _port; } if (string.IsNullOrEmpty(AppConfig.ServerOptions.MappingIP)) { ConfigureRoute(mapper, _ip, _port, serviceToken); } else { ConfigureRoute(mapper, _ip, _port, mappingIp, mappingPort, serviceToken); } //初始化模块 mapper.Resolve <IModuleProvider>().Initialize(); //获取微服务主机(可能存在多个运行时主机,取决于微服务依赖的模块) var serviceHosts = mapper.Resolve <IList <Runtime.Server.IServiceHost> >(); Task.Factory.StartNew(async() => { foreach (var serviceHost in serviceHosts) { await serviceHost.StartAsync(_ip, _port); } mapper.Resolve <IServiceEngineLifetime>().NotifyStarted(); }).Wait(); })); }
public static IServiceHostBuilder UseServer(this IServiceHostBuilder hostBuilder, string ip, int port, string token = "True") { return(hostBuilder.MapServices(mapper => { mapper.Resolve <IServiceCommandManager>().SetServiceCommandsAsync(); var serviceEntryManager = mapper.Resolve <IServiceEntryManager>(); bool enableToken; string serviceToken; int _port = port; string _ip = ip; _port = AppConfig.ServerOptions.IpEndpoint?.Port ?? _port; _ip = AppConfig.ServerOptions.IpEndpoint?.Address.ToString() ?? _ip; if (_ip.IndexOf(".") < 0 || _ip == "" || _ip == "0.0.0.0") { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && (_ip == "" || _ip == "0.0.0.0" || _ip == adapter.Name)) { IPInterfaceProperties ipxx = adapter.GetIPProperties(); UnicastIPAddressInformationCollection ipCollection = ipxx.UnicastAddresses; foreach (UnicastIPAddressInformation ipadd in ipCollection) { if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { _ip = ipadd.Address.ToString(); } } } } } if (!bool.TryParse(token, out enableToken)) { serviceToken = token; } else { if (enableToken) { serviceToken = Guid.NewGuid().ToString("N"); } else { serviceToken = null; } } var addressDescriptors = serviceEntryManager.GetEntries().Select(i => new ServiceRoute { Address = new[] { new IpAddressModel { Ip = _ip, Port = _port, Token = serviceToken } }, ServiceDescriptor = i.Descriptor }).ToList(); mapper.Resolve <IServiceRouteManager>().SetRoutesAsync(addressDescriptors); mapper.Resolve <IModuleProvider>().Initialize(); var serviceHost = mapper.Resolve <Runtime.Server.IServiceHost>(); Task.Factory.StartNew(async() => { await serviceHost.StartAsync(new IPEndPoint(IPAddress.Parse(_ip), _port)); }).Wait(); })); }
public static IServiceHostBuilder UseClient(this IServiceHostBuilder hostBuilder) { return(hostBuilder.MapServices(mapper => { })); }