Spring Boot Admin 发送监控邮件

  最近在弄关于运行中的服务的Jvm监控,经过同事和自己搜索发现目前主要有两种方案,普罗米修斯和 Spring Boot Admin,由于我们的系统是基于 Spring Cloud 的,所有可能搜索的都与 Spring Cloud 比较容易整合的。

   Spring Boot Admin 是比较容易接入的,只要将加上 Spring Boot Admin 依赖简单配置一下,然后注册到 Spring Boot 的注册中心,并且其他应用都是对外有提供监控指标的话,进入 Spring Boot Admin 即可,看到每个应用的 Jvm 和一些配置信息,还可以线上设置每个类的日志打印级别。不过要注意一下,如果数据库和 redis 等连接不通的话,可能会打印出大量错误日志,邮件也会被检测到是否能链接通,不过邮件这里的检测好像是有问题(我自己能发送邮件,但是 其检测还是链接不同,原因还在找中),最好是禁用掉邮件的检测。具体要监控的配置 Spring Boot 应用可以参考后面的配置例子。

   普罗米修斯 功能也差不多,不过功能会更加强大一些,目前我也还没接入,待接入后,再来补充,目前了解到其除了可以监控Jvm状态,可以监控每个url的请求时间跟链路监控类似。

配置邮件监控(翻译官方文档)

其实这个是比较简单,主要是里面有些坑主要注意。

  1. 添加 Spring Boot 的mail依赖

    org.springframework.boot
    spring-boot-starter-mail


  1. 配置发送邮件的账号和密码,和接受的通知的邮箱
spring:
  mail:
    host: smtp.qq.com
    username: [email protected]
    password: 123456
    properties:
      mail:
        smtp:
          auth: true
  boot:
    admin:
      notify:
        mail:
          to: ["[email protected]"]
          from: [email protected]  # 这个必须要配置,并且和 Spring.mail.username的一样,否则会报错,因为其源码默认使用的 Spring Boot Admin 

注意事项

  1. Spring Boot Admin 在配置邮件的时候,要注意配置的发件人和spring.mail.user的是一样的,否则可能 报 553 mailException

附录

要监控的 Spring Boot 应用的对外暴露监控配置(供参考):

management:
  health:
    db:
      enabled: false
    mail:
      enabled: false   # 这个最好必须配置,否则如果有Spring Boot Admin 不断检测的话,可能导致邮件不能发送成功
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

Spring Boot Admin 的配置参考(供参考):

spring:
  security:
    user:
      name: 123456
      password: 123456
  mail:
    host: smtp.qq.com
    username: [email protected]
    password: 123456
    default-encoding: UTF-8
    properties:
      mail:
        smtp:
          connectiontimeout: 600000
          timeout: 600000
          writetimeout: 600000
          auth: true
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory
            port: 25
  boot:
    admin:
      notify:
        mail:
          to: ["[email protected]"]
          from: [email protected]  # 这个必须要配置,并且和 Spring.mail.username的一样

参考

  1. https://blog.csdn.net/qq_20597149/article/details/78463538
  2. https://codecentric.github.io/spring-boot-admin/current/#mail-notifications 官方说明如何配置Spring Boot Admin 的邮件监控。 Spring Boot Admin 的版本为:2.1.6

你可能感兴趣的:(java,spring)