使用Spring Boot Admin进行集中式监控

在微服务生态系统中,监控单个应用程序变得难以掌控。Spring Boot Admin(SBA)通过提供一个统一的仪表板来管理、监控Spring Boot服务并发出警报,从而解决了这一问题。

让我们构建一个管理服务器,连接客户端应用程序,配置警报,并动态控制日志记录——同时让运维团队轻松应对工作。

步骤1:创建Spring Boot Admin服务器

初始化管理服务器项目

创建一个新的Spring Boot应用程序,并添加SBA依赖项:

Maven

<dependency>  
    <groupId>de.codecentric</groupId>  
    <artifactId>spring-boot-admin-starter-server</artifactId>  
    <version>3.2.0</version>  
</dependency>

Gradle

implementation 'de.codecentric:spring-boot-admin-starter-server:3.2.0'

启用管理服务器

在主类上添加 @EnableAdminServer 注解:

@SpringBootApplication  
@EnableAdminServer  
public class AdminServerApplication {  
    public static void main(String[] args) {  
        SpringApplication.run(AdminServerApplication.class, args);  
    }  
}

配置服务器端口(可选):

# application.properties  
server.port=8080

步骤2:注册客户端应用程序

客户端设置

在你的Spring Boot服务中添加SBA客户端依赖项:

<dependency>  
    <groupId>de.codecentric</groupId>  
    <artifactId>spring-boot-admin-starter-client</artifactId>  
    <version>3.2.0</version>  
</dependency>

将客户端指向管理服务器

# client-service/src/main/resources/application.properties  
spring.boot.admin.client.url=http://localhost:8080  
management.endpoints.web.exposure.include=*  
management.endpoint.health.show-details=always
  • • management.endpoints.web.exposure.include=* 会公开所有的Actuator端点。
  • • 客户端在启动时会自动注册。

步骤3:监控健康状况和指标

仪表板概述

访问管理用户界面,地址为 http://localhost:8080

  • • 应用程序:已注册服务的列表。
  • • 健康状态:运行正常(UP)/运行异常(DOWN)指示器。
  • • 详细信息:CPU使用率、线程数、堆内存。

步骤4:配置警报

电子邮件通知

在管理服务器中添加邮件依赖项:

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-mail</artifactId>  
</dependency>

配置SMTP和通知

# 管理服务器的application.properties  
spring.mail.host=smtp.gmail.com  
spring.mail.port=587  
spring.mail.username=admin@example.com  
spring.mail.password=你的应用密码  
spring.mail.properties.mail.smtp.auth=true  
spring.mail.properties.mail.smtp.starttls.enable=true  
spring.boot.admin.notify.mail.to=alerts@example.com  
spring.boot.admin.notify.mail.from=admin-server@example.com

触发条件

@Configuration  
public class AlertConfig {  
    @Bean  
    public MailNotifier mailNotifier(JavaMailSender mailSender) {  
        MailNotifier notifier = new MailNotifier(mailSender);  
        notifier.setIgnoreChanges(new String[]{"UNKNOWN:UP"}); // 仅在服务状态为DOWN时发出警报  
        return notifier;  
    }  
}

Slack警报(额外内容)

使用 WebhookNotifier 来实现Slack警报:

@Bean  
public WebhookNotifier slackNotifier() {  
    return new WebhookNotifier(  
        List.of("https://hooks.slack.com/services/TXXXXXX/BXXXXXX/XXXXXXXX"),  
        new ObjectMapper()  
    );  
}

步骤5:动态日志级别管理

在客户端启用日志记录器端点

确保客户端公开 /actuator/loggers 端点:

# client-service/application.properties  
management.endpoints.web.exposure.include=health,metrics,loggers

通过管理用户界面调整日志级别

  1. 导航到客户端的详细信息页面。
  2. 从侧边栏中选择 Loggers(日志记录器)。
  3. 搜索一个包(例如,com.example.service)。
  4. 更改日志级别(例如,从DEBUG更改为WARN)。

生产环境中的最佳实践

保护管理服务器

@Configuration  
@EnableWebSecurity  
public class SecurityConfig {  
    @Bean  
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {  
        http  
            .authorizeHttpRequests(auth -> auth  
                .anyRequest().authenticated()  
            )  
            .formLogin(withDefaults());  
        return http.build();  
    }  
}

客户端身份验证

# 客户端application.properties   
spring.boot.admin.client.username=admin   
spring.boot.admin.client.password=secret

保留策略:使用Prometheus或Elasticsearch归档旧指标。

挑战:构建多服务监控仪表板

你的任务

部署2个或更多Spring Boot客户端(例如,orders-service、users-service)。

配置管理服务器 以监控所有客户端。

设置警报

  • 当任何服务状态变为DOWN时发送电子邮件。
  • 当JVM内存使用率高于90%时发送Slack警报。

动态日志记录:通过用户界面更改服务的日志级别。

示例警报配置

@Bean  
public CompositeNotifier compositeNotifier(MailNotifier mail, WebhookNotifier slack) {  
    return new CompositeNotifier(List.of(mail, slack));  
}

分享你的设置

  • 发布你的管理仪表板截图。
  • 分享你的警报 application.properties 文件。
  • 包含客户端/服务器代码的GitHub仓库。

在你的解决方案中添加标签 #bytewise010,就有机会被展示!

结论

Spring Boot Admin将可观测性从一项繁琐的任务转变为一项强大的能力。通过集中进行健康检查、启用动态日志记录以及触发有针对性的警报,你可以在问题升级之前发现并解决它们——所有这些操作都可以在一个统一的界面中完成。

 

请登录后发表评论