NETCORE - Consul 注册
NETCORE - Consul 注册
1. 部署Consul服务,参考:https://www.cnblogs.com/1285026182YUAN/p/13847705.html
2. 创建NetCore Web Api 项目,框架:net5:NETCORE.ConSul
3. 引用nuget包
4. 修改配置文件
"ConsulSetting": { "ServiceName": "TestService", "ServiceIP": "10.10.50.99", //服务地址 "ServiceHealthCheck": "/healthcheck", "ServicePort": "5000",//服务端口 "ConsulAddress": "http://192.168.91.128:8500/", //consul地址 "ConsulCenter": "dc1" }
5. 增加扩展类
using Consul; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; public static class ConsulExtensions { public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IConfiguration configuration)//, IHostApplicationLifetime lifetime) { var consulClient = new ConsulClient(c => { c.Address = new Uri(configuration["ConsulSetting:ConsulAddress"]); }); var registration = new AgentServiceRegistration() { ID = Guid.NewGuid().ToString(),//服务实例唯一标识 Name = configuration["ConsulSetting:ServiceName"],//服务名 Address = configuration["ConsulSetting:ServiceIP"], //服务IP Port = int.Parse(configuration["ConsulSetting:ServicePort"]),//服务端口 因为要运行多个实例,端口不能在appsettings.json里配置,在docker容器运行时传入 Check = new AgentServiceCheck() { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后注册 Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔 HTTP = $"http://{configuration["ConsulSetting:ServiceIP"]}:{configuration["ConsulSetting:ServicePort"]}{configuration["ConsulSetting:ServiceHealthCheck"]}",//健康检查地址 Timeout = TimeSpan.FromSeconds(5)//超时时间 } }; //服务注册 consulClient.Agent.ServiceRegister(registration).Wait(); ////应用程序终止时,手动取消注册 //lifetime.ApplicationStopping.Register(() => //{ // consulClient.Agent.ServiceDeregister(registration.ID).Wait(); //}); return app; } }
6. 在Starup里面注册Consul服务
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NETCORE.ConSul v1")); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // Consul注册 app.RegisterConsul(Configuration); }
7. 如调试模式,注意修改IP地址
8. 增加 webapi 控制器:BookController.cs
接口访问地址:https://10.10.50.99:5001/api/Book/GetInfo
namespace NETCORE.ConSul.Controllers { [Route("api/[controller]")] [ApiController] public class BookController : ControllerBase { [Route("GetInfo")] [HttpGet] public IActionResult GetInfo() { Task.Run(() => { Console.WriteLine("name:onn , Host:{0}", HttpContext.Request.Host.Value); }); return Ok("ok , task running go !"); } } }
9. 增加 webapi 控制器:HealthController.cs
namespace NETCORE.ConSul.Controllers { [Route("api/[controller]")] [ApiController] public class HealthController : ControllerBase { [HttpGet] public IActionResult Get() => Ok("ok"); } }
docker 环境下的consul,访问主机上的健康检查接口失败,暂未找到原因,估计是网段问题。
参考:https://www.cnblogs.com/xiaoquangege/p/13812271.html
项目:NETCORE.ConSul
附代码:https://gitee.com/wuxincaicai/NETCORE.git