spring boot 添加admin监控

一、Spring Boot  Admin简介

spring boot admin github开源地址:https://github.com/codecentric/spring-boot-admin

它主要的作用是在Spring Boot Actuator的基础上提供简洁的WEB UI展示。

二、项目使用:

1、搭建一个maven web项目

2、pom依赖配置

[plain]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-web</artifactId>  
  4. </dependency>  
  5. <dependency>  
  6.     <groupId>org.springframework.boot</groupId>  
  7.     <artifactId>spring-boot-starter-security</artifactId>  
  8. </dependency>  
  9. <dependency>  
  10.     <groupId>de.codecentric</groupId>  
  11.     <artifactId>spring-boot-admin-starter-client</artifactId>  
  12. </dependency>  
  13. <dependency>  
  14.     <groupId>de.codecentric</groupId>  
  15.     <artifactId>spring-boot-admin-server</artifactId>  
  16. </dependency>  
  17. <dependency>  
  18.     <groupId>de.codecentric</groupId>  
  19.     <artifactId>spring-boot-admin-server-ui</artifactId>  
  20. </dependency>  
  21. <dependency>  
  22.     <groupId>de.codecentric</groupId>  
  23.     <artifactId>spring-boot-admin-server-ui-login</artifactId>  
  24. </dependency>  

在pom.xml中添加上以上配置

admin服务端:spring-boot-admin-server、spring-boot-admin-server-ui

admin客户端:spring-boot-admin-starter-client  (加上该项能监控服务端自身的运行状态,其他项目只需要引入client就可以引入监控)

安全:spring-boot-starter-security

登录验证:spring-boot-admin-server-ui-login (也可以自行添加简单的登录界面)

3、application.yml

[html]  view plain  copy
  1. info:  
  2.   app:  
  3.     name: imard  
  4.     version: v1.0.0  
[html]  view plain  copy
  1. logging:  
  2.   file: "d:/logs/imard/boot.log"  
  3.   
  4. management:  
  5.   context-path: "/actuator"  
  6.   
  7. spring:  
  8.   application:  
  9.     name: "@pom.artifactId@"  
  10.   boot:  
  11.     admin:  
  12.       url: http://www.test.com:8080  
  13.   profiles:  
  14.     active:  
  15.       - secure  
  16.   
  17. ---  
  18. spring:  
  19.   profiles: insecure  
  20.   
  21. management:  
  22.   security:  
  23.     enabled: false  
  24.   
  25. security:  
  26.   basic:  
  27.     enabled: false  
  28.   
  29. ---  
  30. spring:  
  31.   profiles: secure  
  32.   boot:  
  33.     admin:  
  34.       username: "${security.user.name}"  
  35.       password: "${security.user.password}"  
  36.       client:  
  37.         metadata:  
  38.           user.name: "${security.user.name}"  
  39.           user.password:  "${security.user.password}"  
  40.   
  41. security:  
  42.   user:  
  43.     name: user  
  44.     password: pass  
其中:spring.boot.admin.url声明admin服务端地址(其他项目会通过这个url主动的注册到admin监控中)

            info配置app的基本信息

            www.test.com  在本机hosts中做了映射

4、Application.java

[java]  view plain  copy
  1. @Configuration  
  2. @EnableAutoConfiguration  
  3. @EnableAdminServer  
  4. public class Application extends SpringBootServletInitializer {  
  5.     @Override  
  6.     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
  7.         return application.sources(Application.class);  
  8.     }  
  9.   
  10.     public static void main(String[] args) {  
  11.         SpringApplication.run(Application.class, args);  
  12.     }  
  13. }  

@EnableAdminServer 添加上该注解启动监控

5、SecurityConfig

[java]  view plain  copy
  1. @Profile("secure")  
  2. @Configuration  
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {  
  4.     @Override  
  5.     protected void configure(HttpSecurity http) throws Exception {  
  6.         http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();  
  7.         http.logout().logoutUrl("/logout");  
  8.         http.csrf().disable();  
  9.   
  10.         http.authorizeRequests()  
  11.             .antMatchers("/login.html""/**/*.css""/img/**""/third-party/**").permitAll();  
  12.   
  13.         http.authorizeRequests().antMatchers("/api/**").permitAll().antMatchers("/**")  
  14.             .authenticated();  
  15.   
  16.         // Enable so that the clients can authenticate via HTTP basic for registering  
  17.         http.httpBasic();  
  18.     }  
  19. }  
使用Spring Security配置一个基本的安全策略

6、监管管理

配置完1~5个步骤以后,使用application启动监控程序。

通过http://www.test.com:8080/login.html监控登录界面进行安全验证后,如下图:


进入details就可以看到具体的项目监控信息(Details、Log、Metrics、Environment、Logging、JMX、Threads、Audit、Trace、Heapdump)

猜你喜欢

转载自blog.csdn.net/clypm/article/details/79907537