添加es服务扩展

This commit is contained in:
橙子
2021-11-09 18:50:49 +08:00
parent 6d5f2cbae7
commit c50bc97c51
15 changed files with 490 additions and 11 deletions

View File

@@ -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<6C><6F><EFBFBD><EFBFBD>
#endregion

View File

@@ -70,6 +70,11 @@ namespace Yi.Framework.ApiMicroservice
#endregion
services.AddRabbitMQService();
#region
//ElasticSeach<63><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#endregion
services.AddElasticSeachService();
#region
//<2F><><EFBFBD>ŷ<EFBFBD><C5B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#endregion

View File

@@ -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"

View File

@@ -1,8 +0,0 @@
using System;
namespace Yi.Framework.ElasticSearchProcessor
{
public class Class1
{
}
}

View File

@@ -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<InitESIndexWorker> _logger;
private readonly RabbitMQInvoker _RabbitMQInvoker;
private readonly ElasticSearchInvoker _elasticSearchInvoker;
public InitESIndexWorker(ILogger<InitESIndexWorker> 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
{
//<2F><><EFBFBD>õ<EFBFBD>ģ<EFBFBD>͡<EFBFBD>
//SPUCQRSQueueModel spuCQRSQueueModel = JsonConvert.DeserializeObject<SPUCQRSQueueModel>(message);
//<2F><><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ɾ<EFBFBD>ģ<EFBFBD>es<65><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>
//switch (spuCQRSQueueModel.CQRSType)
//{
//case (int)SPUCQRSQueueModelType.Insert:
//case (int)SPUCQRSQueueModelType.Update:
// {
// Goods goods = this._ISearchService.GetGoodsBySpuId(spuCQRSQueueModel.SpuId);
// this._IElasticSearchService.InsertOrUpdata<Goods>(goods);
// break;
// }
//case (int)SPUCQRSQueueModelType.Delete:
// this._IElasticSearchService.Delete<Goods>(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 = "<22><>ʱ<EFBFBD><CAB1>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־"
};
this._logger.LogError(ex, $"{nameof(InitESIndexWorker)}.Init ESIndex failed message={message}, Exception:{ex.Message}", JsonConvert.SerializeObject(logModel));
return false;
}
});
await Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<!-- 将日志以回滚文件的形式写到文件中 -->
<!-- 按日期切分日志文件,并将日期作为日志文件的名字 -->
<!--Error-->
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<!--不加utf-8编码格式中文字符将显示成乱码-->
<param name="Encoding" value="utf-8" />
<file value="log/"/>
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
<datePattern value="&quot;GlobalExceptionLogs_&quot;yyyyMMdd&quot;.log&quot;" />
<!--日志文件名是否为静态-->
<StaticLogFileName value="false"/>
<!--多线程时采用最小锁定-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--布局(向用户显示最后经过格式化的输出信息)-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date| %-5level %newline%message%newline--------------------------------%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<!--Error-->
<!--Info-->
<appender name="InfoLog" type="log4net.Appender.RollingFileAppender">
<!--不加utf-8编码格式中文字符将显示成乱码-->
<param name="Encoding" value="utf-8" />
<!--定义文件存放位置-->
<file value="log/"/>
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--日志文件名是否为静态-->
<StaticLogFileName value="false"/>
<!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
<datePattern value="&quot;GlobalInfoLogs_&quot;yyyyMMdd&quot;.log&quot;" />
<!--多线程时采用最小锁定-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--布局(向用户显示最后经过格式化的输出信息)-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date| %-5level%c %newline%message%newline--------------------------------%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="WARN" />
</filter>
</appender>
<!--Info-->
<root>
<!-- 控制级别由低到高ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF -->
<!-- 比如定义级别为INFO则INFO级别向下的级别比如DEBUG日志将不会被记录 -->
<!-- 如果没有定义LEVEL的值则缺省为DEBUG -->
<level value="ALL" />
<!-- 按日期切分日志文件,并将日期作为日志文件的名字 -->
<appender-ref ref="ErrorLog" />
<appender-ref ref="InfoLog" />
</root>
</log4net>

View File

@@ -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<IConfiguration>();
#region
//Ioc配置
#endregion
services.AddSingleton(new Appsettings(configuration));
services.AddHostedService<Worker>();
services.AddHostedService<InitESIndexWorker>();
services.AddHostedService<WarmupESIndexWorker>();
#region
//services.Configure<MySqlConnOptions>(configuration.GetSection("MysqlConn"));
#region
//RabbitMQ服务配置
#endregion
services.AddRabbitMQService();
#endregion
#region Consul
//services.Configure<ConsulClientOption>(configuration.GetSection("ConsulClientOption"));
//services.AddTransient<AbstractConsulDispatcher, PollingDispatcher>();
#endregion
});
}
}

View File

@@ -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<WarmupESIndexWorker> _logger;
private readonly RabbitMQInvoker _RabbitMQInvoker;
private readonly ElasticSearchInvoker _elasticSearchInvoker;
private readonly IOptionsMonitor<ElasticSearchOptions> _ElasticSearchOptions = null;
public WarmupESIndexWorker(ILogger<WarmupESIndexWorker> logger, RabbitMQInvoker rabbitMQInvoker, IConfiguration configuration, ElasticSearchInvoker elasticSearchInvoker, IOptionsMonitor<ElasticSearchOptions> 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<SKUWarmupQueueModel>(message);
//<2F><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>ģ<EFBFBD>͡<EFBFBD>
#region <EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>Index---<EFBFBD>½<EFBFBD>Index---<EFBFBD>ٽ<EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
try
{
this._elasticSearchInvoker.DropIndex(this._ElasticSearchOptions.CurrentValue.IndexName);
//this._ISearchService.ImpDataBySpu();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>es<65><73><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
this._logger.LogInformation($"{nameof(WarmupESIndexWorker)}.InitAll succeed");
return true;
}
catch (Exception ex)
{
var logModel = new LogModel()
{
OriginalClassName = this.GetType().FullName,
OriginalMethodName = nameof(ExecuteAsync),
Remark = "<22><>ʱ<EFBFBD><CAB1>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־"
};
this._logger.LogError(ex, $"{nameof(WarmupESIndexWorker)}.Warmup ESIndex failed message={message}, Exception:{ex.Message}", JsonConvert.SerializeObject(logModel));
return false;
}
}
#endregion
});
await Task.CompletedTask;
}
}
}

View File

@@ -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<Worker> _logger;
public Worker(ILogger<Worker> 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);
}
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -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"
}

View File

@@ -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" ]
}
}

View File

@@ -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);

View File

@@ -0,0 +1,32 @@
using 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);
}
}
}
}
}

View File

@@ -0,0 +1,27 @@
using 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
{
/// <summary>
/// Redis扩展
/// </summary>
public static class ElasticSeachExtend
{
public static IServiceCollection AddElasticSeachService(this IServiceCollection services)
{
if (Appsettings.appBool("ElasticSeach_Enabled"))
{
services.Configure<ElasticSearchOptions>(Appsettings.appConfiguration("ElasticSeachConn"));
services.AddTransient<ElasticSearchInvoker>();
}
return services;
}
}
}