diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs index 646320e7..b9a68835 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Program.cs @@ -24,7 +24,7 @@ namespace Yi.Framework.ApiMicroservice .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => { configurationBuilder.AddCommandLine(args); - configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false); + configurationBuilder.AddJsonFileService(); #region //Apollo配置 #endregion diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs index d1b9fd1c..86a2bf8f 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs @@ -70,6 +70,11 @@ namespace Yi.Framework.ApiMicroservice #endregion services.AddRabbitMQService(); + #region + //ElasticSeach服务配置 + #endregion + services.AddElasticSeachService(); + #region //短信服务配置 #endregion diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json index d27f3c97..91088903 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json @@ -17,6 +17,7 @@ "Redis_Enabled": true, "RedisSeed_Enabled": true, "Kafka_Enabled": false, + "ElasticSeach_Enabled": false, "MutiDB_Enabled": false, "SMS_Enabled": true, "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], @@ -53,6 +54,10 @@ "Password": "cc", "Port": 5672 }, + "ElasticSeachConn": { + "Url": "", + "IndexName": "" + }, "KafkaOptions": { "BrokerList": "192.168.3.230:9092", "TopicName": "kafkalog" diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Class1.cs b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Class1.cs deleted file mode 100644 index 553b9bc6..00000000 --- a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -锘縰sing System; - -namespace Yi.Framework.ElasticSearchProcessor -{ - public class Class1 - { - } -} diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/InitESIndexWorker.cs b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/InitESIndexWorker.cs new file mode 100644 index 00000000..161e40c5 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/InitESIndexWorker.cs @@ -0,0 +1,84 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Common.Models; +using Yi.Framework.Core; + +namespace Yi.Framework.ElasticSearchProcessor +{ + public class InitESIndexWorker : BackgroundService + { + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + private readonly RabbitMQInvoker _RabbitMQInvoker; + private readonly ElasticSearchInvoker _elasticSearchInvoker; + + public InitESIndexWorker(ILogger logger, RabbitMQInvoker rabbitMQInvoker, IConfiguration configuration, ElasticSearchInvoker elasticSearchInvoker) + { + this._logger = logger; + this._RabbitMQInvoker = rabbitMQInvoker; + this._configuration = configuration; + this._elasticSearchInvoker = elasticSearchInvoker; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + RabbitMQConsumerModel rabbitMQConsumerModel = new RabbitMQConsumerModel() + { + ExchangeName = RabbitMQExchangeQueueName.SKUCQRS_Exchange, + QueueName = RabbitMQExchangeQueueName.SKUCQRS_Queue_ESIndex + }; + HttpClient _HttpClient = new HttpClient(); + this._RabbitMQInvoker.RegistReciveAction(rabbitMQConsumerModel, message => + { + try + { + //【得到模型】 + //SPUCQRSQueueModel spuCQRSQueueModel = JsonConvert.DeserializeObject(message); + + //【判断增删改,es进行相应的操作】 + //switch (spuCQRSQueueModel.CQRSType) + //{ + //case (int)SPUCQRSQueueModelType.Insert: + //case (int)SPUCQRSQueueModelType.Update: + // { + // Goods goods = this._ISearchService.GetGoodsBySpuId(spuCQRSQueueModel.SpuId); + // this._IElasticSearchService.InsertOrUpdata(goods); + // break; + // } + //case (int)SPUCQRSQueueModelType.Delete: + // this._IElasticSearchService.Delete(spuCQRSQueueModel.SpuId.ToString()); + // break; + // default: + // throw new Exception("wrong spuCQRSQueueModel.CQRSType"); + //} + + this._logger.LogInformation($"{nameof(InitESIndexWorker)}.Init ESIndex succeed SpuId"); + return true; + } + catch (Exception ex) + { + LogModel logModel = new LogModel() + { + OriginalClassName = this.GetType().FullName, + OriginalMethodName = nameof(ExecuteAsync), + Remark = "定时作业错误日志" + }; + this._logger.LogError(ex, $"{nameof(InitESIndexWorker)}.Init ESIndex failed message={message}, Exception:{ex.Message}", JsonConvert.SerializeObject(logModel)); + return false; + } + }); + await Task.CompletedTask; + } + } +} diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Log4net.config b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Log4net.config new file mode 100644 index 00000000..958c7e78 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Log4net.config @@ -0,0 +1,65 @@ +锘 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Program.cs b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Program.cs new file mode 100644 index 00000000..b0e05774 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Program.cs @@ -0,0 +1,68 @@ +锘 +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using Yi.Framework.WebCore; +using Yi.Framework.WebCore.BuilderExtend; +using Yi.Framework.WebCore.MiddlewareExtend; + +namespace Yi.Framework.ElasticSearchProcessor +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => + { + configurationBuilder.AddCommandLine(args); + configurationBuilder.AddJsonFileService(); + //configurationBuilder.AddJsonFile("configuration.json", optional: false, reloadOnChange: true); + #region + //Apollo閰嶇疆 + #endregion + //configurationBuilder.AddApolloService("Yi"); + }) + .ConfigureLogging(loggingBuilder => + { + loggingBuilder.AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning); + loggingBuilder.AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning); + loggingBuilder.AddLog4Net(); + }) + .ConfigureServices((hostContext, services) => + { + + IConfiguration configuration = services.BuildServiceProvider().GetRequiredService(); + + #region + //Ioc閰嶇疆 + #endregion + services.AddSingleton(new Appsettings(configuration)); + + services.AddHostedService(); + services.AddHostedService(); + services.AddHostedService(); + #region 鏈嶅姟娉ㄥ叆 + //services.Configure(configuration.GetSection("MysqlConn")); + + + #region + //RabbitMQ鏈嶅姟閰嶇疆 + #endregion + services.AddRabbitMQService(); + #endregion + + #region Consul + //services.Configure(configuration.GetSection("ConsulClientOption")); + //services.AddTransient(); + #endregion + + }); + } +} diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/WarmupESIndexWorker.cs b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/WarmupESIndexWorker.cs new file mode 100644 index 00000000..a4d5550a --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/WarmupESIndexWorker.cs @@ -0,0 +1,76 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Common.Models; +using Yi.Framework.Core; + +namespace Yi.Framework.ElasticSearchProcessor +{ + public class WarmupESIndexWorker : BackgroundService + { + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + private readonly RabbitMQInvoker _RabbitMQInvoker; + private readonly ElasticSearchInvoker _elasticSearchInvoker; + private readonly IOptionsMonitor _ElasticSearchOptions = null; + + public WarmupESIndexWorker(ILogger logger, RabbitMQInvoker rabbitMQInvoker, IConfiguration configuration, ElasticSearchInvoker elasticSearchInvoker, IOptionsMonitor optionsMonitor) + { + this._logger = logger; + this._RabbitMQInvoker = rabbitMQInvoker; + this._configuration = configuration; + this._elasticSearchInvoker = elasticSearchInvoker; + this._ElasticSearchOptions = optionsMonitor; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + RabbitMQConsumerModel rabbitMQConsumerModel = new RabbitMQConsumerModel() + { + ExchangeName = RabbitMQExchangeQueueName.SKUWarmup_Exchange, + QueueName = RabbitMQExchangeQueueName.SKUWarmup_Queue_ESIndex + }; + HttpClient _HttpClient = new HttpClient(); + this._RabbitMQInvoker.RegistReciveAction(rabbitMQConsumerModel, message => + { + //SKUWarmupQueueModel skuWarmupQueueModel = JsonConvert.DeserializeObject(message); + //【得到消息队列模型】 + #region 先删除Index---新建Index---再建立全部数据索引 + { + try + { + this._elasticSearchInvoker.DropIndex(this._ElasticSearchOptions.CurrentValue.IndexName); + //this._ISearchService.ImpDataBySpu(); + //【触发es数据导入服务】 + this._logger.LogInformation($"{nameof(WarmupESIndexWorker)}.InitAll succeed"); + return true; + } + catch (Exception ex) + { + var logModel = new LogModel() + { + OriginalClassName = this.GetType().FullName, + OriginalMethodName = nameof(ExecuteAsync), + Remark = "定时作业错误日志" + }; + this._logger.LogError(ex, $"{nameof(WarmupESIndexWorker)}.Warmup ESIndex failed message={message}, Exception:{ex.Message}", JsonConvert.SerializeObject(logModel)); + return false; + } + } + #endregion + }); + await Task.CompletedTask; + } + } +} diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Worker.cs b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Worker.cs new file mode 100644 index 00000000..2966fab6 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/Worker.cs @@ -0,0 +1,29 @@ +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Yi.Framework.ElasticSearchProcessor +{ + public class Worker : BackgroundService + { + private readonly ILogger _logger; + + public Worker(ILogger logger) + { + _logger = logger; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + await Task.Delay(100000, stoppingToken); + } + } + } +} diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/appsettings.Development.json b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/appsettings.Development.json new file mode 100644 index 00000000..8983e0fc --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/appsettings.json b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/appsettings.json new file mode 100644 index 00000000..53d2c643 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/appsettings.json @@ -0,0 +1,81 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + + "Consul_Enabled": false, + "DbSeed_Enabled": true, + "Apollo_Enabled": false, + "HealthCheck_Enabled": false, + "Cors_Enabled": true, + "RabbitMQ_Enabled": true, + "Redis_Enabled": true, + "RedisSeed_Enabled": true, + "Kafka_Enabled": false, + "ElasticSeach_Enabled": false, + "MutiDB_Enabled": false, + "SMS_Enabled": true, + "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], + "DbSelect": "Mysql", + + "DbConn": { + "WriteUrl": "server=118.195.191.41;port=3306;database=YIDB;user id=root;password=Qz52013142020.", + "ReadUrl": [ + "server=118.195.191.41;port=3306;database=YIDB;user id=root;password=Qz52013142020.", + "server=118.195.191.41;port=3306;database=YIDB;user id=root;password=Qz52013142020.", + "server=118.195.191.41;port=3306;database=YIDB;user id=root;password=Qz52013142020." + ] + }, + "JWTTokenOptions": { + "Audience": "http://localhost:7000", + "Issuer": "http://localhost:7000", + "SecurityKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI2a2EJ7m872v0afyoSDJT2o1+SitIeJSWtLJU8/Wz2m7gStexajkeD+Lka6DSTy8gt9UwfgVQo6uKjVLG5Ex7PiGOODVqAEghBuS7JzIYU5RvI543nNDAPfnJsas96mSA7L/mD7RTE2drj6hf3oZjJpMPZUQI/B1Qjb5H3K3PNwIDAQAB" + }, + "RedisConnOptions": { + "Host": "118.195.191.41", + "Prot": 6379, + "DB": 1, + "Password": "Qz52013142020." + }, + "RabbitConn": { + "HostName": "118.195.191.41", + "UserName": "cc", + "Password": "cc", + "Port": 5672 + }, + "ElasticSeachConn": { + "Url": "", + "IndexName": "" + }, + "KafkaOptions": { + "BrokerList": "192.168.3.230:9092", + "TopicName": "kafkalog" + }, + "ConsulClientOption": { + "IP": "118.195.191.41", + "Port": "8500", + "Datacenter": "dc1" + }, + "ConsulRegisterOption": { + "IP": "183.216.18.15", + "Port": "44329", + "GroupName": "ApiMicroservice", + "HealthCheckUrl": "/Health", + "Interval": 10, + "Timeout": 5, + "DeregisterCriticalServiceAfter": 60, + "Tag": "13" + }, + "SMS": { + "ID": "LTAI5tJvjPaXCyyPMfXLNbVA", + "Secret": "fLQv7jjj57fUKLFK8REeAQPFVDjUYn", + "Sign": "JiftCC", + "Template": "SMS_221640732" + }, + "IPLibraryServiceUrl": "http://gRPCIPLibraryService" +} \ No newline at end of file diff --git a/Yi.Framework/Yi.Framework.ElasticSearchProcessor/configuration.json b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/configuration.json new file mode 100644 index 00000000..3fa71f14 --- /dev/null +++ b/Yi.Framework/Yi.Framework.ElasticSearchProcessor/configuration.json @@ -0,0 +1,8 @@ +{ + "Apollo": { + "AppId": "Yi.Framework.ApiMicroservice", + "Env": "DEV", + "MetaServer": "http://192.168.2.168:8080", + "ConfigServer": [ "http://192.168.2.168:8080" ] + } +} \ No newline at end of file diff --git a/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs b/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs index 0ecec572..313fce11 100644 --- a/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs +++ b/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/ApolloExtension.cs @@ -27,8 +27,6 @@ namespace Yi.Framework.WebCore.BuilderExtend { var apolloBuilder = builder.AddApollo(root.GetSection("apollo")).AddDefault(); - - foreach (var item in NameSpace) { apolloBuilder.AddNamespace(item, ConfigFileFormat.Json); diff --git a/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs b/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs new file mode 100644 index 00000000..0f00c052 --- /dev/null +++ b/Yi.Framework/Yi.Framework.WebCore/BuilderExtend/JsonFileExtension.cs @@ -0,0 +1,32 @@ +锘縰sing Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.BuilderExtend +{ + public static class JsonFileExtension + { + public static void AddJsonFileService(this IConfigurationBuilder builder, params string[] JsonFile) + { + if (JsonFile==null) + { + string[] myJsonFile = new string[] { "appsettings.json", "configuration.json" }; + foreach (var item in myJsonFile) + { + builder.AddJsonFile(item, optional: true, reloadOnChange: false); + } + } + else + { + foreach (var item in JsonFile) + { + builder.AddJsonFile(item, optional: true, reloadOnChange: false); + } + } + + } + } +} diff --git a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ElasticSeachExtend.cs b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ElasticSeachExtend.cs new file mode 100644 index 00000000..cfdaff61 --- /dev/null +++ b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/ElasticSeachExtend.cs @@ -0,0 +1,27 @@ +锘縰sing Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using System; +using System.IO; +using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Core; + +namespace Yi.Framework.WebCore.MiddlewareExtend +{ + /// + /// Redis鎵╁睍 + /// + public static class ElasticSeachExtend + { + public static IServiceCollection AddElasticSeachService(this IServiceCollection services) + { + if (Appsettings.appBool("ElasticSeach_Enabled")) + { + services.Configure(Appsettings.appConfiguration("ElasticSeachConn")); + services.AddTransient(); + } + return services; + + } + } +}