赞
踩
SpringBootAdmin可以监控SpringBoot单击或集群项目,提供详细的健康信息、内存信息、JVM系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、SpringBoot缓存查看和管理功能。
第一步:监控服务端搭建
创建一个SpringBoot项目,添加下面依赖
- <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>
- </dependency>
在启动类上开启监控服务
- import de.codecentric.boot.admin.server.config.EnableAdminServer;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @EnableAdminServer
- @SpringBootApplication
- public class SysadminApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SysadminApplication.class, args);
- System.out.println("项目启动成功!");
- }
-
- }
在配置文件中只需要配置一个服务端口即可。配置端口的目的是为了不与其它服务冲突。
server.port=8081
启动服务端,此时访问地址:http://localhost:8081/applications 界面如下
添加security支持,防止任意用户可查看监控。首先添加依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
在配置文件中添加以下配置
- # 设置登录用户名、密码和角色
- spring.security.user.name=java666
- spring.security.user.password=java666
- spring.security.user.roles=SBA_ADMIN
最后,进行权限资源设置。在服务端创建类,加入下面代码即可
- 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.builders.WebSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
- import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
-
- @EnableWebSecurity
- @Configuration(proxyBeanMethods = false)
- public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
-
- private final String adminContextPath;
-
- public AdminSecurityConfig(AdminServerProperties adminServerProperties) {
- this.adminContextPath = adminServerProperties.getContextPath();
- }
-
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
- successHandler.setTargetUrlParameter("redirectTo");
- successHandler.setDefaultTargetUrl(adminContextPath + "/");
- http.authorizeRequests()
- .antMatchers(adminContextPath + "/assets/**").permitAll()
- .antMatchers(adminContextPath + "/login").permitAll()
- .antMatchers(adminContextPath + "/instances/**").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/**"
- );
- }
-
- @Override
- public void configure(WebSecurity web) {
- web.ignoring().antMatchers("/actuator/**");
- }
-
- }
此时访问监控系统就需要输入用户名和密码才能正常使用。如下所示
第二步:客户端搭建配置
1、所需依赖
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
2、在客户端配置文件中添加如下配置
注意:如果是yml配置文件,在配置开启监控所有项的时候,*需要使用单引号包裹
- # 当前项目端口号
- server.port=8080
- # Spring Boot Admin 监控服务器端地址
- spring.boot.admin.client.url=http://localhost:8081
- # 开启监控所有项,Actuator功能
- management.endpoints.web.exposure.include=*
3、启动项目,利用服务端主页地址查看即可http://localhost:8081/applications
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。