10/19更新
This commit is contained in:
@@ -139,7 +139,7 @@ builder.Services.AddHeiCaptcha();
|
|||||||
#endregion
|
#endregion
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
#region
|
#region
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>System.Drawing<6E><67>linu<6E><75>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>
|
||||||
#endregion
|
#endregion
|
||||||
builder.Services.AddSingleton<ThumbnailSharpInvoer>();
|
builder.Services.AddSingleton<ThumbnailSharpInvoer>();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
using Consul;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Common.IOCOptions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 注册ConsulRegisterService 这个servcie在app启动的时候会自动注册服务信息
|
||||||
|
/// </summary>
|
||||||
|
public class ConsulRegisterHostExtend : IHostedService
|
||||||
|
{
|
||||||
|
private readonly ConsulRegisterOption _consulRegisterOptions;
|
||||||
|
private readonly ConsulClientOption _consulClientOptions;
|
||||||
|
public ConsulRegisterHostExtend()
|
||||||
|
{
|
||||||
|
_consulRegisterOptions = Appsettings.app<ConsulRegisterOption>("ConsulRegisterOption");
|
||||||
|
|
||||||
|
_consulClientOptions = Appsettings.app<ConsulClientOption>("ConsulClientOption");
|
||||||
|
}
|
||||||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var httpPort = this._consulRegisterOptions.Port;
|
||||||
|
//var grpcPort = Convert.ToInt32(Appsettings.app("GrpcPort"));
|
||||||
|
//------------------Http------------------
|
||||||
|
using (ConsulClient client = new ConsulClient(c =>
|
||||||
|
{
|
||||||
|
c.Address = new Uri($"http://{this._consulClientOptions.IP}:{this._consulClientOptions.Port}/");
|
||||||
|
c.Datacenter = this._consulClientOptions.Datacenter;
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
var serviceId = $"{this._consulRegisterOptions.IP}:{httpPort}-{this._consulRegisterOptions.GroupName}";
|
||||||
|
|
||||||
|
await client.Agent.ServiceDeregister(serviceId, cancellationToken);
|
||||||
|
|
||||||
|
Console.WriteLine($"开始向Consul注册Http[{serviceId}]服务 ...");
|
||||||
|
|
||||||
|
await client.Agent.ServiceRegister(new AgentServiceRegistration()
|
||||||
|
{
|
||||||
|
ID = serviceId,//唯一Id
|
||||||
|
Name = this._consulRegisterOptions.GroupName,//组名称-Group
|
||||||
|
Address = this._consulRegisterOptions.IP,
|
||||||
|
Port = httpPort,
|
||||||
|
Tags = new string[] { "Http" },
|
||||||
|
Check = new AgentServiceCheck()
|
||||||
|
{
|
||||||
|
Interval = TimeSpan.FromSeconds(this._consulRegisterOptions.Interval),
|
||||||
|
HTTP = $"http://{_consulRegisterOptions.IP}:{httpPort}/Health",
|
||||||
|
//GRPC = $"{this._consulRegisterOptions.IP}:{grpcPort}",//gRPC特有
|
||||||
|
GRPCUseTLS = false,//支持http
|
||||||
|
Timeout = TimeSpan.FromSeconds(this._consulRegisterOptions.Timeout),
|
||||||
|
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(this._consulRegisterOptions.DeregisterCriticalServiceAfter),
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//------------------Grpc------------------
|
||||||
|
//using (ConsulClient client = new ConsulClient(c =>
|
||||||
|
//{
|
||||||
|
// c.Address = new Uri($"http://{this._consulClientOptions.IP}:{this._consulClientOptions.Port}/");
|
||||||
|
// c.Datacenter = this._consulClientOptions.Datacenter;
|
||||||
|
//}))
|
||||||
|
//{
|
||||||
|
// var serviceId = $"{this._consulRegisterOptions.IP}:{grpcPort}-{this._consulRegisterOptions.GrpcGroupName}";
|
||||||
|
|
||||||
|
// await client.Agent.ServiceDeregister(serviceId, cancellationToken);
|
||||||
|
|
||||||
|
// Console.WriteLine($"开始向Consul注册Grpc[{serviceId}]服务 ...");
|
||||||
|
|
||||||
|
// await client.Agent.ServiceRegister(new AgentServiceRegistration()
|
||||||
|
// {
|
||||||
|
// ID = serviceId,//唯一Id
|
||||||
|
// Name = this._consulRegisterOptions.GrpcGroupName,//组名称-Group
|
||||||
|
// Address = this._consulRegisterOptions.IP,
|
||||||
|
// Port = grpcPort,
|
||||||
|
// Tags = new string[] { "Grpc" },
|
||||||
|
// Check = new AgentServiceCheck()
|
||||||
|
// {
|
||||||
|
// Interval = TimeSpan.FromSeconds(this._consulRegisterOptions.Interval),
|
||||||
|
// //HTTP = this._consulRegisterOption.HealthCheckUrl,
|
||||||
|
// GRPC = $"{this._consulRegisterOptions.IP}:{grpcPort}",//gRPC特有
|
||||||
|
// GRPCUseTLS = false,//支持http
|
||||||
|
// Timeout = TimeSpan.FromSeconds(this._consulRegisterOptions.Timeout),
|
||||||
|
// DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(this._consulRegisterOptions.DeregisterCriticalServiceAfter),
|
||||||
|
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 正常注销调用
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var httpPort = this._consulRegisterOptions.Port;
|
||||||
|
//var grpcPort = Appsettings.app<int>("GrpcPort");
|
||||||
|
|
||||||
|
|
||||||
|
using (ConsulClient client = new ConsulClient(c =>
|
||||||
|
{
|
||||||
|
c.Address = new Uri($"http://{this._consulClientOptions.IP}:{this._consulClientOptions.Port}/");
|
||||||
|
c.Datacenter = this._consulClientOptions.Datacenter;
|
||||||
|
})) {
|
||||||
|
|
||||||
|
|
||||||
|
var serviceId = $"{this._consulRegisterOptions.GroupName}:{this._consulRegisterOptions.IP}-{httpPort}";
|
||||||
|
|
||||||
|
//var grpcServiceId = $"{this._consulRegisterOptions.GrpcGroupName}:{this._consulRegisterOptions.IP}-{grpcPort}";
|
||||||
|
|
||||||
|
await client.Agent.ServiceDeregister(serviceId, cancellationToken);
|
||||||
|
//await client.Agent.ServiceDeregister(grpcServiceId, cancellationToken);
|
||||||
|
Console.WriteLine($"开始Consul注销[{serviceId}]服务 ...");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user