搭建Springboot Admin监控服务器

文章介绍了SpringBootActuator的Endpoints概念,包括应用配置、度量指标和操作控制类端点。接着,详细阐述了如何搭建SpringBootAdminServer,包括添加依赖、启用注解和配置端点暴露。文章还提到了应用如何注册到SpringBootAdminServer,以及通过SpringBootSecurity开启服务器认证的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 认识springboot actuator

1.1 Actuator Endpoints (端点)

♦ Endpoints是Actuator的核心部分,它用来监视应用程序及交互; SpringBoot Actuator 内置了很多 Endpoints ,并支持扩展

♦ SpringBoot Actuator提供的原生端点有三类:
应用配置类:自动配置信息、Spring Bean信息、yml文件信息、环境信息等等
度量指标类:主要是运行期间的动态信息,例如堆栈、健康指标、metrics信息等等
操性制类:主要是指shutdown ,用户可以发送一个请求将应用的监控功能关闭

2 搭建服务器步骤:

2.1 添加SpringBoot Admin Start依赖
<!--实现对 Spring Boot Admin Server 自动化配置 -->
<!--包含
1. spring-boot-admin-server : Server端
2. spring-boot-admin-server-ui: UI
3. spring-boot-admin-server-cloud :对 Spring Cloud 的接入
-->
<dependency>
  <groupId>de. codecentric</groupId>
  <artifactId>spring-boot-admin-starter-server</artifactId>
  <version>2.2.0</version>
</dependency>
2.2 添加@EnableAdminServer注解
2.3 在bootstrap.yml添加如下配置:
server:
  port: 7001
  servlet:
    context-path: /e-commerce-admin
spring:
  application:
    name: e-commerce-admin
  cloud:
    nacos:
      discovery:
        enabled: true
        server-addr: 127.0.0.1:8848
        namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator
     thymeleaf:
    check-template: false
    check-template-location: false
# 暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'  # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 *, 可以开放所有端点
  endpoint:
    health:
      show-details: always

3 应用注册到 SpringBoot Admin Server

3.1 被监控和管理的应用(微服务),注册到 Admin Server的两种方式
◆ 方式一:被监控和管理的应用程序,使用SpringBoot Admin Client库,通过 HTTP
调用注册到SpringBoot Admin Server上
◆ 方式二:首先,被监控和管理的应用程序,注册到SpringCloud集成的注册中心;
然后SpringBoot Admin Server通过注册中心获取到被监控和管理的应用程序
3.2 应用注册到AdminServer步骤:
3.2.1 在模块的pom文件中添加SpringBoot Actuotor配置:
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
3.2.2 在模块的bootstrap.yml添加以下配置:
spring:
  application:
    name: e-commerce-nacos-client # 应用名称也是构成 Nacos 配置管理 dataId 字段的一部分 (当 config.prefix 为空时)
  cloud:
    nacos:
      namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
      discovery:
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator
# 暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'  # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 *, 可以开放所有端点
  endpoint:
    health:
      show-details: always

3 SpringBoot Admin Server开启认证

3.1 在SpringBoot Admin Server模块的pom文件中添加SpringBoot Security依赖:

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

3.2 在bootstrap.yml文件中添加配置:

spring:
  security: #
    user: root #
    password: root #
spring:
  cloud:
    nacos:
      namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
      discovery:
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator
          user.name: root 
          user.password: root   

3.3 创建Spring Security的配置类:


import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * <h1>配置安全认证, 以便其他的微服务可以注册</h1>
 * 参考 Spring Security 官方
 * */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    /** 应用上下文路径 */
    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {

        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        SavedRequestAwareAuthenticationSuccessHandler successHandler =
                new SavedRequestAwareAuthenticationSuccessHandler();

        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                // 1. 配置所有的静态资源和登录页可以公开访问
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                // 2. 其他请求, 必须要经过认证
                .anyRequest().authenticated()
                .and()
                // 3. 配置登录和登出路径
                .formLogin().loginPage(adminContextPath + "/login")
                .successHandler(successHandler)
                .and()
                .logout().logoutUrl(adminContextPath + "/logout")
                .and()
                // 4. 开启 http basic 支持, 其他的服务模块注册时需要使用
                .httpBasic()
                .and()
                // 5. 开启基于 cookie 的 csrf 保护
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                // 6. 忽略这些路径的 csrf 保护以便其他的模块可以实现注册
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}
### 如何部署 Spring Boot Admin 进行应用监控 #### 启用 Spring Boot Admin Server 为了实现应用程序的集中化管理和监控,可以通过创建一个新的 Spring Boot 应用来充当 `Spring Boot Admin` 服务器。此服务器负责收集并展示所有已注册客户端的状态信息。 在项目中定义一个类用于启动该服务端实例: ```java @Configuration @EnableAdminServer @SpringBootApplication public class SpringBootAdminServerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminServerApplication.class, args); } } ``` 上述代码片段展示了如何通过添加 `@EnableAdminServer` 注解来开启 `SBA Server` 功能[^1]。 #### 客户端集成 为了让被监控的应用能够向 `Spring Boot Admin` 报告其健康状况和其他元数据,在这些应用里需引入依赖项并将自己注册成为 `Spring Boot Admin` 的客户之一。这通常涉及到修改项目的构建文件(如 Maven 的 pom.xml 或 Gradle 的 build.gradle),增加必要的库支持,并设置一些基本属性以便于连接到指定的服务端地址。 对于每一个希望纳入监管范围内的 Spring Boot 应用而言,应该在其配置文件 application.properties 中指明如下参数: ```properties spring.boot.admin.client.url=http://<admin-server-host>:<port> management.endpoints.web.exposure.include=* management.endpoint.health.show-details=ALWAYS ``` 这里 `<admin-server-host>` 和 `<port>` 分别代表实际运行着 `Spring Boot Admin` 服务的那一台机器的名字以及监听端口号;而后面两条则是确保所有的管理端点都可通过 Web 接口访问并且当查询 `/actuator/health` 路径时返回详细的诊断报告[^2]。 #### 版本兼容性注意事项 值得注意的是,在搭建这套体系结构的过程中要注意不同组件之间的版本匹配情况。特别是 `Spring Boot Admin`、`Spring Boot` 自身及其所处环境下的其他框架或工具链之间可能存在特定的要求。如果选用不恰当的话可能会遇到诸如无法正常初始化等问题。因此建议开发者们仔细查阅官方文档确认最佳实践指南[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值