Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyVersion>1.9.15</AssemblyVersion>
<Version>1.9.15</Version>
<PackageVersion>1.9.15</PackageVersion>
<AssemblyVersion>1.10.0</AssemblyVersion>
<Version>1.10.0</Version>
<PackageVersion>1.10.0</PackageVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<FileVersion>1.9.15</FileVersion>
<FileVersion>1.10.0</FileVersion>
<Authors>kklldog</Authors>
<Company>kklldog</Company>
</PropertyGroup>
Expand Down
33 changes: 21 additions & 12 deletions src/AgileConfig.Server.Apisite/Controllers/api/ConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using AgileConfig.Server.Apisite.Models.Mapping;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

Expand Down Expand Up @@ -60,24 +61,32 @@ public async Task<ActionResult<List<ApiConfigVM>>> GetAppConfig(string appId, [F
}

var cacheKey = $"ConfigController_AppConfig_{appId}_{env.Value}";
List<ApiConfigVM> configs = null;
_cacheMemory?.TryGetValue(cacheKey, out configs);
if (configs != null)
AppConfigsCache cache = null;
_cacheMemory?.TryGetValue(cacheKey, out cache);

if (cache == null)
{
return configs;
}
cache = new AppConfigsCache();

var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritanced(appId, env.Value);
var vms = appConfigs.Select(x => x.ToApiConfigVM()).ToList();
var publishTimelineId = await _configService.GetLastPublishTimelineVirtualIdAsync(appId, env.Value);
var appConfigs = await _configService.GetPublishedConfigsByAppIdWithInheritance(appId, env.Value);
var vms = appConfigs.Select(x => x.ToApiConfigVM()).ToList();

//增加5s的缓存,防止同一个app同时启动造成db的压力过大
var cacheOp = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(5));
_cacheMemory?.Set(cacheKey, vms, cacheOp);
cache.Key = cacheKey;
cache.Configs = vms;
cache.VirtualId = publishTimelineId;

//cache 5 seconds to avoid too many db query
var cacheOp = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(5));
_cacheMemory?.Set(cacheKey, cache, cacheOp);
}

Response?.Headers?.Append("publish-time-line-id", cache.VirtualId);

_meterService.PullAppConfigCounter?.Add(1, new("appId", appId), new("env", env));

return vms;
return cache.Configs;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using AgileConfig.Server.Apisite.Models;
using System.Collections.Generic;
using AgileConfig.Server.Apisite.Models;
using AgileConfig.Server.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AgileConfig.Server.Apisite.Controllers.api.Models
{
Expand Down Expand Up @@ -53,6 +50,16 @@ public class ApiConfigVM : IAppIdModel
public string Description { get; set; }
}


public class AppConfigsCache
{
public string Key { get; set; }

public string VirtualId { get; set; }

public List<ApiConfigVM> Configs { get; set; }
}

public static class ApiConfigVMExtension
{
public static ConfigVM ToConfigVM(this ApiConfigVM model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ internal class MessageHandler : IMessageHandler
private readonly IConfigService _configService;
private readonly IRegisterCenterService _registerCenterService;
private readonly IServiceInfoService _serviceInfoService;


private int ClientVersion { get; set; }

public MessageHandler(
IConfigService configService,
IRegisterCenterService registerCenterService,
IConfigService configService,
IRegisterCenterService registerCenterService,
IServiceInfoService serviceInfoService)
{
_configService = configService;
_registerCenterService = registerCenterService;
_serviceInfoService = serviceInfoService;
}

public bool Hit(HttpRequest request)
{
var ver = request.Headers["client-v"];
Expand All @@ -40,6 +42,8 @@ public bool Hit(HttpRequest request)

if (int.TryParse(ver.ToString().Replace(".", ""), out var verInt))
{
ClientVersion = verInt;

return verInt >= 160;
}

Expand All @@ -63,12 +67,14 @@ public async Task Handle(string message, HttpRequest request, WebsocketClient cl
appId = HttpUtility.UrlDecode(appId);
var env = request.Headers["env"].ToString();
ISettingService.IfEnvEmptySetDefault(ref env);
var md5 = await _configService.AppPublishedConfigsMd5CacheWithInheritanced(appId, env);

var data = await GetCPingData(appId, env);

await SendMessage(client.Client, JsonConvert.SerializeObject(new WebsocketAction()
{
Action = ActionConst.Ping,
Module = ActionModule.ConfigCenter,
Data = md5
Data = data
}));
}
else if (message.StartsWith("s:ping:"))
Expand Down Expand Up @@ -98,4 +104,22 @@ await SendMessage(client.Client, JsonConvert.SerializeObject(new WebsocketAction
await SendMessage(client.Client, "0");
}
}

private async Task<string> GetCPingData(string appId, string env)
{
if (ClientVersion <= 176)
{
// 1.7.6及以前的版本,返回V:md5
var md5 = await _configService.AppPublishedConfigsMd5CacheWithInheritance(appId, env);

return md5;
}
else
{
// 1.7.7及以后的版本,返回 publish time line id
var publishTimeLineId = await _configService.GetLastPublishTimelineVirtualIdAsyncWithCache(appId, env);

return publishTimeLineId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Handle(string message, HttpRequest request, WebsocketClient cl
var appId = request.Headers["appid"];
var env = request.Headers["env"].ToString();
env = ISettingService.IfEnvEmptySetDefault(ref env);
var md5 = await _configService.AppPublishedConfigsMd5CacheWithInheritanced(appId, env);
var md5 = await _configService.AppPublishedConfigsMd5CacheWithInheritance(appId, env);
await SendMessage(client.Client, $"V:{md5}");
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace AgileConfig.Server.Data.Abstraction
{
public interface IPublishTimelineRepository : IRepository<PublishTimeline, string>
{
Task<string> GetLastPublishTimelineNodeIdAsync(string appId, string env);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ public PublishTimelineRepository(IFreeSql freeSql) : base(freeSql)
{
this.freeSql = freeSql;
}

public async Task<string> GetLastPublishTimelineNodeIdAsync(string appId, string env)
{
var node = await freeSql.Select<PublishTimeline>()
.Where(x => x.AppId == appId && x.Env == env)
.OrderByDescending(x => x.Version)
.FirstAsync();

return node?.Id;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AgileConfig.Server.Data.Repository.Mongodb;

namespace AgileConfig.Server.Data.Repository.Mongodb;

public class PublishTimelineRepository: MongodbRepository<PublishTimeline, string>, IPublishTimelineRepository
public class PublishTimelineRepository : MongodbRepository<PublishTimeline, string>, IPublishTimelineRepository
{
public PublishTimelineRepository(string? connectionString) : base(connectionString)
{
Expand All @@ -10,4 +11,11 @@ public PublishTimelineRepository(string? connectionString) : base(connectionStri
public PublishTimelineRepository(IConfiguration configuration) : base(configuration)
{
}

public async Task<string> GetLastPublishTimelineNodeIdAsync(string appId, string env)
{
var nodes = await this.QueryPageAsync(x => x.AppId == appId && x.Env == env, 1, 1, nameof(PublishTimeline.Version), "DESC");

return nodes?.FirstOrDefault()?.Id;
}
}
13 changes: 9 additions & 4 deletions src/AgileConfig.Server.IService/IConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public interface IConfigService: IDisposable
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
Task<List<Config>> GetPublishedConfigsByAppIdWithInheritanced(string appId, string env);
Task<List<Config>> GetPublishedConfigsByAppIdWithInheritance(string appId, string env);
/// <summary>
/// 获取app的配置项继承的app配置合并进来转换为字典
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
Task<Dictionary<string, Config>> GetPublishedConfigsByAppIdWithInheritanced_Dictionary(string appId, string env);
Task<Dictionary<string, Config>> GetPublishedConfigsByAppIdWithInheritance_Dictionary(string appId, string env);
Task<bool> AddAsync(Config config, string env);

Task<bool> AddRangeAsync(List<Config> configs, string env);
Expand Down Expand Up @@ -77,7 +77,7 @@ public interface IConfigService: IDisposable
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
Task<string> AppPublishedConfigsMd5WithInheritanced(string appId, string env);
Task<string> AppPublishedConfigsMd5WithInheritance(string appId, string env);

/// <summary>
/// 计算已发布配置项的MD5进行缓存
Expand All @@ -91,7 +91,7 @@ public interface IConfigService: IDisposable
/// </summary>
/// <param name="appId"></param>
/// <returns></returns>
Task<string> AppPublishedConfigsMd5CacheWithInheritanced(string appId, string env);
Task<string> AppPublishedConfigsMd5CacheWithInheritance(string appId, string env);

/// <summary>
/// 构造key
Expand Down Expand Up @@ -213,5 +213,10 @@ public interface IConfigService: IDisposable
/// clear all cache
/// </summary>
void ClearCache();

Task<string> GetLastPublishTimelineVirtualIdAsync(string appId, string env);

Task<string> GetLastPublishTimelineVirtualIdAsyncWithCache(string appId, string env);

}
}
Loading
Loading