有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Show Environment in mail notification 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的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) 个答案

  1. # 1 楼答案

    你可以查看here来定制邮件模板

    如果只显示环境名称是必需的,您可以考虑使用^ {CD2>}属性注册客户机作为^ {CD1>},避免凌驾于TyeleleAF模板的麻烦。p>

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