public override Task <Response> Call(Request request, ServerCallContext context) { try { var methodType = MicroServiceManage.GetMethodType(request.ServiceName); var serverType = MicroServiceManage.GetServiceTypeByMethodKey(request.ServiceName); var parameters = methodType.GetParameters(); var values = request.Data.Split('兲'); var args = new List <object>(); var i = 0; foreach (var parameter in parameters) { var parameterType = parameter.ParameterType; var value = JsonHelper.DeserializeJsonToObject(values[i], parameterType); args.Add(value); i++; } var instance = Activator.CreateInstance(serverType); //var args = JsonHelper.DeserializeJsonToObject<object[]>(request.Data); var result = methodType.Invoke(instance, args.ToArray()); var resultJson = JsonConvert.SerializeObject(result); return(Task.FromResult(new Response { Code = "200", Data = resultJson })); } catch (System.Exception ex) { return(Task.FromResult(new Response { Code = "500", Data = "", Message = ex.ToString() })); } }
protected override object Invoke(MethodInfo targetMethod, object[] args) { object result = null; var needTry = false; var tryCount = 0; do { try { var serviceKey = MicroServiceManage.BuildServiceKey(type); var methodKey = MicroServiceManage.BuildMethodKey(serviceKey, targetMethod); var response = MicroServiceManage.Call(serviceKey, methodKey, args); if (targetMethod.ReturnType == typeof(void)) { return(""); } if (response.Code == "200") { result = JsonHelper.DeserializeJsonToObject(response.Data, targetMethod.ReturnType); } else if (response.Code == "500") { throw new Exception(response.Message); } needTry = false; } catch (System.Exception ex) { System.Console.WriteLine(ex); needTry = true; if (tryCount == 3) { throw ex; } } tryCount++; Thread.Sleep(5000); }while (needTry && tryCount < 3); return(result); }
static ConsulHelper() { HealthPort = MicroServiceManage.GetPort(); Task.Run(() => { string host = MicroServiceManage.GetIpAddress(); IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, HealthPort); Socket sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sSocket.Bind(ipe); sSocket.Listen(0); while (true) { try { var send = sSocket.Accept(); var sendIpoint = send.RemoteEndPoint.ToString(); //Console.WriteLine($"{sendIpoint}Connection"); } catch (Exception ex) { System.Console.WriteLine(ex); } } }); ConsulClientInstance = new ConsulClient(); Task.Run(() => { while (true) { try { var services = ConsulClientInstance.Agent.Services(); services.Wait(); var agentService1 = services.GetAwaiter().GetResult().Response; //foreach (var agentService in AgentServices) foreach (var agentService in agentService1.Values) { var checks = ConsulClientInstance.Health.Checks(agentService.Service); checks.Wait(); var dicChecks = checks.GetAwaiter().GetResult().Response; var unHealthChecks = dicChecks.Where(x => x.Status.Status != "passing").ToList(); foreach (var unHealthCheck in unHealthChecks) { ConsulClientInstance.Catalog.Deregister(new CatalogDeregistration { ServiceID = unHealthCheck.ServiceID, Node = unHealthCheck.Node }); } } } catch (System.Exception ex) { System.Console.WriteLine(ex); } Thread.Sleep(5000); } }); }