From 87f096e0e64e456b32744638baedf04e4776c94f Mon Sep 17 00:00:00 2001 From: xiaolipro <2357729423@qq.com> Date: Tue, 31 Dec 2024 17:02:03 +0800 Subject: [PATCH] 1 --- src/InterfaceForward.Api/appsettings.json | 6 + .../InterfaceForwardRepositoriesModule.cs | 20 ++- .../Log/ES/ConnectionFactory.cs | 118 ++++++++---------- .../Log/ES/ElasticSearchContextAbstract.cs | 75 ++--------- .../Log/ES/ElasticSearchOptions.cs | 16 +-- .../Log/ES/IElasticsearchClientService.cs | 45 ++++++- .../Log/ES/UseIndexPrefixAttribute.cs | 16 --- .../Log/Services/ILogRepository.cs | 3 +- 8 files changed, 131 insertions(+), 168 deletions(-) delete mode 100644 src/InterfaceForward.Repositories/Log/ES/UseIndexPrefixAttribute.cs diff --git a/src/InterfaceForward.Api/appsettings.json b/src/InterfaceForward.Api/appsettings.json index 2df4f10..ccc5b05 100644 --- a/src/InterfaceForward.Api/appsettings.json +++ b/src/InterfaceForward.Api/appsettings.json @@ -42,5 +42,11 @@ "UserName": "dev", "Password": "itd!@#123" } + }, + "ElasticSearch": { + "NodeUrls": [ "http://elasticsearch-dev.default.svc.cluster.local:9200" ], + "LoginName": "elastic", + "Password": "afyTgmVb8jq6", + "Base64PassPassword": "Basic ZWxhc3RpYzphZnlUZ21WYjhqcTY=" } } diff --git a/src/InterfaceForward.Repositories/InterfaceForwardRepositoriesModule.cs b/src/InterfaceForward.Repositories/InterfaceForwardRepositoriesModule.cs index c2c2c81..4cffc66 100644 --- a/src/InterfaceForward.Repositories/InterfaceForwardRepositoriesModule.cs +++ b/src/InterfaceForward.Repositories/InterfaceForwardRepositoriesModule.cs @@ -1,6 +1,8 @@ -using Fake.Modularity; +using System.Configuration; +using Fake.Modularity; using Fake.SqlSugarCore; using Fake.UnitOfWork; +using InterfaceForward.Repositories.Log.ES; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -14,11 +16,19 @@ public class InterfaceForwardRepositoriesModule : FakeModule { var configuration = context.Services.GetConfiguration(); context.Services.AddSugarDbContext(options => - { - options.ConnectionString = configuration.GetConnectionString("Default")!; - options.DbType = DbType.MySql; - }); + { + options.ConnectionString = configuration.GetConnectionString("Default")!; + options.DbType = DbType.MySql; + }); context.Services.AddScoped(typeof(IBasicRepository<>), typeof(BasicRepository<>)); + + context.Services.Configure(options => + { + configuration.GetSection("ElasticSearch").Bind(options); +#if !DEBUG + options.ShowLogInfo = false; +#endif + }); } } \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Log/ES/ConnectionFactory.cs b/src/InterfaceForward.Repositories/Log/ES/ConnectionFactory.cs index 5d6bb7f..45e8624 100644 --- a/src/InterfaceForward.Repositories/Log/ES/ConnectionFactory.cs +++ b/src/InterfaceForward.Repositories/Log/ES/ConnectionFactory.cs @@ -1,97 +1,84 @@ using System.Collections.Concurrent; using System.Reflection; using Elasticsearch.Net; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Nest; namespace InterfaceForward.Repositories.Log.ES; -public class ConnectionFactory +public class ConnectionFactory : ISingletonDependency { - internal static ElasticSearchOptions _options; - internal static ConnectionSettings _connectionSettings; - private static ConcurrentDictionary _clientDic = new(); + private readonly ILogger _logger; + private readonly ElasticSearchOptions _options; + private readonly ConnectionSettings _connectionSettings; + private readonly ConcurrentDictionary _clientDic = new(); - #region 构造函数 - /// - /// 构造函数 - /// - /// - public ConnectionFactory(IOptions options) + + public ConnectionFactory(IOptions options, ILogger logger) { - if (_options == null) - _options = options.Value; + _logger = logger; + _options = options.Value; - if (_connectionSettings == null) + var uris = new List(); + foreach (var item in _options.NodeUrls) { - var uris = new List(); - foreach (var item in _options.NodeUrls) - { - uris.Add(new Uri(item)); - } - - //using var connectionPool = new SniffingConnectionPool(uris);//集群连接池,支持嗅探,ping - - using var connectionPool = new SingleNodeConnectionPool(uris[0]);//单点连接池 - _connectionSettings = new ConnectionSettings(connectionPool).BasicAuthentication(_options.LoginName, _options.Password); + uris.Add(new Uri(item)); } - } - #endregion - #region 上下文 + //using var connectionPool = new SniffingConnectionPool(uris);//集群连接池,支持嗅探,ping + + using var connectionPool = new SingleNodeConnectionPool(uris[0]); //单点连接池 + _connectionSettings = + new ConnectionSettings(connectionPool).BasicAuthentication(_options.LoginName, _options.Password); + } + + public IElasticClient GetContext(Type type) { - IElasticClient client = null; - //如果有缓存,直接从缓存返回 - if (_clientDic.ContainsKey(type)) + if (_clientDic.TryGetValue(type, out var value)) { - client = _clientDic[type]; + return value; } - else + + IElasticClient client; + var defaultIndex = ""; + + var attr = type.GetCustomAttribute(false); + if (attr == null) + throw new Exception("未找到指定对象的index名称"); + + defaultIndex = !_options.IndexPrefix.IsNullOrWhiteSpace() + ? $"{_options.IndexPrefix}{attr.RelationName}" + : attr.RelationName; + _connectionSettings.DefaultIndex(defaultIndex); + + //是否输出日志 + if (_options.ShowLogInfo) { - var defaultIndex = ""; - - var attr = type.GetCustomAttribute(false); - if (attr == null) - throw new Exception("未找到指定对象的index名称"); - - //如果启用了index前缀 - var useIndexNamePrefix = type.GetCustomAttributes(typeof(UseIndexPrefixAttribute), true).Any(); - if (useIndexNamePrefix && string.IsNullOrEmpty(_options.IndexPrefix)) - throw new Exception($"{type}启用了index前缀,却没在配置文件中找到相关配置,请检查配置项[ElasticSearch:IndexPrefix]是否指定!"); - - defaultIndex = useIndexNamePrefix ? $"{_options.IndexPrefix}{attr.RelationName}" : attr.RelationName; - _connectionSettings.DefaultIndex(defaultIndex); - - //是否输出日志 - if (_options.ShowLogInfo) - { - _connectionSettings.EnableDebugMode(); - client = new ElasticClient(_connectionSettings - //打印请求、回复,可能影响性能 - .DisableDirectStreaming() - .OnRequestCompleted(apiCallDetails => + _connectionSettings.EnableDebugMode(); + client = new ElasticClient(_connectionSettings + //打印请求、回复,可能影响性能 + .DisableDirectStreaming() + .OnRequestCompleted(apiCallDetails => { string infos = GetInfosFromApiCallDetails(apiCallDetails); Console.WriteLine($"{Environment.NewLine}{infos}{Environment.NewLine}"); })); - } - else - { - client = new ElasticClient(_connectionSettings); - } - - - _clientDic.TryAdd(type, client); - } + else + { + client = new ElasticClient(_connectionSettings); + } + + + _clientDic.TryAdd(type, client); return client; } - #region 输出执行结果 /// /// 输出执行结果 /// @@ -100,11 +87,8 @@ public class ConnectionFactory private string GetInfosFromApiCallDetails(IApiCallDetails details) { var infos = $@" - |-执行时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")} + |-执行时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss ffff} |-请求响应:{details.DebugInformation} "; return infos; } - #endregion - - #endregion } \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Log/ES/ElasticSearchContextAbstract.cs b/src/InterfaceForward.Repositories/Log/ES/ElasticSearchContextAbstract.cs index aa2eae3..faec8db 100644 --- a/src/InterfaceForward.Repositories/Log/ES/ElasticSearchContextAbstract.cs +++ b/src/InterfaceForward.Repositories/Log/ES/ElasticSearchContextAbstract.cs @@ -4,35 +4,15 @@ namespace InterfaceForward.Repositories.Log.ES; public abstract class ElasticSearchContextAbstract : IElasticsearchClientService where T : IdBaseIndex, new() { + private readonly string _indexName; + protected IElasticClient Context { get; } - #region 构造函数 - private readonly IElasticClient _client; - private string _indexName; - public ElasticSearchContextAbstract(ConnectionFactory connectionFactory) + protected ElasticSearchContextAbstract(ConnectionFactory connectionFactory) { - _client = connectionFactory.GetContext(typeof(T)); - _indexName = _client.ConnectionSettings.DefaultIndex; + Context = connectionFactory.GetContext(typeof(T)); + _indexName = Context.ConnectionSettings.DefaultIndex; } - #endregion - - #region 上下文 - public IElasticClient Context - { - get - { - return _client; - } - } - - #endregion - - #region 新增文档 - /// - /// 新增文档 - /// - /// 索引数据 - /// - /// + public virtual async Task IndexDocumentAsync(T data) { var response = await Context.IndexAsync(data, request => request.Index(_indexName)); @@ -42,13 +22,6 @@ public abstract class ElasticSearchContextAbstract : IElasticsearchClientServ return response.Id; } - /// - /// 批量新增文档 - /// - /// - /// - /// - /// public virtual async Task IndexManyDocumentAsync(List list, bool refresh = false) { var response = await Context.IndexManyAsync(list, _indexName); @@ -60,47 +33,22 @@ public abstract class ElasticSearchContextAbstract : IElasticsearchClientServ return response; } - #endregion - - #region 根据Id删除文档 - /// - /// 根据Id删除文档 - /// - /// - /// - /// + public virtual async Task DeleteByIdAsync(string id) { var response = await Context.DeleteAsync(id, i => i.Index(_indexName)); if (!response.IsValid) throw new Exception($"数据删除失败,原因:{response.DebugInformation}"); } - #endregion - - #region 修改指定的文档 - /// - /// 修改指定的文档 - /// - /// - /// - /// - /// + public virtual async Task UpdateAsync(string id, T data) { var response = await Context.UpdateAsync(id, u => u.Doc(data).Index(_indexName)); if (!response.IsValid) throw new Exception($"数据修改失败,原因:{response.DebugInformation}"); } - #endregion - - #region 根据ID获取单条 - /// - /// 根据ID获取单条文档 - /// - /// - /// - /// - public virtual async Task GetByIdAsync(string id) + + public virtual async Task GetByIdAsync(string id) { var response = await Context.GetAsync(id, i => i.Index(_indexName)); if (response.Source != null) @@ -108,11 +56,10 @@ public abstract class ElasticSearchContextAbstract : IElasticsearchClientServ switch (response.ApiCall.HttpStatusCode) { - case 200: return response?.Source; + case 200: return response.Source; case 404: return null; default: throw new Exception($"数据查询失败,原因:{response.DebugInformation}"); } } - #endregion } \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Log/ES/ElasticSearchOptions.cs b/src/InterfaceForward.Repositories/Log/ES/ElasticSearchOptions.cs index 8ba6807..fbfb449 100644 --- a/src/InterfaceForward.Repositories/Log/ES/ElasticSearchOptions.cs +++ b/src/InterfaceForward.Repositories/Log/ES/ElasticSearchOptions.cs @@ -5,31 +5,25 @@ public class ElasticSearchOptions /// /// 是否显示日志 /// - public bool ShowLogInfo { get; set; } = false; + public bool ShowLogInfo { get; set; } /// /// 服务节点 /// - public List NodeUrls { get; set; } + public List NodeUrls { get; set; } = []; /// /// 索引前缀 /// - public string IndexPrefix { get; set; } + public string? IndexPrefix { get; set; } /// /// 登录账号 /// - public string LoginName { get; set; } + public string LoginName { get; set; } = null!; /// /// 登录密码(明文) /// - public string Password { get; set; } - - /// - /// 登录密码(加密) - /// - public string Base64PassPassword { get; set; } - + public string Password { get; set; } = null!; } \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Log/ES/IElasticsearchClientService.cs b/src/InterfaceForward.Repositories/Log/ES/IElasticsearchClientService.cs index 3f94f3f..fc6d2ca 100644 --- a/src/InterfaceForward.Repositories/Log/ES/IElasticsearchClientService.cs +++ b/src/InterfaceForward.Repositories/Log/ES/IElasticsearchClientService.cs @@ -4,10 +4,47 @@ namespace InterfaceForward.Repositories.Log.ES; public interface IElasticsearchClientService where T : class { - + /// + /// 新增文档 + /// + /// 索引数据 + /// + /// Task IndexDocumentAsync(T data); - Task GetByIdAsync(string Id); - Task UpdateAsync(string id, T input); - Task DeleteByIdAsync(string id); + + /// + /// 批量新增文档 + /// + /// + /// + /// + /// + /// Task IndexManyDocumentAsync(List list, bool refresh); + + /// + /// 根据ID获取单条文档 + /// + /// + /// + /// + Task GetByIdAsync(string id); + + /// + /// 修改指定的文档 + /// + /// + /// + /// + /// + /// + Task UpdateAsync(string id, T data); + + /// + /// 根据Id删除文档 + /// + /// + /// + /// + Task DeleteByIdAsync(string id); } \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Log/ES/UseIndexPrefixAttribute.cs b/src/InterfaceForward.Repositories/Log/ES/UseIndexPrefixAttribute.cs deleted file mode 100644 index 9254e93..0000000 --- a/src/InterfaceForward.Repositories/Log/ES/UseIndexPrefixAttribute.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace InterfaceForward.Repositories.Log.ES; - -[AttributeUsage(AttributeTargets.Class)] -public class UseIndexPrefixAttribute : Attribute -{ - public UseIndexPrefixAttribute() - { - - } - public UseIndexPrefixAttribute(string indexPrefix) - { - IndexPrefix = indexPrefix; - } - - public string IndexPrefix { get; } -} \ No newline at end of file diff --git a/src/InterfaceForward.Repositories/Log/Services/ILogRepository.cs b/src/InterfaceForward.Repositories/Log/Services/ILogRepository.cs index 8a428cc..d87fe46 100644 --- a/src/InterfaceForward.Repositories/Log/Services/ILogRepository.cs +++ b/src/InterfaceForward.Repositories/Log/Services/ILogRepository.cs @@ -1,5 +1,6 @@ using InterfaceForward.Domain.Shared.Dtos; using InterfaceForward.Repositories.Log.Entitys; +using InterfaceForward.Repositories.Log.ES; using InterfaceForward.Repositories.Log.ValueObjects; using Nest; @@ -8,7 +9,7 @@ namespace InterfaceForward.Repositories.Log.Services /// /// 日志仓储 /// - public interface ILogRepository + public interface ILogRepository : IElasticsearchClientService { /// /// 应用id