Spring Boot管理控制台:应用程序特定的电子邮件通知

4
我们使用Spring Boot Admin Server来监控我们的Spring Boot应用程序,并使用Slack和电子邮件通知。
spring.boot.admin.notify.slack.enabled: true
...
spring.boot.admin.notify.mail.enabled: true

是否可以为每个应用程序定义单独的通知电子邮件接收者,例如以下内容:

spring.boot.admin.notify.mail.enabled.app1: true
spring.boot.admin.notify.mail.to.app1: app1-notifier@gmail.de
spring.boot.admin.notify.mail.enabled.app2: true
spring.boot.admin.notify.mail.to.app2: app2-notifier@gmail.de
1个回答

4
根据SBA文件,我们可能无法按要求实现特定于客户端的通知(至少目前不行)。我已经查看了代码,并分享了一些简要说明,包括当前的工作方式以及使用相同方法自定义实现的可能方式。

它是如何工作的?

spring.boot.admin.notify.mail

SBA 使用 AdminServerNotifierAutoConfiguration 实现通知器,该配置基于我们为通知定义的属性进行基本配置。对于邮件服务,它具有 MailNotifierConfiguration 和 TemplateEngine,其中 mailNotifierTemplateEngine 如下所示。
     @Bean
     @ConditionalOnMissingBean
     @ConfigurationProperties("spring.boot.admin.notify.mail")
     public MailNotifier mailNotifier(JavaMailSender mailSender, InstanceRepository repository) {
         return new MailNotifier(mailSender, repository, mailNotifierTemplateEngine());
     }

    @Bean
    public TemplateEngine mailNotifierTemplateEngine() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(this.applicationContext);
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding(StandardCharsets.UTF_8.name());

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(resolver);
        return templateEngine;
    }

spring.boot.admin.notify.mail.to
spring.boot.admin.notify.mail.from

MailNotifier包含基本的邮件细节,如发件人、收件人附加信息等,以及默认的邮件模板,该模板扩展了AbstractStatusChangeNotifier,负责应用程序状态更新。

我已经为此创建了一个增强请求,在这里


方法一

步骤1:创建客户端特定的MailNotifier

您也可以在MailNotifier内创建子类。

public class MailNotifierClient1 extends AbstractStatusChangeNotifier {

    private final JavaMailSender mailSender;

    private final TemplateEngine templateEngine;
    
    private String[] to = { "root@localhost" };
    private String[] cc = {};
    private String from = "Spring Boot Admin <noreply@localhost>";
    private Map<String, Object> additionalProperties = new HashMap<>();
    @Nullable private String baseUrl;
    private String template = "classpath:/META-INF/spring-boot-admin-server/mail/status-changed.html";

    public MailNotifierClient1(JavaMailSender mailSender, InstanceRepository repository, TemplateEngine templateEngine) {
        super(repository);
        this.mailSender = mailSender;
        this.templateEngine = templateEngine;
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            Context ctx = new Context();
            ctx.setVariables(additionalProperties);
            ctx.setVariable("baseUrl", this.baseUrl);
            ctx.setVariable("event", event);
            ctx.setVariable("instance", instance);
            ctx.setVariable("lastStatus", getLastStatus(event.getInstance()));

            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, StandardCharsets.UTF_8.name());
                message.setText(getBody(ctx).replaceAll("\\s+\\n", "\n"), true);
                message.setSubject(getSubject(ctx));
                message.setTo(this.to);
                message.setCc(this.cc);
                message.setFrom(this.from);
                mailSender.send(mimeMessage);
            }
            catch (MessagingException ex) {
                throw new RuntimeException("Error sending mail notification", ex);
            }
        });
    }

    protected String getBody(Context ctx) {
        return templateEngine.process(this.template, ctx);
    }

步骤2:为特定客户端和属性映射AdminServerNotifierAutoConfiguration

        @Bean
        @ConditionalOnMissingBean
        @ConfigurationProperties("spring.boot.admin.notify.mail.client1")
        public MailNotifierClient1 mailNotifierClient1(JavaMailSender mailSender, InstanceRepository repository) {
            return new MailNotifierClient1(mailSender, repository, mailNotifierTemplateEngine());
        }

方法二

在这里,您可以使用InstanceEvent、Instance(使用包含基本客户端详细信息的Registration)域来添加修改doNotify方法,并将邮件收件人设置到字段中。您可以根据自己的要求更改条件。这样,您就不必创建其他类/子类。

@Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            Context ctx = new Context();
            ctx.setVariables(additionalProperties);
            ctx.setVariable("baseUrl", this.baseUrl);
            ctx.setVariable("event", event);
            ctx.setVariable("instance", instance);
            ctx.setVariable("lastStatus", getLastStatus(event.getInstance()));

            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, StandardCharsets.UTF_8.name());
                message.setText(getBody(ctx).replaceAll("\\s+\\n", "\n"), true);
                message.setSubject(getSubject(ctx));
                message.setCc(this.cc);
                message.setFrom(this.from);
                if(instance.getRegistration().getName().equals("client1")) {
                    message.setTo("client1.to");
                }
                mailSender.send(mimeMessage);
            }
            catch (MessagingException ex) {
                throw new RuntimeException("Error sending mail notification", ex);
            }
        });
    }

如果我们不想自定义SBA,则实现要求的另一种方法是为每个客户创建单独的SBA。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接