springBootAdmin
是面向springBoot的一款监控组件,很好集成,配置一下参数加个依赖就能用
admin工程
<!-- springBoot admin 监控 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 引入Web场景 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
引入web依赖和admin依赖,spring security依赖是为了做安全验证,不需要则不加入也可以
application.properties配置
spring.application.name=iot-admin
server.port=9000
#配置admin工程登录的账号密码
spring.security.user.name=eetal
spring.security.user.password=123456
新增一个application主类,打上EnableAdminServer注解即代表开启adminServer服务,如果需要安全验证,就加入SecuritySecureConfig 的配置对接口拦截,对应的客户端工程需要配置登录的账号和密码(客户端不需要引入spring security依赖)
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
//配置安全校验
@Configuration
@Order(Ordered.LOWEST_PRECEDENCE)
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();
}
}
}
客户端
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
客户端在web的基础上,加入spring-boot-admin-starter-client的依赖,在application.properties配置文件里加上如下参数
#要展示的应用名称
spring.application.name=iot-web-80
#admin工程的url
spring.boot.admin.client.url=http://localhost:9000
#展示全部细节信息
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
#允许admin工程远程停止本应用
management.endpoint.shutdown.enabled=true
#admin工程的账号密码
spring.boot.admin.client.username=eetal
spring.boot.admin.client.password=123456
效果
先开启admin工程,再运行客户端工程。访问admin工程的首页,登录以后就可以看到监控页面了
还可以看到springBoot工程的日志,太舒服了
还可以通过jmx远程修改一些配置参数,比如数据库连接池
线程运行状况
dump堆内存
非常实用!