.Net6 Furion框架配置Consul
1、安装Consul
Windows系统可参考:《Windows安装Consul并配置为Windows服务》
2、搭建Furion
使用的Sqlsugar的Webfirst生成的Furion,因此构成如下:
Furion.Core
Furion.Application
Furion.Web.Core
Furion.Web.Entry
3、配置
3.1 Furion.Web.Core项目配置
添加依赖:
Install-Package Consul
修改appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Consul": {
"Address": "http://ip或域名:8500",
"CheckUrl": "/api/checkhealth",
"DataCenter": "dc1"
}
}
添加Helper
ConsulHelper.cs
public static class ConsulHandler
{
public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IHostApplicationLifetime lifetime,IConfiguration cfg)
{
//var consulClient = new ConsulClient(x => x.Address = new Uri($"{cfg["Consul:Scheme"]}://{cfg["Consul:ServiceIP"]}:{cfg["Consul:ServicePort"]}"));
var consulClient = new ConsulClient(x => x.Address = new Uri(cfg["Consul:Address"]));
string port = cfg["Consul:ServicePort"];
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务器启动5秒后注册
Interval = TimeSpan.FromMinutes(1),//每分钟检测一次(健康检查间隔时间)
HTTP = $"{cfg["Consul:Scheme"]}://{cfg["Consul:ServiceIP"]}:{port}{cfg["Consul:CheckUrl"]}",//本服务健康检查地址
Timeout = TimeSpan.FromSeconds(20),
};
var registerAgent = new AgentServiceRegistration()
{
Check = httpCheck,
Checks = new[] { httpCheck },
ID = cfg["Consul:ServiceId"],// serviceConfig.ServiceId,//一定要指定服务ID,否则每次都会创建一个新的服务节点
Name = cfg["Consul:ServiceName"],// serviceConfig.ServiceName,
Address = cfg["Consul:ServiceIP"], // serviceConfig.ServiceIP,
Port = int.Parse(port)//,
//Tags = new[] { $"urlprefix-/{serviceConfig.ServiceName}" }//添加 urlprefix-/servicename 格式的tag标签,以便Fabio识别
};
consulClient.Agent.ServiceRegister(registerAgent).Wait();//服务启动时注册,使用Consul API进行注册(HttpClient发起)
lifetime.ApplicationStopped.Register(() =>
{
consulClient.Agent.ServiceDeregister(registerAgent.ID).Wait();//应用停止时自动注销consul服务注册,个人暂时将本代码注释掉,转而设置consul配置,参考《consul中.net 6服务被下线问题》
});
return app;
}
Startup.cs配置:
public void Configure(IApplicationBuilder app,IConfiguration configuration, IHostApplicationLifetime lifetime, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCorsAccessor();
app.RegisterConsul(lifetime,configuration);
app.UseAuthentication();
app.UseAuthorization();
app.UseInject(string.Empty);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
3.2 Furion.Web.Entry配置
appsettings.json配置:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Consul": {
"ServiceId": "分配个服务ID",
"ServiceName": "服务名称",
"ServicePort": "8000",
"Scheme": "http",
"ServiceIP": "IP或域名"
}
}
新建api或添加action
ConsulApi.cs
using Microsoft.AspNetCore.Mvc;
[DynamicApiController]
public class ConsulApi
{
[NonUnify]
[HttpGet("api/checkhealth")]
public string Test()
{
return "ok";
}
}
至此完成Furion配置Consul操作,如果需要新增其他服务,只需要新建webapi项目,按照(3)步骤操作修改相应的配置即可!注意创建项目时不用勾选OpenApi,因为Furion本身就有Swagger配置。