Skip to content

Commit c92bcb3

Browse files
committed
Optimize the problem that the startup is too slow in the debug environment
1 parent 9936673 commit c92bcb3

4 files changed

Lines changed: 137 additions & 33 deletions

File tree

src/Surging.Core/Surging.Core.Consul/ConsulServiceCacheManager.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ private async Task<ServiceCache[]> GetCaches(IEnumerable<string> childrens)
126126
return caches.ToArray();
127127
}
128128

129+
private async Task<ServiceCache[]> GetCaches(IEnumerable<byte[]> childrens)
130+
{
131+
if (childrens == null) return new ServiceCache[0];
132+
childrens = childrens.ToArray();
133+
var caches = new List<ServiceCache>(childrens.Count());
134+
135+
foreach (var children in childrens)
136+
{
137+
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
138+
_logger.LogDebug($"准备从节点:{children}中获取缓存信息。");
139+
140+
var cache = await GetCache(children);
141+
if (cache != null)
142+
{
143+
caches.Add(cache);
144+
var watcher = new NodeMonitorWatcher(GetConsulClient, _manager, $"{_configInfo.CachePath}{cache.CacheDescriptor.Id}",
145+
async (oldData, newData) => await NodeChange(oldData, newData), null);
146+
watcher.SetCurrentData(children);
147+
}
148+
}
149+
return caches.ToArray();
150+
}
151+
152+
129153
private async Task<ServiceCache> GetCache(string path)
130154
{
131155
ServiceCache result = null;
@@ -178,11 +202,10 @@ private async Task EnterCaches()
178202
(result) => ConvertPaths(result).Result);
179203
if (client.KV.Keys(_configInfo.CachePath).Result.Response?.Count() > 0)
180204
{
181-
var result = await client.GetChildrenAsync(_configInfo.CachePath);
182-
var keys = await client.KV.Keys(_configInfo.CachePath);
183-
var childrens = result;
184-
watcher.SetCurrentData(ConvertPaths(childrens).Result.Select(key => $"{_configInfo.CachePath}{key}").ToArray());
185-
_serviceCaches = await GetCaches(keys.Response);
205+
var response = await client.GetChildrenListAsync(_configInfo.CachePath);
206+
_serviceCaches = await GetCaches(response);
207+
var serviceCacheIds = _serviceCaches.Select(p => p.CacheDescriptor.Id).ToArray();
208+
watcher.SetCurrentData(serviceCacheIds.Select(key => $"{_configInfo.CachePath}{key}").ToArray());
186209
}
187210
else
188211
{

src/Surging.Core/Surging.Core.Consul/ConsulServiceCommandManager.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ public override async Task SetServiceCommandsAsync(IEnumerable<ServiceCommandDes
149149

150150
protected override async Task InitServiceCommandsAsync(IEnumerable<ServiceCommandDescriptor> serviceCommands)
151151
{
152-
var commands = await GetServiceCommands(serviceCommands.Select(p => $"{ _configInfo.CommandPath}{ p.ServiceId}"));
152+
var client = await GetConsulClient();
153+
var response = await client.GetChildrenListAsync(_configInfo.CommandPath);
154+
var commands = await GetServiceCommands(response); //传参数到方法中
153155
if (commands.Count() == 0 || _configInfo.ReloadOnChange)
154156
{
155157
await SetServiceCommandsAsync(serviceCommands);
@@ -242,6 +244,34 @@ private async Task<ServiceCommandDescriptor[]> GetServiceCommands(IEnumerable<st
242244
}
243245
return serviceCommands.ToArray();
244246
}
247+
248+
private Task<ServiceCommandDescriptor[]> GetServiceCommands(IEnumerable<byte[]> childrens)
249+
{
250+
if (childrens == null) return Task.FromResult(new ServiceCommandDescriptor[0]);
251+
childrens = childrens.ToArray();
252+
var serviceCommands = new List<ServiceCommandDescriptor>(childrens.Count());
253+
254+
foreach (var children in childrens)
255+
{
256+
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
257+
_logger.LogDebug($"准备从节点:{children}中获取服务命令信息。");
258+
259+
var serviceCommand = GetServiceCommand(children);
260+
if (serviceCommand != null)
261+
{
262+
serviceCommands.Add(serviceCommand);
263+
var watcher = new NodeMonitorWatcher(GetConsulClient, _manager, $"{ _configInfo.CommandPath}{ serviceCommand.ServiceId}",
264+
(oldData, newData) => NodeChange(oldData, newData), tmpPath =>
265+
{
266+
var index = tmpPath.LastIndexOf("/");
267+
return _serviceHeartbeatManager.ExistsWhitelist(tmpPath.Substring(index + 1));
268+
});
269+
watcher.SetCurrentData(children);
270+
}
271+
}
272+
return Task.FromResult(serviceCommands.ToArray());
273+
}
274+
245275
private async ValueTask<ConsulClient> GetConsulClient()
246276
{
247277
var client = await _consulClientFactory.GetClient();
@@ -263,11 +293,10 @@ private async Task EnterServiceCommands()
263293
}
264294
if (client.KV.Keys(_configInfo.CommandPath).Result.Response?.Count() > 0)
265295
{
266-
var result = await client.GetChildrenAsync(_configInfo.CommandPath);
267-
var keys = await client.KV.Keys(_configInfo.CommandPath);
268-
var childrens = result;
269-
action?.Invoke(ConvertPaths(childrens).Select(key => $"{_configInfo.CommandPath}{key}").ToArray());
270-
_serviceCommands = await GetServiceCommands(keys.Response);
296+
var response = await client.GetChildrenListAsync(_configInfo.CommandPath);
297+
_serviceCommands = await GetServiceCommands(response);
298+
var serviceIds = _serviceCommands.Select(p => p.ServiceId).ToArray();
299+
action?.Invoke(serviceIds.Select(key => $"{_configInfo.CommandPath}{key}").ToArray());
271300
}
272301
else
273302
{

src/Surging.Core/Surging.Core.Consul/ConsulServiceRouteManager.cs

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,41 @@ public override async Task<IEnumerable<ServiceRoute>> GetRoutesAsync()
8989
}
9090

9191
public override async Task SetRoutesAsync(IEnumerable<ServiceRoute> routes)
92-
{
93-
await _consulClientProvider.Check();
94-
var hostAddr = NetUtils.GetHostAddress();
95-
var serviceRoutes = await GetRoutes(routes.Select(p => $"{ _configInfo.RoutePath}{p.ServiceDescriptor.Id}"));
96-
foreach (var route in routes)
92+
{
93+
// var locks = await CreateLock();
94+
try
9795
{
98-
var serviceRoute = serviceRoutes.Where(p => p.ServiceDescriptor.Id == route.ServiceDescriptor.Id).FirstOrDefault();
99-
100-
if (serviceRoute != null)
96+
await _consulClientProvider.Check();
97+
var hostAddr = NetUtils.GetHostAddress();
98+
var client = await GetConsulClient();
99+
var response = await client.GetChildrenListAsync(_configInfo.RoutePath);
100+
var serviceRoutes = await GetRoutes(response);
101+
foreach (var route in routes)
101102
{
102-
var addresses = serviceRoute.Address.Concat(
103-
route.Address.Except(serviceRoute.Address)).ToList();
103+
var serviceRoute = serviceRoutes.Where(p => p.ServiceDescriptor.Id == route.ServiceDescriptor.Id).FirstOrDefault();
104104

105-
foreach (var address in route.Address)
105+
if (serviceRoute != null)
106106
{
107-
addresses.Remove(addresses.Where(p => p.ToString() == address.ToString()).FirstOrDefault());
108-
addresses.Add(address);
107+
var addresses = serviceRoute.Address.Concat(
108+
route.Address.Except(serviceRoute.Address)).ToList();
109+
110+
foreach (var address in route.Address)
111+
{
112+
addresses.Remove(addresses.Where(p => p.ToString() == address.ToString()).FirstOrDefault());
113+
addresses.Add(address);
114+
}
115+
route.Address = addresses;
109116
}
110-
route.Address = addresses;
111117
}
118+
await RemoveExceptRoutesAsync(routes, hostAddr);
119+
var routeIds = serviceRoutes.Where(p => p.Address.Contains(hostAddr)).Select(p => p.ServiceDescriptor.Id).ToList();
120+
routes = routes.Where(p => !routeIds.Contains(p.ServiceDescriptor.Id));
121+
await base.SetRoutesAsync(routes);
122+
}
123+
finally
124+
{
125+
//locks.ForEach(p => p.Release());
112126
}
113-
await RemoveExceptRoutesAsync(routes, hostAddr);
114-
await base.SetRoutesAsync(routes);
115127
}
116128

117129
public override async Task RemveAddressAsync(IEnumerable<AddressModel> Address)
@@ -248,6 +260,33 @@ private async Task<ServiceRoute[]> GetRoutes(IEnumerable<string> childrens)
248260
return routes.ToArray();
249261
}
250262

263+
private async Task<ServiceRoute[]> GetRoutes(IEnumerable<byte[]> childrens)
264+
{
265+
if (childrens == null) return new ServiceRoute[0];
266+
childrens = childrens.ToArray();
267+
var routes = new List<ServiceRoute>(childrens.Count());
268+
269+
foreach (var children in childrens)
270+
{
271+
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
272+
_logger.LogDebug($"准备从节点:{children}中获取路由信息。");
273+
274+
var route = await GetRoute(children);
275+
if (route != null)
276+
{
277+
routes.Add(route);
278+
var watcher = new NodeMonitorWatcher(GetConsulClient, _manager, $"{_configInfo.RoutePath}{route.ServiceDescriptor.Id}",
279+
async (oldData, newData) => await NodeChange(oldData, newData), tmpPath =>
280+
{
281+
var index = tmpPath.LastIndexOf("/");
282+
return _serviceHeartbeatManager.ExistsWhitelist(tmpPath.Substring(index + 1));
283+
});
284+
watcher.SetCurrentData(children);
285+
}
286+
}
287+
return routes.ToArray();
288+
}
289+
251290
private async Task<ServiceRoute> GetRoute(string path)
252291
{
253292
ServiceRoute result = null;
@@ -296,13 +335,12 @@ private async Task EnterRoutes()
296335
}
297336
if (client.KV.Keys(_configInfo.RoutePath).Result.Response?.Count() > 0)
298337
{
299-
var result = await client.GetChildrenAsync(_configInfo.RoutePath);
300-
var keys = await client.KV.Keys(_configInfo.RoutePath);
301-
var childrens = result;
302-
//传参数到方法中
303-
action?.Invoke(ConvertPaths(childrens).Result.Select(key => $"{_configInfo.RoutePath}{key}").ToArray());
338+
var response = await client.GetChildrenListAsync(_configInfo.RoutePath);
304339
//重新赋值到routes中
305-
_routes = await GetRoutes(keys.Response);
340+
_routes = await GetRoutes(response);
341+
var serviceIds = _routes.Select(p => p.ServiceDescriptor.Id).ToArray();
342+
//传参数到方法中
343+
action?.Invoke(serviceIds.Select(key => $"{_configInfo.RoutePath}{key}").ToArray());
306344
}
307345
else
308346
{

src/Surging.Core/Surging.Core.Consul/Utilitys/ConsulClientExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Net.Http;
77
using System.Net.Http.Headers;
8+
using System.Collections.Generic;
89

910
namespace Surging.Core.Consul.Utilitys
1011
{
@@ -23,6 +24,19 @@ public static async Task<string[]> GetChildrenAsync(this ConsulClient client, st
2324
}
2425
}
2526

27+
public static async Task<List<byte[]>> GetChildrenListAsync(this ConsulClient client, string path)
28+
{
29+
try
30+
{
31+
var queryResult = await client.KV.List(path);
32+
return queryResult.Response?.Select(p => p.Value).ToList();
33+
}
34+
catch (HttpRequestException)
35+
{
36+
return null;
37+
}
38+
}
39+
2640
public static async Task<byte[]> GetDataAsync(this ConsulClient client, string path)
2741
{
2842
try

0 commit comments

Comments
 (0)