使用 Spring Boot Admin 监控应用状态

导读:本篇文章讲解 使用 Spring Boot Admin 监控应用状态,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1 Spring Boot Actuator

Spring Boot Actuator 是 Spring Boot 提供的对应用的自省和监控功能,如健康检查,审计,指标收集,HTTP 跟踪等,可以帮助开发和运维人员监控和管理 Spring Boot 应用。该模块采集应用的内部信息,并暴露给外部的模块,支持 HTTP 和 JMX,并可以与一些第三方监控系统(如 Prometheus)整合。

1.1 Actuator endpoint

端点 Endpoint 是 Actuator 的核心组成部分,用来监视应用程序的各种状态。 Spring Boot Actuator 内置很多 Endpoint,总体上看分成三类:

  1. 应用配置类:主要包括配置信息、Spring Bean 的信息、配置文件信息、环境信息等;
  2. 度量指标类:应用在运行期间的信息,包括堆栈、健康状态、线程池信息、HTTP请求统计等;
  3. 操作控制类:如 shutdown,提供了对应用的关闭等操作类功能。

1.2 添加依赖

在 pom.xml 中添加 Actuator 的 starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码

1.3 访问端点

添加依赖后,启动服务,可通过如下请求查看暴露的端点:

http://localhost:9099/actuator

该请求返回:

{
	"_links": {
		"self": {
			"href": "http://localhost:9099/actuator",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:9099/actuator/health/{*path}",
			"templated": true
		},
		"health": {
			"href": "http://localhost:9099/actuator/health",
			"templated": false
		}
	}
}
复制代码

从返回的结果可以看出默认只开放了 /actuator/health 端点。访问该端点 http://localhost:9099/actuator/health

{"status":"UP"}
复制代码

其他未开放的端点可以独立配置开启或禁用。

在 application.yml 中添加如下配置,开放所有的端点,并显示详细的 health:

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
复制代码

重启服务,再次查看暴露的端点,可以看出有如下端点:

使用 Spring Boot Admin 监控应用状态

2 Spring Boot Admin

Spring Boot Actuator 提供了各种端点,Spring Boot Admin 能够将 Actuator 中的信息进行界面化的展示,并提供实时报警功能。

在微服务环境中,使用 Spring Boot Admin,通常包括服务端和客户端,服务端只运行 Spring Boot Admin Server,收集各个客户端的数据,并以可视化界面显示出来。客户端运行 Spring Boot Admin Client,或者通过服务发现与注册获取应用的信息。

这里的 demo 我就不在 Spring Boot Admin Server了,将当前 hero-springboot-demo 既作为 server、也作为 client 使用。在后面的实战篇章中会独立 Admin Server,同时客户端也不使用 client,而是通过服务注册与发现。

2.1 添加依赖

在 pom.xml 中添加 Spring Boot Admin Server 的依赖:

<!-- 实战中该依赖只在独立的 Admin Server 中使用,此处仅为测试 -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.7.4</version>
</dependency>
<!-- 实战中客户端也不需要添加该依赖,而是通过服务发现与注册,此处仅为测试 -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.7.4</version>
</dependency>
复制代码

需要注意版本号,由于 Spring Boot 版本使用的是 2.7.x,Spring Boot Admin Server 的版本也要用 2.7.x,千万别乱搞!

2.2 开启 Admin Server

在启动类 DemoApplication 上添加注解 @EnableAdminServer 开启 Spring Boot Admin Server。

@EnableAdminServer
@EnableAsync
@MapperScan("com.yygnb.demo.mapper")
@SpringBootApplication
public class DemoApplication {
  ...
}
复制代码

2.3 配置客户端

在 application.yml 中添加如下配置:

  1. 配置 Admin Server 的 context-path;
  2. 为客户端配置 Admin Server 的地址。
spring:
  application:
    name: hero-springboot-demo
  boot:
    admin:
      client:
        url: 'http://localhost:9099/monitor'
      context-path: '/monitor'
复制代码

2.4 访问 Admin Server

重启服务,在浏览器中访问 Spring Boot Admin Server:

http://localhost:9099/monitor
复制代码

可以看到当前应用的作为客户端注册到 Admin Server 上:

使用 Spring Boot Admin 监控应用状态

再次强调,上述操作仅仅针对demo学习,非真实的企业级开发!

3 自定义告警

当应用状态异常时,Spring Boot Admin 会自动实时告警,而告警的方式可以由我们自定义。这里模拟日志的方式。

在 config 包下创建类 DemoNotifier,该类继承自 AbstractEventNotifier

@Slf4j
@Component
public class DemoNotifier extends AbstractEventNotifier {

    protected DemoNotifier(InstanceRepository repository) {
        super(repository);
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> log.error("Instance info: {}, {}, {}",
                instance.getRegistration().getName(), event.getInstance(),
                event.getType()));
    }
}
复制代码

此时,注册到这个Admin Server的其他客户端启动、停止等,当前应用都会监听到事件,输出日志。实战中可以在这里面发送邮件、消息等。

4 登录访问

上面配置的 Admin Server 无需登录就可以访问,在真实开发中需要登录后才能访问。admin server 也提供了登录页面。

4.1 添加依赖

在 pom.xml 添加 Spring Security 的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码

4.2 配置用户名密码

在 application.yml 中配置登录的用户名和密码:

spring:
  application:
    name: hero-springboot-demo
  boot:
    admin:
      client:
        url: 'http://localhost:9099/monitor'
      context-path: '/monitor'
  security:
    user:
      name: admin
      password: 111111
复制代码

上面的配置在之前的基础上增加了:spring.security.user 的配置。

4.3 添加配置类

在 config 包下添加 Spring Security 的配置类 SecurityConfig

@Configuration
public class SecurityConfig {

    private final String adminContextPath;

    public SecurityConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler =
                new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");
        return http.authorizeHttpRequests(auth -> auth.antMatchers(
                                        adminContextPath + "/assets/**",
                                        adminContextPath + "/login",
                                        adminContextPath + "/instances",
                                        adminContextPath + "/actuator/**"
                                ).permitAll()
                                .antMatchers(adminContextPath + "/**").authenticated()
                                .anyRequest().permitAll()
                ).formLogin(form -> form.loginPage(adminContextPath + "/login")
                        .successHandler(successHandler)
                ).logout(logout -> logout.logoutUrl(adminContextPath + "/logout"))
                .csrf(AbstractHttpConfigurer::disable)
                .build();
    }
}
复制代码

上面配置文件中的 adminContextPath 就是前面配置的 spring.boot.admin.context-path,即 /monitor

上面配置包括几个部分:

  1. 仅对路径 /monitor/** 请求权限控制;
  2. 登录页面和登录成功后的默认地址;
  3. 表单登录配置;
  4. 禁用 CSRF。

4.4 测试运行

重启服务,访问之前开发的 computer 等接口,可以正常访问;如果访问 /monitor 等路径,就会跳转 Spring Boot Admin 提供的登录页:

使用 Spring Boot Admin 监控应用状态

使用配置的用户名密码(admin/111111)登录,登录成功后进入 Admin Server 页面。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/94750.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!