基于.net core微服务的另一种实现方法(2)

[Route("api/svc/{interfaceId}/{methodId}"), Produces("application/json")] public async Task<ApiActionResult> Process(string interfaceId, string methodId) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); ApiActionResult result = null; string reqParam = string.Empty; try { using (var reader = new StreamReader(Request.Body, Encoding.UTF8)) { reqParam = await reader.ReadToEndAsync(); } AppRuntimes.Instance.Loger.Debug($"recive client request:api/svc/{interfaceId}/{methodId},data:{reqParam}"); ArrayList param = null; if (!string.IsNullOrWhiteSpace(reqParam)) { //解析参数 param = JsonConvert.DeserializeObject<ArrayList>(reqParam); } //转交本地服务处理中心处理 var data = LocalServiceExector.Exec(interfaceId, methodId, param); result = ApiActionResult.Success(data); } catch BusinessException ex) //业务异常 { result = ApiActionResult.Error(ex.Message); } catch (Exception ex) { //业务异常 if (ex.InnerException is BusinessException) { result = ApiActionResult.Error(ex.InnerException.Message); } else { AppRuntimes.Instance.Loger.Error($"调用服务发生异常{interfaceId}-{methodId},data:{reqParam}", ex); result = ApiActionResult.Fail("服务发生异常"); } } finally { stopwatch.Stop(); AppRuntimes.Instance.Loger.Debug($"process client request end:api/svc/{interfaceId}/{methodId},耗时[ {stopwatch.ElapsedMilliseconds} ]毫秒"); } //result.Message = AppRuntimes.Instance.GetCfgVal("ServerName") + " " + result.Message; result.Message = result.Message; return result; }

本地服务中心通过接口名和方法名,找出具体的实现类的方法,并使用传递的参数执行,ps:因为涉及到反射获取具体的方法,暂不支持相同参数个数的方法重载.仅支持不同参数个数的方法重载.

public static object Exec(string interfaceId, string methodId, ArrayList param) { var svcMethodInfo = GetInstanceAndMethod(interfaceId, methodId, param.Count); var currentMethodParameters = new ArrayList(); for (var i = 0; i < svcMethodInfo.Paramters.Length; i++) { var tempParamter = svcMethodInfo.Paramters[i]; if (param[i] == null) { currentMethodParameters.Add(null); } else { if (!tempParamter.ParameterType.Namespace.Equals("System") || tempParamter.ParameterType.Name == "Byte[]") { currentMethodParameters.Add(JsonConvert.DeserializeObject(Convert.ToString(param[i]), tempParamter.ParameterType) } else { currentMethodParameters.Add(TypeConvertUtil.BasicTypeConvert(tempParamter.ParameterType, param[i])); } } } return svcMethodInfo.Invoke(currentMethodParameters.ToArray()); } private static InstanceMethodInfo GetInstanceAndMethod(string interfaceId, string methodId, int paramCount) { var methodKey = $"{interfaceId}_{methodId}_{paramCount}"; if (methodCache.ContainsKey(methodKey)) { return methodCache[methodKey]; } InstanceMethodInfo temp = null; var svcType = ServiceFactory.GetSvcType(interfaceId, true); if (svcType == null) { throw new ICVIPException($"找不到API接口的服务实现:{interfaceId}"); } var methods = svcType.GetMethods().Where(t => t.Name == methodId).ToList(); if (methods.IsNullEmpty()) { throw new BusinessException($"在API接口[{interfaceId}]的服务实现中[{svcType.FullName}]找不到指定的方法:{methodId}"); } var method = methods.FirstOrDefault(t => t.GetParameters().Length == paramCount); if (method == null) { throw new ICVIPException($"在API接口中[{interfaceId}]的服务实现[{svcType.FullName}]中,方法[{methodId}]参数个数不匹配"); } var paramtersTypes = method.GetParameters(); object instance = null; try { instance = Activator.CreateInstance(svcType); } catch (Exception ex) { throw new BusinessException($"在实例化服务[{svcType}]发生异常,请确认其是否包含一个无参的构造函数", ex); } temp = new InstanceMethodInfo() { Instance = instance, InstanceType = svcType, Key = methodKey, Method = method, Paramters = paramtersTypes }; if (!methodCache.ContainsKey(methodKey)) { lock (_syncAddMethodCacheLocker) { if (!methodCache.ContainsKey(methodKey)) { methodCache.Add(methodKey, temp); } } } return temp;

服务配置,指示具体的服务的远程地址,当未配置的服务默认为本地服务.

[ { "ServiceId": "XZL.Api.IOrderService", "Address": "http://localhost:8801/api/svc" }, { "ServiceId": "XZL.Api.IUserService", "Address": "http://localhost:8802/api/svc" } ]

AppRuntime.Instance.GetService<TService>()的实现.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsxdf.html