Spring Boot Admin-应用健康监控后台管理

Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。

1. 什么是Spring Boot Admin?


Spring Boot Admin(下文简称SBA)是一个社区开源项目,用于管理和监控你的Spring Boot应用。应用通过SBA Client注册到SBA Server中,可通过HTTP请求或者Spring Cloud发现(例如Eureka、Consul),UI展示通过Vue在Spring Boot Actuator端点上获取应用监控数据进行管理。

2. 开始使用


2,1 配置好你的SBA Server服务端程序

2.1.1 添加spring-boot-admin-starter-server的Maven依赖到你的pom.xml中:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.1.2 打开你的Spring Boot启动文件中添加@EnableAdminServer 注解用于激活SBA Server配置:

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer
public class Application {

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

注意如果你需要将应用以war形式部署,请将启动文件做如下改变:

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@EnableAdminServer
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

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

2.1.3 在你的SBA Server中的 application.yml 中进行相关配置:

spring:
  # 配置SBA Client连接的安全账号密码
  security:
    user:
      name: admin
      password: admin
  boot:
    admin:
      ui:
        # 修改网页显示的tab标题
        title: "应用监控管理"
        # 修改网页的brand的图标和标题
        brand: "<img src='assets/img/icon-spring-boot-admin.svg'><span>应用监控管理</span>"
server:
  port: 7070

2.1.4 进行Spring Security相关配置:

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

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

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

        http.authorizeRequests()
                .antMatchers(
                        adminContextPath + "/assets/**",
                        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(
                        "/instances",
                        "/actuator/**",
                        adminContextPath + "/logout"
                );
    }
}

2.2 注册你的客户端应用

为了注册你的应用在SBA Server中,你可以使用SBA Client 或者使用Spring Cloud Discovery(例如:Eureka, Consul, ...)。

2.2.1 配置你的Spring Boot Admin客户端

每一个想注册的应用都必须包含SBA Client,同时为了确保Actuator端点的安全,建议添加 spring-boot-starter-security 依赖用于保护端点安全访问。
添加 spring-boot-admin-starter-client 的Maven依赖到你的pom.xml中:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.0.2</version>
</dependency>
<!-- 可选择添加下面依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.2.2. 在你的SBA Client中的 application.yml 中配置你的SBA Server的服务地址:

spring:
  boot:
    admin:
      client:
        # 这个URL地址是SBA Server的服务地址,你需要将你的应用注册到该地址上
        url: http://localhost:7070
        # 配置连接到监测管理平台的Security安全密码
        username: admin
        password: admin
        instance:
          metadata:
            # 配置发送到SBA Server的SBA Client的端点安全密码
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
  security:
    user:
      name: root
      password: root
# 官方文档中有提到, SpringBoot 的 Logging 配置的级别有7个:TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF
logging:
  # 设置日志保存的路径,path和file只需要设置一个,指定path则日志名称固定为spring.log
  path: /java-log
  # 打印日志的级别
  level:
    root: info
# 在默认情况下大多数Actuator的端点并没有完全公开,这里我将所有端点都进行公开进行管理
management.endpoints.web.exposure.include: "*" 

2.2.3. 如果你添加了 spring-boot-starter-security 依赖,你需要进行如下配置使SBA Server服务端能够访问SBA Client客户端的Actuator端点:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and()
                .authorizeRequests().antMatchers("/actuator/**").authenticated()
                .anyRequest().permitAll();
    }
}

3.至此相关配置已经完成,现在让我们启动SBA Server服务端程序,打开浏览器输入http://localhost:7070 访问它。

login.png

我们可以看到SBA Server跳转到了登录页面提示我们登录,这时候我们输入我们在application.yml中配置的账号密码进行登录:


form.png

输入用户名和密码后点击Login后跳转到我们的应用监控管理首页:


monitor.png

这时我们看到是没有应用注册进来的,页面是数据是空白的,整个页面简洁干净,接下去让我们启动一个SBA Client客户端程序注册进来
applications.png

启动一个SBA Client后,可以看到页面上出现了一个客户端程序注册进来了
wallboard.png

下图是你的应用的基本信息查看,包括线程使用图表,堆内存使用图表,非堆内存使用图表等等


detail.png

下图是你的应用的日志打印


logfile.png

下图是你的应用的网络URL请求情况监控


http traces.png

这里就不过多演示了,可以看出这个通过这个监控平台我们可以方便快捷的管理监控我们的应用,再也不用打开Linux命令行查看我们项目运行的情况,怎么样功能是不是很强大。

最后贴上以下官方文档地址
spring-boot-admin 官方文档地址
spring-boot-admin GitHub地址

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容