根据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 包含基本的邮件详细信息,例如 from、to 和 additional info 等,以及扩展了负责的 AbstractStatusChangeNotifier 的默认邮件模板用于更新应用程序的状态。
我为同一个here创建了一个增强请求
方式 1
第 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());
}
方式 2
在这里,您可以使用InstanceEvent、Instance(使用由基本客户端详细信息组成的注册)域添加修改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