在邮件通知中显示环境Spring Boot Admin

时间:2019-06-17 13:37:41

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

我有一组用spring-boot-admin监视和检测的应用程序。万一发生故障,我会通过邮件和闲暇收到通知。 我想在消息/电子邮件中显示出发生问题的环境。

这是我用于电子邮件的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>

这是目前的显示方式

Notification

这是我来自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

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

Notification_2

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

如何实现将该变量放入Thymeleaf模板中?如果还有其他方法,那就完全欢迎。

1 个答案:

答案 0 :(得分:0)

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

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

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