gpt4 book ai didi

java - Spring Boot 管理服务器 : App Specific Email Notification

转载 作者:行者123 更新时间:2023-12-01 13:10:01 25 4
gpt4 key购买 nike

我们使用 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

最佳答案

根据SBA document ,我们可能不会按要求实现特定于客户的通知(至少现在不是)。我已经浏览了代码并分享了它目前的工作原理以及使用相同的自定义实现的可能方法,

这个怎么运作?
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 的默认邮件模板。它负责应用程序的状态更新。
我为相同的 here 创建了一个增强请求

方式一
第 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());
}

方式二
在这里您可以添加修改 doNotify方法使用 InstanceEvent、Instance(使用由基本客户端详细信息组成的注册)域,并将邮件的收件人设置到该字段中。您可以根据您的要求更改条件。通过这些方式,您不必创建其他类/子类。
@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

关于java - Spring Boot 管理服务器 : App Specific Email Notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60395639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com