static void Main(string[] args) { while (true) { try { NetClient client = new NetClient("127.0.0.1", 8911); client.Dispose(); break; } catch (Exception) { Thread.Sleep(100); } } var gatewaycert = new System.Security.Cryptography.X509Certificates.X509Certificate2("../../../../pfx/client.pfx", "123456"); ServiceCollection services = new ServiceCollection(); var gateways = new NetAddress[] { new NetAddress { Address = "localhost", Port = 8911 } }; var msp = new MicroServiceHost(services); var builder = new ConfigurationBuilder(); builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false); var configuration = builder.Build(); //services.UseJmsTokenAuthentication(AuthorizationContentType.String, "127.0.0.1", 9911,"auth"); services.AddLogging(loggingBuilder => { loggingBuilder.AddConfiguration(configuration.GetSection("Logging")); loggingBuilder.AddConsole(); // 将日志输出到控制台 }); services.AddSingleton <IConfiguration>(configuration); msp.Register <Controller1>("Controller1"); msp.Register <Controller2>("Service2"); msp.RegisterScheduleTask <AutoRun1>(); msp.ServiceProviderBuilded += Msp_ServiceProviderBuilded; msp.Build(8912, gateways) .UseSSL(c => { //配置ssl c.GatewayClientCertificate = gatewaycert; c.ServerCertificate = new X509Certificate2("../../../../pfx/service_server.pfx", "123456"); }) .Run(); }
/// <summary> /// /// </summary> /// <param name="proxyAddr">代理地址,如果为null,则直接访问目标服务器</param> /// <param name="targetAddr">目标服务器地址</param> /// <param name="cert">访问代理服务器的客户端证书</param> public ProxyClient(NetAddress proxyAddr, NetAddress targetAddr, X509Certificate2 cert) : base(proxyAddr == null ? targetAddr:proxyAddr, cert) { this.ProxyAddress = proxyAddr; this.Address = targetAddr.Address; this.Port = targetAddr.Port; if (proxyAddr != null) { this.WriteServiceData(targetAddr); } }
public static NetClient CreateClient(NetAddress proxy, NetAddress addr, X509Certificate2 cert) { var key = $"{addr.Address}--{addr.Port}"; NetClientSeat[] array; if (Dict.TryGetValue(key, out array) == false) { Dict.TryAdd(key, GetArray()); array = Dict[key]; } var freeitem = GetFree(array); if (freeitem == null) { freeitem = new ProxyClient(proxy, addr, cert); freeitem.KeepAlive = array.Any(m => m.Client == null); } return(freeitem); }
public GatewayRefereeClient(IConfiguration configuration, LockKeyManager lockKeyManager, Gateway gateway, ILogger <GatewayRefereeClient> logger) { _refereeAddress = configuration.GetSection("Cluster:Referee").Get <NetAddress>(); _logger = logger; _lockKeyManager = lockKeyManager; _gateway = gateway; if (_refereeAddress == null) { _lockKeyManager.IsReady = true; this.IsMaster = true; } else { new Thread(toBeMaster).Start(); } SystemEventCenter.MicroServiceUploadLockedKeyCompleted += SystemEventCenter_MicroServiceUploadLockedKeyCompleted; }
public ShareFileClient(NetAddress gatewayAddress, ILogger logger = null, X509Certificate2 gatewayClientCert = null) { this._gatewayAddress = gatewayAddress; this._gatewayClientCert = gatewayClientCert; this._logger = logger; }
public override bool Equals(object obj) { NetAddress compaire = (NetAddress)obj; return(this.Equals(compaire.Address, compaire.Port)); }
public static NetClient CreateClient(NetAddress proxy, string ip, int port, X509Certificate2 cert) { return(CreateClient(proxy, new NetAddress(ip, port), cert)); }
public NetClient(NetAddress addr) : base(addr.Address, addr.Port) { this.Address = addr.Address; this.Port = addr.Port; this.ReadTimeout = 16000; }
static void Main(string[] args) { while (true) { try { NetClient client = new NetClient("127.0.0.1", 8911); client.Dispose(); break; } catch (Exception) { Thread.Sleep(100); } } ServiceCollection services = new ServiceCollection(); var gateways = new NetAddress[] { new NetAddress { Address = "localhost", Port = 8911 } }; var msp = new MicroServiceHost(services); var builder = new ConfigurationBuilder(); builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false); var configuration = builder.Build(); //services.UseJmsTokenAuthentication(AuthorizationContentType.String, "127.0.0.1", 9911,"auth"); services.AddLogging(loggingBuilder => { loggingBuilder.AddConfiguration(configuration.GetSection("Logging")); loggingBuilder.AddConsole(); // 将日志输出到控制台 }); services.AddSingleton <IConfiguration>(configuration); msp.ClientCheckCode = @" try { var number = Convert.ToInt64(arg); return number > 0; } catch { } return false; "; msp.Register <Controller1>("Controller1"); msp.Register <Controller2>("Service2"); msp.RegisterScheduleTask <AutoRun1>(); msp.ServiceProviderBuilded += Msp_ServiceProviderBuilded; msp.Build(8912, gateways) .Run(); }
public CertClient(NetAddress addr, X509Certificate2 cert) : base(addr) { loadCert(cert); }
void toBeMaster() { /* * 申请成为master --> * 成为master --> 维持心跳 --> 心跳断开后 -->重新连接裁判机申请成为master * -->还是master ... * -->如果不再是master --> 断开连接的所有微服务 * 不能成为master --> 和master维持心跳 --> 心跳断开后 -->重新连接裁判机申请成为master */ NetAddress masterAddr = null; while (true) { try { if (!this.IsMaster) { _lockKeyManager.IsReady = false; } using (var client = new NetClient(_refereeAddress)) { client.WriteServiceData(new GatewayCommand { Type = CommandType.ApplyToBeMaster, Content = _gateway.Port.ToString() }); var ret = client.ReadServiceObject <InvokeResult <string> >(); _logger?.LogInformation("与裁判连接成功,裁判返回数据:{0}", ret.Data); if (ret.Success) { masterAddr = null; if (this.IsMaster == false) { _logger?.LogInformation("成为主网关"); _waitServiceList = ret.Data.FromJson <ConcurrentDictionary <string, RegisterServiceLocation> >(); this.IsMaster = true; //等待所有微服务上传locked key var timeout = _lockKeyManager.KeyTimeout / 1000; for (int i = 0; i < timeout && _waitServiceList.Count > 0; i++) { _logger?.LogInformation("还有{0}个微服务没有报到 {1}", _waitServiceList.Count, _waitServiceList.Keys.ToArray().ToJsonString()); Thread.Sleep(1000); } _lockKeyManager.IsReady = true; if (_waitServiceList.Count > 0) { _logger?.LogInformation("还有{0}个微服务没有报到,但被忽略了。", _waitServiceList.Count); } _logger?.LogInformation("lockKeyManager就绪"); _logger?.LogDebug("锁记录:{0}", _lockKeyManager.GetCaches().ToJsonString()); } client.KeepHeartBeating(); _logger?.LogInformation("与裁判的连接断开"); } else { if (this.IsMaster) { this.IsMaster = false; _lockKeyManager.IsReady = false; //不是主网关,需要断开所有微服务 var allservices = _gateway.OnlineMicroServices.ToArray(); foreach (var s in allservices) { s.Close(); } } //另一个网关成为主网关 masterAddr = ret.Data.FromJson <NetAddress>(); } } if (masterAddr != null) { _logger?.LogInformation("准备和主网关连接{0}", masterAddr.ToJsonString()); //连上主网关,直到连接出现问题,再申请成为主网关 using (var client = new NetClient(masterAddr)) { _logger?.LogInformation("与主网关连接心跳"); client.KeepHeartBeating(); Thread.Sleep(100); _logger?.LogInformation("与主网关连接断开"); } } } catch (SocketException) { Thread.Sleep(2000); } catch (Exception ex) { Thread.Sleep(2000); _logger?.LogError(ex, ex.Message); } } }
/// <summary> /// 获取网关共享文件,并保存到本地 /// </summary> /// <param name="gatewayAddress">包含共享文件的网关地址</param> /// <param name="filepath">共享文件路径</param> /// <param name="localFilePath">保存到本地的路径</param> /// <param name="gatewayClientCert">网关客户端证书</param> public void GetGatewayShareFile(NetAddress gatewayAddress, string filepath, string localFilePath, X509Certificate2 gatewayClientCert = null) { _mapFileManager.GetGatewayShareFile(gatewayAddress, filepath, localFilePath, gatewayClientCert); }
/// <summary> /// 映射网关上的共享文件到本地 /// </summary> /// <param name="gatewayAddress">包含共享文件的网关地址</param> /// <param name="shareFilePath">共享文件路径</param> /// <param name="localFilePath">映射本地的路径</param> /// <param name="callback">文件写入本地后,回调委托</param> public void MapShareFileToLocal(NetAddress gatewayAddress, string shareFilePath, string localFilePath, Action <string, string> callback = null) { _mapFileManager.MapShareFileToLocal(gatewayAddress, shareFilePath, localFilePath, callback); }
static void Main(string[] args) { while (true) { try { NetClient client = new NetClient("127.0.0.1", 8911); client.Dispose(); break; } catch (Exception) { Thread.Sleep(100); } } var gatewaycert = new System.Security.Cryptography.X509Certificates.X509Certificate2("../../../../pfx/client.pfx", "123456"); ServiceCollection services = new ServiceCollection(); var gateways = new NetAddress[] { new NetAddress { Address = "localhost", Port = 8911 } }; var msp = new MicroServiceHost(services); if (File.Exists("./appsettings.json") == false) { //本地没有appsettings.json,先从网关拉一个 msp.GetGatewayShareFile(gateways[0], "test/appsettings.json", "./appsettings.json", gatewaycert); } var builder = new ConfigurationBuilder(); builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false); var configuration = builder.Build(); services.AddLogging(loggingBuilder => { loggingBuilder.AddConfiguration(configuration.GetSection("Logging")); loggingBuilder.AddConsole(); // 将日志输出到控制台 }); services.AddSingleton <IConfiguration>(configuration); msp.MapShareFileToLocal(gateways[0], "test/appsettings.json", "./appsettings.json", (p, p2) => { Console.WriteLine(p + "回调"); }); msp.MapShareFileToLocal(gateways[0], "test/appsettings2.json", "./appsettings2.json"); msp.Register <Controller1>("Controller1"); msp.Register <Controller2>("Service2"); msp.RegisterScheduleTask <AutoRun>(); msp.Build(8912, gateways) .UseSSL(c => { //配置ssl c.GatewayClientCertificate = gatewaycert; c.ServerCertificate = new X509Certificate2("../../../../pfx/service_server.pfx", "123456"); }) .Run(); }