【问题标题】:Show Environment in mail notification Spring Boot Admin在邮件通知 Spring Boot Admin 中显示环境
【发布时间】:2019-06-17 13:37:41
【问题描述】:

我有一组使用spring-boot-admin 监控和检测的应用程序。如果其中一个出现故障,我会通过邮件和 slack 收到通知。 我会在消息/电子邮件中显示我得到了问题发生的环境。

这是我用于电子邮件的thymeleaf 模板

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <style>
        h1, h2, h3, h4, h5, h6 {
            font-weight: 400
        }
        ul {
            list-style: none
        }
        html {
            box-sizing: border-box
        }
        *, :after, :before {
            box-sizing: inherit
        }
        table {
            border-collapse: collapse;
            border-spacing: 0
        }
        td, th {
            text-align: left
        }
        body, button {
            font-family: BlinkMacSystemFont, -apple-system, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Helvetica, Arial, sans-serif
        }
        code, pre {
            -moz-osx-font-smoothing: auto;
            -webkit-font-smoothing: auto;
            font-family: monospace
        }
    </style>
</head>
<body>
    <th:block th:remove="all">
        <!-- This block will not appear in the body and is used for the subject -->
        <th:block th:remove="tag" th:fragment="subject">
            [Admin-server] - [[${instance.registration.name}]] is 
  [[${event.statusInfo.status}]]
        </th:block>
    </th:block>
    <h1>
        <span th:text="${instance.registration.name}"/>
        (<span th:text="${instance.id}"/>)
        está <span th:text="${event.statusInfo.status}"/>
    </h1>
    <p>
        The instance
        <a th:if="${baseUrl}" th:href="@{${baseUrl + '/#/instances/' + 
instance.id + '/'}}"><span th:text="${instance.id}"/></a>
        <span th:unless="${baseUrl}" th:text="${instance.id}"/>
        has changed its status from
        <span th:text="${lastStatus}"/> to <span 
th:text="${event.statusInfo.status}"/>
    </p>

    <h2>Details</h2>
    <dl th:fragment="statusDetails" th:with="details = ${details ?: 
event.statusInfo.details}">
        <th:block th:each="detail : ${details}">
            <dt th:text="${detail.key}"/>
            <dd th:unless="${detail.value instanceof T(java.util.Map)}" 
 th:text="${detail.value}"/>
            <dd th:if="${detail.value instanceof T(java.util.Map)}">
                <dl th:replace="${#execInfo.templateName} :: statusDetails 
 (details = ${detail.value})"/>
            </dd>
        </th:block>
    </dl>
</body>
</html>

这是目前的显示方式

这是我来自 Spring Admin 的 application.yml

server:
port: ${admin-server.port:8086}
tomcat:
remote-ip-header: x-forwarded-for
protocol-header: x-forwarded-proto
spring:
application:
name: admin-server
security:
user:
  name: ${spring-security.admin.username}
  password:  ${spring-security.admin.password}
cloud:
kubernetes:
  reload:
    enabled: true
boot:
admin:
  discovery:
    ignored-services: admin-server
  notify:
    mail:
      enabled: ${admin-mail.enabled:true}
      to: ${admin-mail.recipients}
      from: ${admin-mail.from}
      template: classpath:/template/status-changed.html
      ignore-changes: OFFLINE:UP, DOWN:UP
    slack:
      webhook-url: ${admin-slack.webhook}
      ignore-changes: OFFLINE:UP, DOWN:UP
      enabled: true
mail:
test-connection: false
host: ********
port: 25
properties:
  mail:
    smtp:
      connectiontimeout: 5000
      timeout: 3000
      writetimeout: 5000
    debug: ${admin-mail.debug:false}
management:
endpoint:
restart:
  enabled: true

这就是我希望它显示的方式

我目前有另一个属性文件(因为我使用配置服务器),其中有一个变量说明应用程序所在的环境。

如何将该变量放入 Thymeleaf 模板?如果有任何其他方式,完全欢迎。

【问题讨论】:

    标签: java spring spring-boot thymeleaf spring-boot-admin


    【解决方案1】:

    您可以查看here 自定义邮件模板。

    如果只要求显示环境名称,您可以考虑使用spring.boot.admin.client.instance.name 属性将您的客户端注册为Client Name (${ENV}),并避免覆盖thymeleaf 模板的麻烦。

    我对我的应用程序监控使用了相同的方法。此外,可以使用包含环境名称的 ${ENV} 变量为每个环境自定义来自 id 的邮件通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 2023-01-28
      相关资源
      最近更新 更多