> 文章列表 > 【SpringBoot 学习】53、Spring Boot 集成 Spring Boot Admin

【SpringBoot 学习】53、Spring Boot 集成 Spring Boot Admin

【SpringBoot 学习】53、Spring Boot 集成 Spring Boot Admin

文章目录

  • Spring Boot 集成 Spring Boot Admin

Spring Boot 集成 Spring Boot Admin

Spring Boot Admin 是服务端、客户端模式。如果把两个端搭建在同一个项目中也可以,但是客户端要是挂了,服务端也挂了,所以可以但没必要

搭建独立的 Spring Boot Admin 监控模块,目前已实现功能:

  • 实现 Spring Boot 应用监控
  • 实现 Spring Boot Admin 登录认证
  • 实现 Spring Boot Admin 监听到日志异常发送消息到邮箱
  • 实现 Spring Boot Admin 时时查看应用日志

Spring Boot Admin 服务端

        <!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot 端点监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Spring Boot Admin 服务端 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId></dependency><!-- Spring Boot Security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- Spring Boot Email --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><!-- Spring Boot Thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

启动类增加注解服务端注解 @EnableAdminServer

package com.qboot.bootadmin;import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBootAdmin 监控服务端** @author Tellsea* @date 2023/2/4*/
@SpringBootApplication
@EnableAdminServer
public class QbootBootAdminApplication {public static void main(String[] args) {SpringApplication.run(QbootBootAdminApplication.class, args);}
}

由于监控信息输入敏感信息,所以需要增加一个登录拦截器,只有登录成功才能访问

package com.qboot.bootadmin.config;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;/*** 权限验证** @author Tellsea* @date 2023/2/6*/
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {private final String adminContextPath;public SecuritySecureConfig(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");http.headers().frameOptions().disable();http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll().antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and().formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout().logoutUrl(adminContextPath + "/logout").and().httpBasic().and().csrf().disable();}
}

为了实时监控项目问题,Spring Boot Admin 还配备了监听服务状态等信息发送邮件给指定邮箱

application.yml 配置如下

server:port: 9090servlet:context-path: /qboot-extend-boot-admin# Spring 配置
spring:profiles:active: @profileActive@application:name: qboot-extend-boot-admin

application-dev.yml 配置如下

# Spring 配置
spring:# Boot Admin 配置boot:admin:ui:public-url: http://localhost:${server.port}notify:mail:# 开启邮箱通知enabled: true# 不需要发送通知的状态:从状态A:到状态Bignore-changes: { "UNKNOWN:UP" }# 发件人from: qboot-extend-boot-admin<3210054449@qq.com># 接受人,多个逗号隔开to: 3210054449@qq.com# 抄送人,多个逗号隔开cc: 3210054449@qq.com# 登录配置security:user:name: "admin"password: "123456"# 邮件发送者信息mail:host: smtp.qq.comusername: 3210054449@qq.compassword: lppmkekscejodefi

Spring Boot Admin 客户端

客户端的主配置文件中需要增加如下配置

# 端点监控配置
management:endpoints:web:exposure:include: "*"endpoint:health:show-details: always# Logback 日志配置
logging:file:name: ./logs/sys-info.logconfig: classpath:logback.xml

客户端直接注册到服务端即可,修改 application-dev.yml 配置文件

# Spring配置
spring:# Boot Admin 监控boot:admin:client:enabled: trueurl: http://localhost:9090/qboot-extend-boot-adminusername: "admin"password: "123456"