概述
Spring Boot 有一个非常好用的监控和管理的源软件,这个软件就是 Spring Boot Admin。该软件能够将 Actuator 中的信息进行界面化的展示,也可以监控所有 Spring Boot 应用的健康状况,提供实时警报功能。
主要的功能点有:
显示应用程序的监控状态
应用程序上下线监控
查看 JVM,线程信息
可视化的查看日志以及下载日志文件
动态切换日志级别
Http 请求信息跟踪
其他功能点……
可点击 https://github.com/codecentric/spring-boot-admin 更多了解 Spring-boot-admin。
搭建服务流程说明
admin-server admin 监控服务
admin-order amdin 客户端服务
Admin-server
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
<!--监控检测-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--拓展:使用密码-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--拓展:配置邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
创建配置文件
server:
port: 9000
spring:
application:
## 注册服务名
name: springboot-admin
security:
user:
name: admin
password: admin
management:
endpoint:
health:
show-details: always
logging:
file:
path: C:\Users\Administrator\logs\sba
name: springboot-admin.log
配置启动类
/**
* SpringAdmin的服务端
*
* @author Promsing(张有博)
* @version 1.0.0
* @since 2022/8/20 - 19:24
*/
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(AdminApplication.class, args);
ConfigurableEnvironment environment = context.getEnvironment();
String serverPort = environment.getProperty("server.port");
System.out.println("SpringBootAdmin: http://127.0.0.1:" + serverPort);
}
}
开启鉴权
package com.promsing.admin.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
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;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import java.util.UUID;
@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final AdminServerProperties adminServer;
private final SecurityProperties security;
public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {
this.adminServer = adminServer;
this.security = security;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
http.authorizeRequests(
(authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() // <1>
.antMatchers(this.adminServer.path("/actuator/info")).permitAll()
.antMatchers(this.adminServer.path("/actuator/health")).permitAll()
.antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() // <2>
).formLogin(
(formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() // <3>
).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) // <4>
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <5>
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminServer.path("/instances"),
HttpMethod.POST.toString()), // <6>
new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
HttpMethod.DELETE.toString()), // <6>
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // <7>
))
.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
}
}
部署成功
Cleinet
引入依赖
<!--客户端的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
添加配置文件+鉴权
spring:
application:
name: msb-web
messages:
basename: i18n/login
boot:
admin:
client:
url: http://127.0.0.1:9000
instance:
prefer-ip: true # 使用ip注册进来
username: admin
password: admin
server:
port: 9001
management:
endpoint:
health:
show-details: always
endpoints:
enabled-by-default: true
web:
base-path: /actuator
exposure:
include: '*'
客户端开启日志配置
spring:
application:
name: msb-web
messages:
basename: i18n/login
boot:
admin:
client:
url: http://127.0.0.1:9000
instance:
prefer-ip: true # 使用ip注册进来
username: admin
password: admin
server:
port: 9001
management:
endpoint:
health:
show-details: always
endpoints:
enabled-by-default: true
web:
base-path: /actuator
exposure:
include: '*'
## 开启日志配置
logging:
file:
path: C:\Users\Administrator\logs\sba
name: boot-web.log
服务端开启邮件提醒
引入依赖
<!--拓展:配置邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
重新编辑配置文件
spring:
application:
## 注册服务名
name: springboot-admin
security:
user:
name: admin # 登录的账号密码
password: admin
mail:
host: smtp.qq.com # 发件人使用的qq邮箱服务
username: [email protected]
# 授权码,不是密码,在qq邮箱设置‐账号里面有生成授权码
password: xsfmzwhtefbbhi ##授权码
port: 465 ## 注意端口号
boot:
admin:
notify:
mail:
# 发件人
from: [email protected]
# 收件人,多个中间用,分隔
to: [email protected]