Spring Cloud 之 SpringBoot Admin监控搭建(十三)

Spring Boot 有一个非常好用的监控和管理的源软件,这个软件就是 Spring Boot Admin。该软件能够将 Actuator 中的信息进行界面化的展示,也可以监控所有 Spring Boot 应用的健康状况,提供实时警报功能。

主要的功能点有:

  • 显示应用程序的监控状态
  • 应用程序上下线监控
  • 查看 JVM,线程信息
  • 可视化的查看日志以及下载日志文件
  • 动态切换日志级别
  • Http 请求信息跟踪
  • 其他功能点……


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

Spring Boot Admin 是由服务端和客户端组成,在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端。

1、新建一个名称为spring-boot-admin的模块

2、build.gradle依赖

1 dependencies {
2     compile 'de.codecentric:spring-boot-admin-starter-server:2.0.1';
3     compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
4     compile("org.springframework.boot:spring-boot-starter-security")
5     compile("org.jolokia:jolokia-core")
6 }

3、创建启动类

/**
 * @author Leo
 */
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableEurekaClient
public class SpringBootAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }

    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
        }
    }
}

4、bootstrap.yml配置

 1 spring:
 2   application:
 3     name: spring-boot-admin
 4 
 5 server:
 6   port: 9999
 7 
 8 eureka:
 9   client:
10     registryFetchIntervalSeconds: 5
11     serviceUrl:
12       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
13 
14 management:
15   endpoints:
16     web:
17       exposure:
18         include: "*"
19   endpoint:
20     health:
21       show-details: ALWAYS

5、启动

服务启动后访问:http://localhost:9999/,打开Spring Boot Admin监控平台。从下图可以看到现在有6个应用,8个实例,状态为全部在线。

 6、security配置

通常情况下,因为安全问题,我们需要给Spring Boot Admin平台设置一个登录账号和密码,怎么做呢

6.1 修改配置文件

 1 spring:
 2   application:
 3     name: spring-boot-admin
 4   security:
 5     user:
 6       name: "admin"
 7       password: "123456"
 8 server:
 9   port: 9999
10 
11 eureka:
12   client:
13     registryFetchIntervalSeconds: 5
14     serviceUrl:
15       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
16 
17 management:
18   endpoints:
19     web:
20       exposure:
21         include: "*"
22   endpoint:
23     health:
24       show-details: ALWAYS

6.2修改启动类

 1 /**
 2  * @author Leo
 3  */
 4 @Configuration
 5 @EnableAutoConfiguration
 6 @EnableAdminServer
 7 @EnableEurekaClient
 8 public class SpringBootAdminApplication {
 9 
10     public static void main(String[] args) {
11         SpringApplication.run(SpringBootAdminApplication.class, args);
12     }
13 
14     @Configuration
15     public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
16         private final String adminContextPath;
17 
18         public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
19             this.adminContextPath = adminServerProperties.getContextPath();
20         }
21 
22         @Override
23         protected void configure(HttpSecurity http) throws Exception {
24             SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
25             successHandler.setTargetUrlParameter("redirectTo");
26 
27             http.authorizeRequests()
28                     .antMatchers(adminContextPath + "/assets/**").permitAll()
29                     .antMatchers(adminContextPath + "/login").permitAll()
30                     .anyRequest().authenticated()
31                     .and()
32                     .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
33                     .logout().logoutUrl(adminContextPath + "/logout").and()
34                     .httpBasic().and()
35                     .csrf().disable();
36         }
37     }
38 }

6.3重启服务,访问http://localhost:9999/,和之前不同的是多了一个登录页面,输入admin/123456登录即可。

 6.4 登录后我们发现Spring Boot Admin本身处于下线状态,如下图

 6.5 修改配置

 1 spring:
 2   application:
 3     name: spring-boot-admin
 4   security:
 5     user:
 6       name: "admin"
 7       password: "123456"
 8 server:
 9   port: 9999
10 
11 eureka:
12   client:
13     registryFetchIntervalSeconds: 5
14     serviceUrl:
15       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
16   instance:
17     metadata-map:
18       user.name: "admin"
19       user.password: "123456"
20 
21 management:
22   endpoints:
23     web:
24       exposure:
25         include: "*"
26   endpoint:
27     health:
28       show-details: ALWAYS

6.6 重启Admin后再次登录

服务全部正常。

再上几个监控页面

大家可以自己研究一下。

原文地址:https://www.cnblogs.com/shileibrave/p/14452093.html