当前位置: 首页 > 知识库问答 >
问题:

Spring Boot Admin Server:应用程序特定的电子邮件通知

黄泰宁
2023-03-14

我们使用Spring Boot Admin Server并使用Slack和电子邮件通知监控我们的Spring Boot应用程序

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个答案

龚浩宕
2023-03-14

根据SBA文档,我们可能不会按要求实施特定于客户端的通知(至少现在不会)。我已经浏览了代码并分享了一些简要的当前工作原理以及使用代码自定义实现的可能方法,

spring.boot.admin.notify.mail代码

SBA使用AdminServerNotifierAutoConfiguration实现通知程序,该配置基于我们为通知定义的属性进行基本配置。对于邮件服务,它具有MailNotifierConfiguration和模板引擎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(AbstractStatusChangeNotifier)。

我已经在这里创建了一个相同的增强请求

步骤1:创建特定于客户端的邮件通知程序

您还可以在邮件通知程序中创建子类。

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(使用包含基本客户端详细信息的注册)域添加modify(修改)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

 类似资料: