Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator的基础上提供简洁的可视化 WEB UI。
官网地址:https://github.com/codecentric/spring-boot-admin
帮助文档:http://codecentric.github.io/spring-boot-admin/2.1.3/#_what_is_spring_boot_admin
在实际开发中,需要监控项目的异常情况,如掉线,内存开销等信息。Spring Boot Admin通过简单的配置即可实现。
Spring Boot Admin 可分为服务端和客户端,如果只需监控一个项目,服务端和客户端使用同一个项目即可。
因我这需监控多个项目,Spring Boot 1.5.X和2.1.X的项目都有,故此处服务端为单独的项目。
下文基于Spring Boot 2.1.X搭建, 具体过程如下:
创建普通SpringBoot项目(此处选择版本为2.1.0.RELEASE),pom.xml中添加依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-security
de.codecentric
spring-boot-admin-starter-server
2.1.3
de.codecentric
spring-boot-admin-server-ui
2.1.3
在启动类上加@EnableAdminServer
注解
@EnableAdminServer
@SpringBootApplication
public class XxzxApplication {
public static void main(String[] args) {
SpringApplication.run(XxzxApplication.class, args);
}
}
spring:
mail:
host: smtp.exmail.qq.com
port: 465
username: [email protected]
password: password
properties:
mail:
smtp:
auth: true
ssl:
enable: true
socketFactory:
class: com.sun.mail.util.MailSSLSocketFactory
fallback: false
application:
name: Spring Boot Admin V2 Web
boot:
admin:
url: http://localhost:8080
notify:
mail:
to: [email protected]
from: [email protected]
security:
user:
name: admin
password: password
freemarker:
check-template-location: false
其中:mail配置为邮箱通知上线下线,具体配置可搜索 Spring Boot 邮件相关。
security配置为设置后台登录密码
如果不需要以上信息,只是看看效果,以上配置都不需要。
如果设置权限,还需增加SecuritySecureConfig.java
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;
/**
* @Author geekfly
* @Date 2019-03-21 10:55
* @Desc
*/
@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 {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
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()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
// @formatter:on
}
}
客户端Spring Boot 版本为2.1.0
增加Maven依赖
de.codecentric
spring-boot-admin-starter-client
2.1.1
org.springframework.boot
spring-boot-starter-actuator
2.1.0.RELEASE
org.jolokia
jolokia-core
增加配置
sprinng:
boot:
admin:
client:
url: https://localhost:8080 # 服务端地址
username: admin # 参照服务端配置,或不需要
password: q1w2e3
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: ["health", "info", "metrics"]
management配置为重点
目前尚未解决的问题: