1SpringBootAdmin · SpringCloud微服务实战 · 看云

导航

本节代码地址


1.1 什么是Spring Boot Admin?

Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。常见的功能或者监控如下:

  • 显示健康状况

  • 显示详细信息,例如

    • JVM和内存指标
    • micrometer.io指标
    • 数据源指标
    • 缓存指标
  • 显示构建信息编号

  • 关注并下载日志文件

  • 查看jvm系统和环境属性

  • 查看Spring Boot配置属性

  • 支持Spring Cloud的postable / env-和/ refresh-endpoint

  • 轻松的日志级管理

  • 与JMX-beans交互

  • 查看线程转储

  • 查看http跟踪

  • 查看auditevents

  • 查看http-endpoints

  • 查看计划任务

  • 查看和删除活动会话(使用spring-session)

  • 查看Flyway / Liquibase数据库迁移

  • 下载heapdump

  • 状态变更通知(通过电子邮件,Slack,Hipchat,......)

  • 状态更改的事件日志(非持久性)

1.2 快速开始

本文演示非基于Eureka 服务发现的方式监控应用。如果想看基于Eureka 服务发现的可以看下一节SpringBootAdmin-Eureka

1.2.1 创建Spring Boot Admin Server

下面我们创建Server 端,既然有服务端就会有客户端,客户端的配置我们后面再看。
6e4f8865d85dff730b25bb8f2c2e2548_MD5.png

1.2.1 引入maven 配置

可以看到我们引入的admin 版本是2.2.1 的,我们使用的Spring Boot 也是
2.2.1.RELEASE,注入如果引入的版本不对应,启动也会不成功。当前版本的admin 已经支持中文的描述,是一种支持国际化的显示。

我们引入spring-boot-starter-security为了限制监控页面的访问,可以在application.yml中设置账号密码,如果不设置,每次启动会生成一个随机的密码。
引入spring-boot-starter-mail是为了及时通知状态变更给对应的用户
引入jolokia-core是用于 JMX-bean 管理的

<dependencies>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
    </dependency>
</dependencies>

1.2.3 应用配置

我们在应用配置中设置admin监控登录所需要的用户名密码均为admin,可以根据需要设置是否需要邮件通知,如果需要配置邮箱信息,注意不要忘记配置spring.boot.notify.fromspring.boot.notify.to,一个是发件人,一个是接收人。笔者这里用的是163的邮箱。

spring:
  application:
    name: admin-server
  security:
    user:
      name: "admin"
      password: "admin"

  boot:
    admin:
      ui:
        title: ${spring.application.name}
#邮件配置开始
      notify:
        mail:
          from: ***@163.com
          to: ***@qq.com
  mail:
    host: smtp.163.com
    username: ***@163.com
    password: ***
#邮件配置结束
server:
  port: 8762


1.2.4 配置Security

我们需要对一些url放行,否则登录页面登录页面无法正常跳转,我们将页面资源和登录退出接口放行。配置好之后,其他url在没有登录的时候会跳转到登录页。

@Configuration
public class FwSecurityConfigure extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

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

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

        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().disable();
    }
}

1.2.5 新建服务端启动类

注意加上@EnableAdminServer,开启admin server

@SpringBootApplication
@EnableAdminServer
public class BootAdminServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootAdminServerApplication.class, args);
    }
}

1.3 客户端

刚才是服务端的配置,下面将客户端配置完后一起演示效果。

1.3.1 新建客户端

这里我们新建客户端,并取名客户端的应用名称为admin-client,这样客户端和服务端一起可以明显看到效果。
a51a50ee8f07ec2de66b5aa2b0fb8c79_MD5.png

1.3.2 应用配置

应用配置中需要指明服务端的注册地址,以及前面服务端设置的用户名密码,这里为了演示方便,我们将客户端应用的所有信息暴露出去(也可以指定暴露的endpoint),如果不暴露,将获取不到应用的监控信息。

spring:
  boot:
    admin:
      client:
        url: http://localhost:8762 #server端地址
        username: admin    #对应server端的账号密码,不配置就监控不到这个client。
        password: admin
        instance:
          service-base-url: http://localhost:8766  #client地址,
          #不配置的情况下,在打包的时候会有提示。不影响运行。
  application:
    name: admin-client
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
info:
  version: @project.version@
  groupId: @project.groupId@
  artifactId: @project.artifactId@
server:
  port: 8766

1.3.3 新建客户端启动类

这里只需要配置应用启动的注解即可

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


}

1.4 服务端和客户端启动

浏览器数据http://localhost:8762/login可以看到跳转到登录页了,账号密码就是配置中的admin、admin
78afb629282f8810bedfe84b062b954f_MD5.png

登录进来之后,发现客户端也注册上来了,并且可以看到当前是中文的内容显示,可以通过右上角选择语言。
3dfbdf4c67a4939595ecc01b031d803f_MD5.png

1.4.1 下面来看应用的监控信息

首先是应用墙,展示所有注册上来的应用及数量以及健康信息
8e6b7770952eb13b2fb7e9956fd2741f_MD5.png

日志报表,显示应用注册上来的时间信息
0e24bc341551198ae7c17837a1870635_MD5.png

通过应用墙或应用点击具体的应用,可以总览当前应用的基本信息
23b9e0a35290428821a4c53f0b022af8_MD5.png

fa343086c819fe6512fcbbcacdb4ef18_MD5.png

其它的菜单读者可以自行点击下看看
bed57b28809b80559c27c7b593703613_MD5.png

如果配置了右键提醒,当应用线下的时候,会发邮件通知
820345696597bef6dc01b449ca8fde75_MD5.png