其他分享
首页 > 其他分享> > Consul服务告警之Watch机制

Consul服务告警之Watch机制

作者:互联网

 熔断保护在Consul和Ocelot中都有实现,意思就是当一个服务不正常时(比如我们的一个服务实例挂了,Consul的健康检查机制检测到了),应该给系统维护人员给以告警。在Consul中,服务告警也是通过配置文件来实现的。

3.1 添加watch.json配置文件

复制代码
{
  "watches": [
    {
      "type": "checks",
      "handler_type": "http",
      "state": "critical",
      "http_handler_config": {
        "path": "http://192.168.80.71:9000/notice",
        "method": "POST",
        "timeout": "10s",
        "header": { "Authorization": [ "token" ] }
      }
    }
  ]
}
复制代码

  *.有关watch的细节,请参考:https://www.consul.io/docs/agent/watches.html

  这里编辑完成之后,就可以放到config目录下了,后面重启Consul Client Agent服务时会加载新的watches_config.json配置文件。

  

3.2 添加NoticeService服务

 新写一个ASP.NET Core WebAPI程序,其主要功能就是接受Consul POST过来的参数并调用方法发送电子邮件。

  (1)Controller编写

复制代码
    [Route("api/[controller]")]
    public class HomeController : Controller
    {
        public IConfiguration Configuration { get; }

        public HomeController(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        [HttpPost("/notice")]
        public IActionResult Notice()
        {
            var bytes = new byte[10240];
            var i = Request.Body.ReadAsync(bytes, 0, bytes.Length);
            var content = System.Text.Encoding.UTF8.GetString(bytes).Trim('\0');

            EmailSettings settings = new EmailSettings()
            {
                SmtpServer = Configuration["Email:SmtpServer"],
                SmtpPort = Convert.ToInt32(Configuration["Email:SmtpPort"]),
                AuthAccount = Configuration["Email:AuthAccount"],
                AuthPassword = Configuration["Email:AuthPassword"],
                ToWho = Configuration["Email:ToWho"],
                ToAccount = Configuration["Email:ToAccount"],
                FromWho = Configuration["Email:FromWho"],
                FromAccount = Configuration["Email:FromAccount"],
                Subject = Configuration["Email:Subject"]
            };

            EmailHelper.SendHealthEmail(settings, content);

            return Ok();
        }
    }
复制代码

  不再解释这段代码。

  (2)SendHealthEmail方法编写

复制代码
    public class EmailHelper
    {
        public static void SendHealthEmail(EmailSettings settings, string content)
        {
            try
            {
                dynamic list = JsonConvert.DeserializeObject(content);
                if (list != null && list.Count > 0)
                {
                    var emailBody = new StringBuilder("健康检查故障:\r\n");
                    foreach (var noticy in list)
                    {
                        emailBody.AppendLine($"--------------------------------------");
                        emailBody.AppendLine($"Node:{noticy.Node}");
                        emailBody.AppendLine($"Service ID:{noticy.ServiceID}");
                        emailBody.AppendLine($"Service Name:{noticy.ServiceName}");
                        emailBody.AppendLine($"Check ID:{noticy.CheckID}");
                        emailBody.AppendLine($"Check Name:{noticy.Name}");
                        emailBody.AppendLine($"Check Status:{noticy.Status}");
                        emailBody.AppendLine($"Check Output:{noticy.Output}");
                        emailBody.AppendLine($"--------------------------------------");
                    }

                    var message = new MimeMessage();
                    message.From.Add(new MailboxAddress(settings.FromWho, settings.FromAccount));
                    message.To.Add(new MailboxAddress(settings.ToWho, settings.ToAccount));

                    message.Subject = settings.Subject;
                    message.Body = new TextPart("plain") { Text = emailBody.ToString() };
                    using (var client = new SmtpClient())
                    {
                        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                        client.Connect(settings.SmtpServer, settings.SmtpPort, false);
                        client.AuthenticationMechanisms.Remove("XOAUTH2");
                        client.Authenticate(settings.AuthAccount, settings.AuthPassword);
                        client.Send(message);
                        client.Disconnect(true);
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
复制代码

  这里使用的是MailKit库(支持.net core),可以通过NuGet搜索并安装,此外为何接受的参数属性是这些,大家可以看看Consul官方文档中watches页中的checks类型,见下图所示:

  

  (3)发布NoticeService到192.168.80.71服务器中,同样也可以加入Consul配置文件中:

 View Code

  发布完成之后,重启Consul Client节点(192.168.80.71)的Consul服务,可以看到NoticeService也注册成功:

  

3.3 测试服务告警

 (1)手动在IIS中关闭一个ClientService服务,例如:这里我关闭了ClientService.01

  

 (2)查看自动发送的Email内容:从Email中我们可以知道哪个Server节点的哪个Service出了问题,并且可以大概了解原因(Check Output),这时我们的系统维护人员就该起床加班了。

  

  *.需要注意的是确保你的虚拟机可以访问外网,不然是发布出来Email的。

4.小结

  本篇将上篇中遗留的内容进行了弥补,下篇将开始基于Ocelot+Polly的API网关服务实践,敬请期待,我要睡了。

标签:settings,AppendLine,Consul,Watch,emailBody,告警,Configuration,Email
来源: https://www.cnblogs.com/ExMan/p/11907491.html