actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理。
Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。
一个能够为基于Spring的企业应用系统提供声明式的安全訪问控制解决方式的安全框架
使用步骤
1引入POM依赖
<!-- 服务端:带UI界面 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 客户端包 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 安全认证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 端点 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 在管理界面中与 JMX-beans 进行交互所需要被依赖的 JAR -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
2在启动类上添加注解
@EnableAdminServer
@SpringBootApplication
public class SpringBootActuatorAdminApplication {
}
3在启动类中加入security配置
/**
* dev 环境加载
*/
@Profile("dev")
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
/**
* prod 环境加载
*/
@Profile("prod")
@Configuration
public static 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");
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();
}
}
4配置文件
application.properties
# 描述信息
info.version=@project.version@
info.name=@project.artifactId@
# 选择激活对应环境的配置,如果是dev则代表不用认证就能访问监控页,prod代表需要认证
spring.profiles.active=prod
# 加载所有的端点/默认只加载了 info / health
management.endpoints.web.exposure.include=*
# 比较重要,默认 /actuator spring-boot-admin 扫描不到
management.endpoints.web.base-path=/
management.endpoint.health.show-details=always
# 可以关闭制定的端点
management.endpoint.shutdown.enabled=false
# 日志文件
logging.file=./target/admin-server.log
#将服务端自己注册进去
spring.boot.admin.client.url=http://localhost:8080
application-prod.properties
#spring.security.user
#spring.boot.admin.client.username
#spring.boot.admin.client.instance.metadata
#三者用户名密码要一致,否则无法通过安全验证
# 登陆所需的账号密码
spring.security.user.name=admin
spring.security.user.password=admin
# 便于客户端可以在受保护的服务器上注册api
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
# 便服务器可以访问受保护的客户端端点
spring.boot.admin.client.instance.metadata.user.name=admin
spring.boot.admin.client.instance.metadata.user.password=admin
经过以上配置就能实现基于Springboot项目的监控。
访问方式 直接输入IP地址端口号即可 例:http://localhost:8080
注意事项
使用prod环境(spring.profiles.active=prod)需要输入用户名密码才能登陆
用户名密码配置方式,可以自行更改
spring.security.user.name=admin
spring.security.user.password=admin
项目源码: https://github.com/houyongkang/spring-boot-actuator-admin.git
官方admin配置介绍 http://codecentric.github.io/spring-boot-admin/2.0.0/#_ui