Spring Boot Admin 搭建监控系统

Spring Boot Admin是Spring Boot项目的一个社区项目,主要用于管理和监控Spring Boot应用程序。Spring Boot Admin应用分为Server应用和Client应用,其中Server应用用于收集Cient应用的信息并对其进行监控。背后的数据采集是由Actuator端点提供的。数据的采集方式主要有两种:应用程序通过HTTP协议向Spring Boot Admin Server注册信息;或者Sring Boot Admin Server通过使用服务发现(Eureka、Consul)收集Client信息。

Spring Boot Admin Server

创建Spring Boot Admin Server应用

pom.xml

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        

        
            org.springframework.boot
            spring-boot-starter-security
        

        
            de.codecentric
            spring-boot-admin-starter-server
        

        
            org.springframework.boot
            spring-boot-starter-jetty
        
    

Tomcat替换为Jetty的原因
启动后有报错

2020-03-09 14:06:57.337 ERROR 6316 --- [nio-8072-exec-1] o.a.catalina.connector.CoyoteAdapter     : Exception while processing an asynchronous request

java.lang.IllegalStateException: Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]
    at org.apache.coyote.AsyncStateMachine.asyncError(AsyncStateMachine.java:440) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:512) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.Request.action(Request.java:430) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.AsyncContextImpl.setErrorState(AsyncContextImpl.java:401) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:239) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProcessor.dispatch(AbstractProcessor.java:241) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:53) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_211]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_211]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.19.jar:9.0.19]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_211]

引入spring-boot-starter-security原因
Spring Boot Admin服务器可以访问应用程序的敏感端点,因此建议为管理员和客户端应用程序添加安全配置,并添加安全配置类

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        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()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
        // @formatter:on
    }
}

这是一个简单的安全配置,但添加后,客户端也需要进行安全设置,否则客户端信息无法注册到服务器,客户端需要增加一下配置,下文client配置将会继续提到

spring.boot.admin.username=admin
spring.boot.admin.password=admin

启动类

启动类增加@EnableAdminServer注解,用于引入Spring Boot Admin Server配置

@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

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

配置文件

server:
  port: 8072
spring:
  security:
    user:
      name: admin
      password: 123456

测试

到这里Spring Boot Admin Server项目就配置完成了,启动项目可以看到如下页面


admin-server

Spring Boot Admin Client

pom.xml

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-security
        

        
            de.codecentric
            spring-boot-admin-starter-client
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

启动类

无需任何另外的设置

@SpringBootApplication
public class AdminClientApplication {

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

配置文件

server:
  port: 8073

spring:
  application:
    name: spring-boot-admin-client
  boot:
    admin:
      client:
        # Spring Boot Admin Server 地址
        url: http://localhost:8072
        # server 设置的用户名
        username: admin
        # server 设置的密码
        password: 123456
        instance:
          prefer-ip: true
          service-url: http://localhost:8073

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

安全配置类

@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
    }
}

测试

启动client应用后,可以查看到如下图所示内容


admin-client-application

admin-client-detail

示例代码

spring-boot-admin

相关推荐

基于Spring Boot 使用Mybatis + Druid访问MySQL

你可能感兴趣的:(Spring Boot Admin 搭建监控系统)