From c5b487ba81ad16eb6a39d7440fcbcf30e4e4e552 Mon Sep 17 00:00:00 2001 From: fanliang11 <137629448@qq.com> Date: Mon, 31 Dec 2018 21:23:19 +0800 Subject: [PATCH] optimization code --- .../ContainerBuilderExtensions.cs | 60 +++++++++++-------- .../Implementation/DefaultAddressResolver.cs | 13 ++-- .../HashAlgorithms/ConsistentHash.cs | 43 ++++--------- .../RedisCache/RedisContext.cs | 5 +- 4 files changed, 58 insertions(+), 63 deletions(-) diff --git a/src/Surging.Core/Surging.Core.CPlatform/ContainerBuilderExtensions.cs b/src/Surging.Core/Surging.Core.CPlatform/ContainerBuilderExtensions.cs index 8df3e89c4..3804f41cc 100644 --- a/src/Surging.Core/Surging.Core.CPlatform/ContainerBuilderExtensions.cs +++ b/src/Surging.Core/Surging.Core.CPlatform/ContainerBuilderExtensions.cs @@ -87,6 +87,7 @@ public static class ContainerBuilderExtensions { private static List _referenceAssembly = new List(); private static List _modules = new List(); + /// /// 添加Json序列化支持。 /// @@ -486,7 +487,7 @@ public static IServiceBuilder RegisterServices(this IServiceBuilder builder, par try { var services = builder.Services; - var referenceAssemblies = GetReferenceAssembly(virtualPaths); + var referenceAssemblies = GetAssemblies(virtualPaths); foreach (var assembly in referenceAssemblies) { services.RegisterAssemblyTypes(assembly) @@ -527,7 +528,7 @@ public static IServiceBuilder RegisterServiceBus (this IServiceBuilder builder, params string[] virtualPaths) { var services = builder.Services; - var referenceAssemblies = GetReferenceAssembly(virtualPaths); + var referenceAssemblies = GetAssemblies(virtualPaths); foreach (var assembly in referenceAssemblies) { @@ -548,7 +549,7 @@ public static IServiceBuilder RegisterRepositories (this IServiceBuilder builder, params string[] virtualPaths) { var services = builder.Services; - var referenceAssemblies = GetReferenceAssembly(virtualPaths); + var referenceAssemblies = GetAssemblies(virtualPaths); foreach (var assembly in referenceAssemblies) { @@ -562,19 +563,7 @@ public static IServiceBuilder RegisterModules( this IServiceBuilder builder, params string[] virtualPaths) { var services = builder.Services; - List referenceAssemblies = new List(); - if (virtualPaths.Any()) - { - referenceAssemblies = GetReferenceAssembly(virtualPaths); - } - else - { - string[] assemblyNames = DependencyContext - .Default.GetDefaultAssemblyNames().Select(p => p.Name).ToArray(); - assemblyNames = GetFilterAssemblies(assemblyNames); - foreach (var name in assemblyNames) - referenceAssemblies.Add(Assembly.Load(name)); - } + var referenceAssemblies = GetAssemblies(virtualPaths); if (builder == null) throw new ArgumentNullException("builder"); var packages = ConvertDictionary(AppConfig.ServerOptions.Packages); foreach (var moduleAssembly in referenceAssemblies) @@ -614,11 +603,11 @@ public static IEnumerable GetDataContractName(this IServiceBuilder build { var namespaces = new List(); var assemblies = builder.GetInterfaceService() - .Select(p=>p.Assembly) + .Select(p => p.Assembly) .Union(GetSystemModules()) .Distinct() .ToList(); - + assemblies.ForEach(assembly => { namespaces.AddRange(assembly.GetTypes().Where(t => t.GetCustomAttribute() != null).Select(n => n.Namespace)); @@ -628,7 +617,7 @@ public static IEnumerable GetDataContractName(this IServiceBuilder build private static IDictionary ConvertDictionary(List list) { - var result = new Dictionary(); + var result = new Dictionary(); list.ForEach(p => { result.Add(p.TypeName, p.Using); @@ -651,7 +640,7 @@ private static List GetReferenceAssembly(params string[] virtualPaths) paths.ForEach(path => { var assemblyFiles = GetAllAssemblyFiles(path); - + foreach (var referencedAssemblyFile in assemblyFiles) { var referencedAssembly = Assembly.LoadFrom(referencedAssemblyFile); @@ -659,7 +648,7 @@ private static List GetReferenceAssembly(params string[] virtualPaths) _referenceAssembly.Add(referencedAssembly); refAssemblies.Add(referencedAssembly); } - result = existsPath ? refAssemblies: _referenceAssembly; + result = existsPath ? refAssemblies : _referenceAssembly; }); } return result; @@ -672,7 +661,7 @@ private static List GetSystemModules() foreach (var referenceAssembly in referenceAssemblies) { var abstractModules = GetAbstractModules(referenceAssembly); - if(abstractModules.Any(p =>p.GetType().IsSubclassOf(typeof(SystemModule)))) + if (abstractModules.Any(p => p.GetType().IsSubclassOf(typeof(SystemModule)))) { assemblies.Add(referenceAssembly); } @@ -680,6 +669,25 @@ private static List GetSystemModules() return assemblies; } + private static List GetAssemblies(params string[] virtualPaths) + { + var referenceAssemblies = new List(); + if (virtualPaths.Any()) + { + referenceAssemblies = GetReferenceAssembly(virtualPaths); + } + else + { + string[] assemblyNames = DependencyContext + .Default.GetDefaultAssemblyNames().Select(p => p.Name).ToArray(); + assemblyNames = GetFilterAssemblies(assemblyNames); + foreach (var name in assemblyNames) + referenceAssemblies.Add(Assembly.Load(name)); + _referenceAssembly.AddRange(referenceAssemblies.Except(_referenceAssembly)); + } + return referenceAssemblies; + } + private static List GetAbstractModules(Assembly assembly) { var abstractModules = new List(); @@ -694,9 +702,9 @@ private static List GetAbstractModules(Assembly assembly) return abstractModules; } - private static string[] GetFilterAssemblies(string[] assemblyNames) + private static string[] GetFilterAssemblies(string[] assemblyNames) { - var notRelatedFile = AppConfig.ServerOptions.NotRelatedAssemblyFiles; + var notRelatedFile = AppConfig.ServerOptions.NotRelatedAssemblyFiles; var relatedFile = AppConfig.ServerOptions.RelatedAssemblyFiles; var pattern = string.Format("^Microsoft.\\w*|^System.\\w*|^DotNetty.\\w*|^runtime.\\w*|^ZooKeeperNetEx\\w*|^StackExchange.Redis\\w*|^Consul\\w*|^Newtonsoft.Json.\\w*|^Autofac.\\w*{0}", string.IsNullOrEmpty(notRelatedFile) ? "" : $"|{notRelatedFile}"); @@ -713,7 +721,7 @@ private static string[] GetFilterAssemblies(string[] assemblyNames) return assemblyNames.Where( name => !notRelatedRegex.IsMatch(name)).ToArray(); - } +} } private static List GetAllAssemblyFiles(string parentDir) @@ -734,7 +742,7 @@ private static List GetAllAssemblyFiles(string parentDir) { return Directory.GetFiles(parentDir, "*.dll").Select(Path.GetFullPath).Where( - a => !notRelatedRegex.IsMatch(a)).ToList(); + a => !notRelatedRegex.IsMatch(Path.GetFileName(a))).ToList(); } } } diff --git a/src/Surging.Core/Surging.Core.Caching/AddressResolvers/Implementation/DefaultAddressResolver.cs b/src/Surging.Core/Surging.Core.Caching/AddressResolvers/Implementation/DefaultAddressResolver.cs index f5ce01b25..20cfc30fd 100644 --- a/src/Surging.Core/Surging.Core.Caching/AddressResolvers/Implementation/DefaultAddressResolver.cs +++ b/src/Surging.Core/Surging.Core.Caching/AddressResolvers/Implementation/DefaultAddressResolver.cs @@ -102,9 +102,11 @@ private void ServiceCacheManager_Removed(object sender, ServiceCacheEventArgs e) if (hash != null) foreach (var node in e.Cache.CacheEndpoint) { - var hashNode = node as ConsistentHashNode; - hash.Remove(hashNode); - hash.Add(hashNode); + + var hashNode = node as ConsistentHashNode; + var addr = string.Format("{0}:{1}", hashNode.Host, hashNode.Port); + hash.Remove(addr); + hash.Add(hashNode, addr); } } } @@ -122,8 +124,9 @@ private void ServiceCacheManager_Add(object sender, ServiceCacheEventArgs e) foreach (var node in e.Cache.CacheEndpoint) { var hashNode = node as ConsistentHashNode; - hash.Remove(hashNode); - hash.Add(hashNode); + var addr = string.Format("{0}:{1}", hashNode.Host, hashNode.Port); + hash.Remove(addr); + hash.Add(hashNode, addr); } } } diff --git a/src/Surging.Core/Surging.Core.Caching/HashAlgorithms/ConsistentHash.cs b/src/Surging.Core/Surging.Core.Caching/HashAlgorithms/ConsistentHash.cs index 03cfc68bb..33a852505 100644 --- a/src/Surging.Core/Surging.Core.Caching/HashAlgorithms/ConsistentHash.cs +++ b/src/Surging.Core/Surging.Core.Caching/HashAlgorithms/ConsistentHash.cs @@ -46,22 +46,6 @@ public int VirtualNodeReplicationFactor } #endregion - /// - /// 初始化节点服务器 - /// - /// 节点 - /// - /// 创建:范亮 - /// 日期:2016/4/2 - /// - public void Initialize(IEnumerable nodes) - { - foreach (var node in nodes) - { - AddNode(node); - } - _nodeKeysInRing = _ring.Keys.ToArray(); - } /// /// 添加节点 @@ -71,12 +55,17 @@ public void Initialize(IEnumerable nodes) /// 创建:范亮 /// 日期:2016/4/2 /// - public void Add(T node) + public void Add(T node, string value) { - AddNode(node); + AddNode(node, value); _nodeKeysInRing = _ring.Keys.ToArray(); } + public IEnumerable GetNodes() + { + return _ring.Values.Distinct().ToList(); + } + /// /// 删除节点 /// @@ -85,7 +74,7 @@ public void Add(T node) /// 创建:范亮 /// 日期:2016/4/2 /// - public void Remove(T node) + public void Remove(string node) { RemoveNode(node); _nodeKeysInRing = _ring.Keys.ToArray(); @@ -107,11 +96,6 @@ public T GetItemNode(string item) return _ring[_nodeKeysInRing[nearestNodePosition]]; } - public IEnumerable GetNodes() - { - return _ring.Values.Distinct().ToList(); - } - /// /// 添加节点 /// @@ -120,11 +104,11 @@ public IEnumerable GetNodes() /// 创建:范亮 /// 日期:2016/4/2 /// - private void AddNode(T node) + private void AddNode(T node, string value) { for (var i = 0; i < _virtualNodeReplicationFactor; i++) { - var hashOfVirtualNode = _hashAlgorithm.Hash(node.GetHashCode().ToString(CultureInfo.InvariantCulture) + i); + var hashOfVirtualNode = _hashAlgorithm.Hash(value.ToString(CultureInfo.InvariantCulture) + i); _ring[hashOfVirtualNode] = node; } } @@ -137,13 +121,12 @@ private void AddNode(T node) /// 创建:范亮 /// 日期:2016/4/2 /// - private void RemoveNode(T node) + private void RemoveNode(string value) { for (var i = 0; i < _virtualNodeReplicationFactor; i++) { - var hashOfVirtualNode = _hashAlgorithm.Hash(node.GetHashCode().ToString() + i); - if (_ring.ContainsKey(hashOfVirtualNode)) - _ring.Remove(hashOfVirtualNode); + var hashOfVirtualNode = _hashAlgorithm.Hash(value.ToString() + i); + _ring.Remove(hashOfVirtualNode); } } diff --git a/src/Surging.Core/Surging.Core.Caching/RedisCache/RedisContext.cs b/src/Surging.Core/Surging.Core.Caching/RedisCache/RedisContext.cs index 18f948d21..238d3e0a4 100644 --- a/src/Surging.Core/Surging.Core.Caching/RedisCache/RedisContext.cs +++ b/src/Surging.Core/Surging.Core.Caching/RedisCache/RedisContext.cs @@ -206,7 +206,7 @@ private void InitSettingHashStorage() { db = dbs[dbs.Length - 1]; } - hash.Add(new ConsistentHashNode() + var node = new ConsistentHashNode() { Type = targetType, Host = endpoints[0], @@ -216,7 +216,8 @@ private void InitSettingHashStorage() MaxSize = this._maxSize, MinSize = this._minSize, Db = db.ToString() - }); + }; + hash.Add(node,string.Format("{0}:{1}",node.Host,node.Port)); dicHash.GetOrAdd(targetType.ToString(), hash); }); }