예제 #1
0
        /// <summary>
        /// exec rpc
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        public async Task <IResult <T> > Invoke <T>(IInvocation invocation)
        {
            //cache
            var cache = this.GetCache(invocation);

            var parameters = invocation.MethodInfo.GetParameters();
            var key        = $"{invocation.AppPoint()}{invocation.TargetType.FullName}.{invocation.MethodInfo.Name}@{StringUtils.Md5(StringUtils.ToArgumentString(parameters, invocation.Arguments))}{invocation.PointVersion()}";

            if (cache != null)
            {
                var value = cache.Get <T>(key);
                if (value != null)
                {
                    Logger().LogInformation($"call from cache:{key}");
                    Logger().LogInformation($"cache type:{cache.GetType().FullName}");
                    return(new RpcResult <T>(value, 0));
                }

                var resultInner = await this.InvokeInner <T>(invocation);

                if (!resultInner.HasException)
                {
                    cache.Put(key, resultInner.Value);
                }
                return(resultInner);
            }

            var result = await this.InvokeInner <T>(invocation);

            return(result);
        }
예제 #2
0
        /// <summary>
        /// 根据配置获取路径集合
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>
        private IList <URL> GetUrls(IInvocation invocation)
        {
            //参数检查
            if (!Urls.ContainsKey(invocation.TargetType.FullName) && !BadUrls.ContainsKey(invocation.TargetType.FullName))
            {
                throw new Exception($"not find the {invocation.TargetType.FullName}'s urls,please config it ");
            }

            if (Urls.ContainsKey(invocation.TargetType.FullName) &&
                Urls?[invocation.TargetType.FullName]?.Count() > 0)
            {
                var result = filterUrls(invocation, Urls[invocation.TargetType.FullName]);
                if (result?.Count > 0)
                {
                    Logger().LogInformation("from good urls");
                    return(result);
                }
            }

            if (BadUrls.ContainsKey(invocation.TargetType.FullName) &&
                BadUrls?[invocation.TargetType.FullName]?.Count() > 0)
            {
                var result = filterUrls(invocation, BadUrls[invocation.TargetType.FullName].Select(w => w.Url).ToList());
                if (result?.Count > 0)
                {
                    Logger().LogInformation("from bad urls");
                    return(result);
                }
            }

            throw new Exception($"not find the {invocation.AppPoint()}{invocation.TargetType.FullName}{invocation.PointVersion()}'s urls,please config it,config version must <= Url version ");
        }
예제 #3
0
        /// <summary>
        /// 执行远程调用
        /// </summary>
        /// <param name="invocation"></param>
        /// <returns></returns>

        public IResult Invoke(IInvocation invocation)
        {
            //缓存
            var cache = this.GetCache(invocation);

            var parameters = invocation.MethodInfo.GetParameters();
            var key        = $"{invocation.AppPoint()}{invocation.TargetType.FullName}.{invocation.MethodInfo.Name}@{StringUtils.Md5(StringUtils.ToArgumentString(parameters, invocation.Arguments))}{invocation.PointVersion()}";

            if (cache != null)
            {
                var value = cache.Get(key);
                if (value != null)
                {
                    Console.WriteLine($"call from cache:{key}");
                    Console.WriteLine($"cache type:{cache.GetType().FullName}");
                    return(new RpcResult(value));
                }

                var resultInner = this.InvokeInner(invocation);
                if (!resultInner.HasException)
                {
                    cache.Put(key, resultInner.Value);
                }
                return(resultInner);
            }

            var result = this.InvokeInner(invocation);

            return(result);
        }
예제 #4
0
        /// <summary>
        /// 获取客户端缓存
        /// </summary>
        /// <param name="invocation">服务路径</param>
        /// <returns>客户端服务连接</returns>
        private ICache GetCache(IInvocation invocation)
        {
            //参数检查
            if (Caches == null)
            {
                return(null);
            }

            ICache result = null;
            //app interface version
            var methodParameter = $"{invocation.AppPoint()}{invocation.TargetType.FullName}.{invocation.MethodInfo.Name}{invocation.PointVersion()}";
            var key             = this.Address.GetMethodParameter(methodParameter, CACHE_KEY, "");

            //app interface
            if (string.IsNullOrEmpty(key))
            {
                key = this.Address.GetMethodParameter($"{invocation.AppPoint()}{invocation.TargetType.FullName}.{invocation.MethodInfo.Name}", CACHE_KEY, "");
            }
            //interface version
            if (string.IsNullOrEmpty(key))
            {
                key = this.Address.GetMethodParameter($"{invocation.TargetType.FullName}.{invocation.MethodInfo.Name}{invocation.PointVersion()}", CACHE_KEY, "");
            }
            //interface
            if (string.IsNullOrEmpty(key))
            {
                key = this.Address.GetMethodParameter($"{invocation.TargetType.FullName}.{invocation.MethodInfo.Name}", CACHE_KEY, "");
            }
            //key
            if (string.IsNullOrEmpty(key))
            {
                key = this.Address.GetParameter(CACHE_KEY, "");
            }

            if (key.ToLower() == "true" || key.ToLower() == LruCache.NAME)
            {
                result = Caches[LruCache.NAME];
            }

            if (Caches.ContainsKey(key) && key != LruCache.NAME)
            {
                result = Caches[key];
            }

            return(result);
        }