Spring Boot-Admin使用

Spring Boot Actuator

Actuator是Spring Boot的模块,它在应用中添加了REST/JMS端点,方便监控和管理应用。端点提供了健康检查、指标监控、访问日志、线程转储、堆转储和环境信息等等。

Spring Boot Admin

Actuator功能强大,便于其他应用使用端点(只需要简单的REST调用)。但是开发人员使用时就没那么方便了。对于开发人员,有良好的交互界面会更方便浏览监控数据和管理应用。这正是Spring Boot Admin做的工作。它为actuator端点提供了良好的交互界面,并提供了额外的特性。

Spring Boot Admin不是Spring团队提供的模块,它是由Codecentric公司创建的,代码在Github上公开。

Client And Server

不像Actuator,Spring Boot Admin由两部分组成:Client和Server。

Server部分包括Admin用户界面并独立运行于被监控应用。Client部分是包含在被监控应用中,并注册到Admin Server。

这样,即使应用挂掉了或者不能正常运行,监控的Server依然正常运行。假如你有多个应用(比如Spring Boot微服务应用),每个应用运行多个实例。对于传统的Actuator监控,很难单独访问每个应用,因为你要跟踪有多少实例及它们在哪运行。

对于Spring Boot Admin,被监控应用的每个实例(Client)在启动时注册到Server,每个实例在Admin Server就有一个单点,就可以检查它们的状态了。

Server配置

首先对Spring Boot Admin Server进行配置。

创建一个Spring Boot工程,可以使用Spring Initializr创建。保证包含web模块。
创建工程后,第一件事就是添加Spring Boot Admin Server依赖:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>

接着需要在启动类中加入注解@EnableAdminServer来开启Admin Server。

@SpringBootApplication
@EnableAdminServerpublic class SpringBootAdminServerApplication {

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

现在运行程序并在浏览器打开http://localhost:8080/,可以看到如下界面:

Spring Boot-Admin使用

Server运行正常,但是没有Client注册。

 

Client配置

和Server一样,第一步是向新建Client工程添加依赖:

<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-actuator</artifactId>
</dependency>

然后指定运行的Admin Server的url,即在application.properties中添加:

spring.boot.admin.client.url=http://localhost:8080

现在可以同时运行Client和Server应用。只要保证没有端口冲突,因为两个应用默认端口都是8080。测试情况下,可以在application.properties中设置server.port=0,这样Client会使用一个随机端口启动。这样就可以测试使用不同的端口启动多个实例。

打开Admin Server界面就可以看到Client应用了。点击应用名称显示应用详情页。

Spring Boot-Admin使用

如果你看到上面最少信息的界面,意味着你的项目中没有添加Actuator。记住,Spring Boot Admin使用Actuator端点监控应用运行状况。幸运的是,只需要在项目中添加依赖,自动配置会解决其他问题。

但是,大部分的端点默认是不对外暴露的,所以需要在application.properties添加配置使它们暴露:

management.endpoints.web.exposure.include=*

暴露Actuator端点后就可以在Admin Server上看到更多的信息了。

 

Server安全

在Admin Server添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.properties配置登录Admin Server的用户名和密码:

spring.security.user.name=server
spring.security.user.password=server

然后在Client端也要添加这些凭证,否则不能注册到server。

spring.boot.admin.client.username=server
spring.boot.admin.client.password=server

回到Server模块,最后一件事是添加Spring Security配置来保证Admin管理界面的安全性。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

        http.authorizeRequests()
            .antMatchers("/assets/**").permitAll()
            .antMatchers("/login").permitAll()
            .anyRequest().authenticated().and()
            .formLogin().loginPage("/login")
            .successHandler(successHandler).and()
            .logout().logoutUrl("/logout").and()
            .httpBasic().and()
            .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .ignoringAntMatchers(
                "/instances",
                "/actuator/**"
             );
    }
}

这段代码的作用是:限制只有通过HTTP基本认证和登录用户可以使用Admin管理界面。登录界面和静态资源(javascript, HTML, CSS)是公开的,否则无法登录。它是基于cookie的CSRF保护。你可以看到在CSRF保护中有些路径被忽略了,因为Admin Server缺少适当的支持。