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搭建, 具体过程如下:
0. 添加Maven依赖
创建普通SpringBoot项目(此处选择版本为2.1.0.RELEASE),pom.xml中添加依赖
<dependencies>
<!-- Web 基础-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 邮件通知,如不需要可删除-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 监控后台登录验证 可删除-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 监控必须-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 监控必须-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
1. 开启监控
在启动类上加@EnableAdminServer
注解
@EnableAdminServer
@SpringBootApplication
public class XxzxApplication {
public static void main(String[] args) {
SpringApplication.run(XxzxApplication.class, args);
}
}
2. 编辑application.yml
spring:
mail:
host: smtp.exmail.qq.com
port: 465
username: spring_boot_admin@xxx.edu.cn
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: xxx@xxx.edu.cn
from: spring_boot_admin@xxx.edu.cn
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
}
}
3. 客户端配置
客户端Spring Boot 版本为2.1.0
增加Maven依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
增加配置
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配置为重点
5. 启动两个项目
邮件提醒都删了,未截图。。。
目前尚未解决的问题:
- 若服务端部署后域名为https,后台无法正常显示,引用资源均为http。
- Spring Boot 1.x版本无法监控,虽然官方有说明如何配置,但未成功,还需使用旧的Spring Boot Admin版本。