You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中Spring Boot Admin无法发送告警邮件问题排查

问题描述

我们正在搭建基于微服务的监控环境,使用Spring Boot Admin配合Actuator监控微服务。本地运行状态一切正常,但将两个应用分别Docker容器化部署后,客户端应用宕机时Spring Boot Admin无法像本地那样发送告警邮件。两个应用在容器内均使用8080端口,映射了不同的外部端口,相关配置及错误日志如下:

客户端配置

application.properties

server.port=8080
spring.boot.admin.client.instance.name=Monitoramento Catracas
spring.boot.admin.client.url=http://my-ip:6060
spring.boot.admin.client.instance.service-base-url=http://my-ip:7070

docker-compose.yml

services:
  my-service:
    build:
      context: ./
      dockerfile: my-file.dockerfile
    image: test/my-image
    container_name: my-container
    ports:
       - 7070:8080

Spring Boot Admin配置

application.properties

spring.mail.host=smtp.office365.com
spring.mail.port=587
spring.mail.username=email@email
spring.mail.password=12345
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true    

server.port=6060
spring.boot.admin.notify.mail.enabled=true
spring.boot.admin.notify.mail.from=email@email
spring.boot.admin.notify.mail.to=email_2@email
spring.boot.admin.notify.mail.template=classpath:templates/email-template.html

docker-compose.yml

services:
  ms-admin:
    build:
      context: ./ms-admin
      dockerfile: ms-admin.dockerfile
    image: cap/ms-admin
    container_name: ms-admin
    ports:
       - 6060:8080

错误日志

2022-12-09 17:06:10.534  WARN 1 --- [or-http-epoll-3] d.c.b.a.s.notify.NotificationTrigger     : Couldn't notify for event InstanceStatusChangedEvent(super=InstanceEvent(instance=2cc8546ac366, version=4, timestamp=2022-12-09T17:06:10.275Z, type=STATUS_CHANGED), statusInfo=StatusInfo(status=OFFLINE, details={exception=org.springframework.web.reactive.function.client.WebClientRequestException, message=finishConnect(..) failed: Connection refused: /my-ip:7070; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: /my-ip:7070}))
ms-monitoramento-hml |
ms-monitoramento-hml | org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1;
ms-monitoramento-hml |   nested exception is:
ms-monitoramento-hml |  java.net.UnknownHostException: smtp.office365.com. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1;
ms-monitoramento-hml |   nested exception is:
ms-monitoramento-hml |  java.net.UnknownHostException: smtp.office365.com


ms-monitoramento-hml | Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 587; timeout -1
ms-monitoramento-hml |  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210) ~[jakarta.mail-1.6.7.jar!/:1.6.7]
ms-monitoramento-hml |  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722) ~[jakarta.mail-1.6.7.jar!/:1.6.7]
ms-monitoramento-hml |  at javax.mail.Service.connect(Service.java:342) ~[jakarta.mail-1.6.7.jar!/:1.6.7]
ms-monitoramento-hml |  at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518) ~[spring-context-support-5.3.23.jar!/:5.3.23]
ms-monitoramento-hml |  at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437) ~[spring-context-support-5.3.23.jar!/:5.3.23]
ms-monitoramento-hml |  ... 124 common frames omitted
解决方案

1. 修复SMTP服务器域名解析问题(邮件发送失败核心原因)

日志中的java.net.UnknownHostException: smtp.office365.com表明Spring Boot Admin容器无法解析SMTP服务器的域名,导致邮件发送失败,可按以下步骤处理:

  • 检查容器网络权限:确保Admin容器所在的Docker网络允许出站访问互联网,若使用自定义网络,确认网络驱动为默认的bridge,且无出站流量限制。
  • 指定DNS服务器:在Admin的docker-compose.yml中添加DNS配置,强制使用公共DNS服务器解决解析问题:
    services:
      ms-admin:
        # 保留原有配置
        dns:
          - 8.8.8.8
          - 1.1.1.1
    
  • 测试容器内连通性:进入Admin容器,执行ping smtp.office365.com和telnet smtp.office365.com 587,确认能正常连通SMTP服务器。

2. 处理客户端端口连接拒绝问题(状态变更事件附加错误)

日志中的Connection refused: /my-ip:7070是客户端宕机后的正常反馈,但需确保客户端运行时Admin能正常访问:

  • 验证外部端口可用性:客户端运行时,在外部机器访问http://my-ip:7070/actuator/health,确认Actuator端点可正常访问。
  • 优先使用Docker内部网络通信:若客户端和Admin部署在同一台机器的Docker环境中,建议修改客户端的spring.boot.admin.client.instance.service-base-url为容器内部地址或服务名(如两者在同一docker-compose网络,可设为http://my-service:8080),避免依赖外部端口映射,提升通信稳定性。

3. 额外配置检查

  • 验证Office365 SMTP权限:确认邮箱账号的SMTP功能已开启,若为新版Office365,需使用应用密码替代普通密码,或开启“低安全性应用访问”(视账号权限而定)。
  • 检查防火墙规则:确保宿主机或云服务商的防火墙允许Admin容器出站访问587端口(SMTP TLS端口)。

内容的提问来源于stack exchange,提问作者user3628479

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:15:28