Spring 执行器邮件 HealthIndicators 不工作

发布于 2025-01-11 04:40:24 字数 666 浏览 0 评论 0原文

我已经在 Spring Boot 中启用了 Spring 执行器并使用了以下配置,

management.health.mail.enabled=true
management.endpoint.health.show-details=always
[email protected],
spring.mail.port=587

但是每当我启动应用程序时,邮件服务器都不会被检查。在路径 /actuator/health 中:

{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP" ...
    },
    "diskSpace": {
      "status": "UP", ...
    },
    "hazelcast": {
      "status": "UP",   
    },
    "ping": {
      "status": "UP"
    }
  }
}

I have enabled Spring actuator in Spring Boot and used following configuration

management.health.mail.enabled=true
management.endpoint.health.show-details=always
[email protected],
spring.mail.port=587

But whenever I start application, Mail server is not getting checked. in path /actuator/health :

{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP" ...
    },
    "diskSpace": {
      "status": "UP", ...
    },
    "hazelcast": {
      "status": "UP",   
    },
    "ping": {
      "status": "UP"
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-01-18 04:40:24

它肯定会根据您的 Spring Boot 版本和其他依赖项而有所不同,但分析此问题的一种方法是测试。

Spring Boot MailHealthContributorAutoConfiguration 需要几个条件,其中之一是有效的 JavaMailSender 配置。

@AutoConfiguration(after = MailSenderAutoConfiguration.class)
@ConditionalOnClass(JavaMailSenderImpl.class)
@ConditionalOnBean(JavaMailSenderImpl.class)
@ConditionalOnEnabledHealthIndicator("mail")
public class MailHealthContributorAutoConfiguration
...

您尚未提供工作配置,但您可以使用类似的测试类测试您的设置,

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMetrics
@ActiveProfiles("test")
class MonitoringTest {

@LocalServerPort
private int port;

@Autowired
private WebApplicationContext context;

@Test
void healthEndpointWithMail() throws Exception {
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).build();

    MockHttpServletResponse response =      mvc.perform(get("http://localhost:" + port + "/actuator/health")).andReturn().getResponse();

    assertEquals(HttpStatus.OK.value(), response.getStatus());

    System.out.println(response.getContentAsString());
    JSONObject jo = new JSONObject(response.getContentAsString());

    JSONObject componentsJo = jo.getJSONObject("components");
    JSONObject mailJo = componentsJo.getJSONObject("mail");
    assertEquals("UP", mailJo.getString("status"));

} }

请注意 @AutoConfigureMetrics 注释。
配置 application-test.yml 看起来像:

management:
  endpoint:
    health:
      show-details: always
  health:
    mail:
      enabled: true
  endpoints:
    web:
      exposure:
        include:
        - health

和邮件配置部分(使用您的邮件服务器或一些类似 GreenMail):

spring:
  mail:
    default-encoding: UTF-8
    host: #{smtp.host}
    username: #{user}
    password: #{pw}
    port: #{port}
    properties:
      mail:
        smtp:
          auth: true
          ssl:
            enable: true
            trust: #{smtp.host}
          starttls:
            enable: true
    protocol: smtp

看看 满的项目

It can surely vary depending on your Spring Boot Version and other dependencies, but one way to analyze this is a test.

Spring Boot MailHealthContributorAutoConfiguration expects couple of conditions, one of them being a valid JavaMailSender Config.

@AutoConfiguration(after = MailSenderAutoConfiguration.class)
@ConditionalOnClass(JavaMailSenderImpl.class)
@ConditionalOnBean(JavaMailSenderImpl.class)
@ConditionalOnEnabledHealthIndicator("mail")
public class MailHealthContributorAutoConfiguration
...

You have not provided a working configuration, but you can test your setup using a similar test class

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMetrics
@ActiveProfiles("test")
class MonitoringTest {

@LocalServerPort
private int port;

@Autowired
private WebApplicationContext context;

@Test
void healthEndpointWithMail() throws Exception {
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).build();

    MockHttpServletResponse response =      mvc.perform(get("http://localhost:" + port + "/actuator/health")).andReturn().getResponse();

    assertEquals(HttpStatus.OK.value(), response.getStatus());

    System.out.println(response.getContentAsString());
    JSONObject jo = new JSONObject(response.getContentAsString());

    JSONObject componentsJo = jo.getJSONObject("components");
    JSONObject mailJo = componentsJo.getJSONObject("mail");
    assertEquals("UP", mailJo.getString("status"));

} }

Note the @AutoConfigureMetrics annotation.
The config application-test.yml looks like:

management:
  endpoint:
    health:
      show-details: always
  health:
    mail:
      enabled: true
  endpoints:
    web:
      exposure:
        include:
        - health

and the mail config part (with your Mail Server or some Mock like GreenMail):

spring:
  mail:
    default-encoding: UTF-8
    host: #{smtp.host}
    username: #{user}
    password: #{pw}
    port: #{port}
    properties:
      mail:
        smtp:
          auth: true
          ssl:
            enable: true
            trust: #{smtp.host}
          starttls:
            enable: true
    protocol: smtp

Have a look at the full project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文