• Spring Boot Admin 介绍及使用


    概述

    Spring Boot 有一个非常好用的监控和管理的源软件,这个软件就是 Spring Boot Admin。该软件能够将 Actuator 中的信息进行界面化的展示,也可以监控所有 Spring Boot 应用的健康状况,提供实时警报功能。

    主要的功能点有:

    • 显示应用程序的监控状态
    • 应用程序上下线监控
    • 查看 JVM,线程信息
    • 可视化的查看日志以及下载日志文件
    • 动态切换日志级别
    • Http 请求信息跟踪
    • 其他功能点……

    可点击https://github.com/codecentric/spring-boot-admin更多了解 Spring-boot-admin。

    搭建服务流程说明

    • admin-server admin 监控服务
    • admin-order amdin 客户端服务

    创建Spring Boot Admin项目

    **版本说明:**版本建议:SpringBoot2.x=SpringBootAdmin2.x(比如SpringBoot2.3.x可以用SpringBootAdmin2.3.x)

    创建一个 Spring Boot 项目,用于展示各个服务中的监控信息,加上 Spring Boot Admin 的依赖,具体代码如下所示

    pom 依赖

    
            org.springframework.boot
            spring-boot-starter-parent
            2.3.6.RELEASE
     
    
    
            1.8
            2.2.5.RELEASE
            2.3.6.RELEASE
            Hoxton.SR8
            2.3.1
        
    
    
         org.springframework.boot
         spring-boot-starter-web
    
    
         de.codecentric
         spring-boot-admin-starter-server
         ${admin.starter.server.version}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    启动类

    添加 @EnableAdminServer

    @EnableAdminServer
    @SpringBootApplication
    public class AdminServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AdminServerApplication.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    yml 配置

    在属性文件中增加端口配置信息:

    server:
      port: 8000
    
    spring:
      application:
        ## 注册服务名
        name: admin-order
    
    management:
      endpoint:
        health:
          show-details: always
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    启动程序,访问 Web 地址 http://127.0.0.1:8000/ 就可以看到主页面了,这个时候是没有数据的,如图 1 所示


    客户端服务

    流程

    创建 amdin-order 服务,将服务注册到 admin-server 中

    pom 依赖

    
            org.springframework.boot
            spring-boot-starter-parent
            2.3.6.RELEASE
        
    
        
            1.8
            2.2.5.RELEASE
            2.3.6.RELEASE
            Hoxton.SR8
            2.3.1
        
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
            
                org.projectlombok
                lombok
                1.16.14
                provided
            
            
                de.codecentric
                spring-boot-admin-starter-client
                ${spring.boot.admin.client.version}
            
          
        
    
    • 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

    yml 配置

    spring:
      application:
        ## 注册服务名
        name: admin-order
      ## spring boot admin
      boot:
        admin:
          client:
            api-path:
            url: http://127.0.0.1:8000
            instance:
              prefer-ip: true #?使用ip注册进来
    
    
    server:
      port: 8100
    
    # ?endpoints config
    management:
      endpoint:
        health:
          show-details: always
      endpoints:
        enabled-by-default: true
        web:
          base-path: /actuator
          exposure:
            include: '*'
    
    • 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

    启动 admin-order 服务

    @SpringBootApplication
    public class AdminOrderApp {
    
        public static void main(String[] args) {
            SpringApplication.run(AdminOrderApp.class,args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    从新刷新 admin 平台,admin-order 服务就可以监控

    • 绿色:健康
    • 灰色:连接客户端健康信息超时(超过10s)
    • 红色:就能看到具体异常信息

    Spring Boot Admin 监控平台

    Insighs 信息

    自定义的 Info 信息、健康状态、元数据,如图

    Endpoint 端点接口信息

    JVM 信息

    Admin 中查看各个服务的日志

    客户端需要把日志同步ADMI服务中,通过JMX,客户端配置如下

    添加 logback-spring.xml 配置

    
    
    
    	
    	
    	
    	
    	
    	
    	
    	
    	
    		
    			${CONSOLE_LOG_PATTERN}
    		
    	
    
    	
    	
    		${log.path}/debug.log
    		
    			${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz
    			50MB
    			30
    		
    		
    			%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n
    		
    	
    
    	
    	
    		${log.path}/error.log
    		
    			${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz
    			50MB
    			30
    		
    		
    			%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n
    		
    		
    			ERROR
    		
    	
    
    	
    		
    	
    
    
    	
    	
    		
    		
    	
    
    
    • 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
    • 67

    yml 配置

    management:
      endpoints:
        web:
          exposure:
            include: '*'
        enabled-by-default: true
      endpoint:
        health:
          show-details: ALWAYS
        # 日志记录
        logfile:
          external-file: D:/project/springcould-alibaba-example/logs/admin-order/debug.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Spring Boot Admin 查询日志

    从新打 Admin 监控平台,点击 admin-order 服务查看日志,如下

    访问 admin-order 接口

    @Slf4j
    @RestController
    public class OrderController {
    
        @GetMapping("getOrder")
        public String getOrder() {
            log.info("getOrder.execute");
            Date date = new Date();
            // SimpleDateFormat
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(date);
            log.info("getOrder.resp 记录当前时间:普通时间: {}", time);
            return "admin order cliect hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    访问 http://127.0.0.1:8100/getOrder 查看日志文件

    日志文件等级配置

    Admin 中 SpringSecurity

    pom 依赖

     
            
                org.springframework.boot
                spring-boot-starter-security
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    SecuritySecureConfig

    @Configuration(proxyBeanMethods = false)
    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 {
            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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    yml 配置

    spring:
      security:
        user:
          name: "admin"
          password: "admin"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    从新启动 admin-server 服务

    客户端注册 admin 服务需要配置 username 和 password

    spring:
      application:
        ## 注册服务名
        name: admin-order
      ## spring boot admin
      boot:
        admin:
          client:
            api-path:
            url: http://127.0.0.1:8000
            instance:
              prefer-ip: true #?使用ip注册进来
            username: admin
            password: admin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Nacos 拉取客户端

    不懂 Nacos 怎样作为注册中心,可以查看 Spring Cloud Alibaba 微服务组件 Nacos 注册中心(三)

    pom 依赖

    
            org.springframework.boot
            spring-boot-starter-parent
            2.3.6.RELEASE
        
    
        
            1.8
            2.2.5.RELEASE
            2.3.6.RELEASE
            Hoxton.SR8
            2.3.1
        
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                de.codecentric
                spring-boot-admin-starter-server
                ${admin.starter.server.version}
            
           
            
                org.springframework.boot
                spring-boot-starter-security
            
           
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-discovery
            
        
    
        
            
                
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring.cloud.version}
                    pom
                    import
                
                
                
                    com.alibaba.cloud
                    spring-cloud-alibaba-dependencies
                    ${spring.cloud.alibaba.version}
                    pom
                    import
                
            
        
    
    • 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

    yml 配置

    group: ADMIN 作为一组监控服务,服务端可以拉取客户端配置为 group:ADMIN

    spring:
      application:
        ## 注册服务名
        name: admin-server
    
      security:
        user:
          name: "admin"
          password: "admin"
    
      cloud:
        nacos:
          ## 注册中心地址
          discovery:
            server-addr: 127.0.0.1:8848
            group: ADMIN
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    客户端同服务端配置 (也要从新注册到 Nacos 服务)

    邮件配置

     
            
                org.springframework.boot
                spring-boot-starter-mail
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    yml 配置

    spring:
      mail:
        host: smtp.qq.com #?发件人使用的qq邮箱服务
        username: 865391093@qq.com
        #?授权码,不是密码,在qq邮箱设置‐账号里面有生成授权码
        password: zaydkxfjbjwbbgbc
      boot:
        admin:
          notify:
            mail:
              #?发件人
              from: 865391093@qq.com
              #?收件人,多个中间用,分隔
              to: 865391093@qq.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    qq 邮箱设置

    测试

    自定义端点配置

    Indicator

    /**
     *  自定义 dicator
     * @date: 2021/12/15 17:31
     */
    @Component
    public class MyHealthIndicator implements HealthIndicator {
    
    
        @Override
        public Health health() {
    
            int errorCode = check(); //?perform?some?specific?health?check
            if (errorCode != 0) {
                return Health.down().
                        withDetail("msg","error service").
                        withDetail("code",500).
                        build();
            }
            return Health.up().build();
        }
    
        private int check() {
            return 0;
        }
    }
    
    • 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

    info

    /**
     * 自定义 info
     * @date: 2021/12/16 18:53
     */
    @Component
    public class AppInfo implements InfoContributor {
    
    
        @Override
        public void contribute(Info.Builder builder) {
    
            builder.withDetail("version","1.0.RELEASE");
            builder.withDetail("project","admin-order");
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    yml 配置

    info:
      appName: admin-order
      mavenAppName: '@project.artifactId@'
      mavenVersion: '@project.version@'
    
    • 1
    • 2
    • 3
    • 4

    ADMIN 控制台查看

    Metrics

    SpringBootActuator为Micrometer提供了依赖管理和自动配置功能,Micrometer是一个应用指标facade(面门),支持多种监控系统,包括:

    AppOptics,AzureMonitor,NetflixAtlas,CloudWatch,Datadog,Dynatrace,Elastic,Ganglia,Graphite,Humio,Influx/Telegraf,JMX,KairosDB,NewRelic,Prometheus,SignalFx,GoogleStackdriver,StatsD,和Wavefront。

    • 提供一些列api供我们操作指标
    • 提供了缓存、类加载器、GC、jvm内存、cpu利用率、线程…指标能够开箱即用
    • 已经融入springboot Actuator

    JVM度量,报告利用率(JVMmetrics,reportutilizationof):

    • 各种内存和缓冲池(Variousmemoryandbufferpools)

    • 与垃圾收集有关的统计数据(Statisticsrelatedtogarbagecollection)

    • 线程利用率(Threadsutilization)

    • 加载/卸载的类数(Numberofclassesloaded/unloaded)

      @RestController
      public class HelloController {

      @GetMapping("/sayHi")
      public String sayHi(String productNo) throws InterruptedException {
          // 线程长请求
          //Thread.sleep(5000);
          // 定制基于Metrics的计数器
          Metrics.counter("order.counter","productNo",productNo).increment();
      
          // 定制基于Metrics的定时器
          Timer timer = Metrics.timer("order.timer");
          timer.record(()->{
              System.out.println("success");
          });
      
          // 定制基于Metrics的仪表  记录单个值  一般集合数量
          Metrics.gauge("order.gauge",1);
      
          // 定制基于Metrics的摘要 一般用来记录 百分比数值(缓存命中率)
          DistributionSummary summary = Metrics.summary("redis.hitRate");
          summary.record(1.5);
          return "success";
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21

      }

    增加定制 Metrics

    • Counter

    Counter 是一种比较简单的Meter,它是一种单值的度量类型,或者说是一个单值计数器。
    使用场景:
    Counter 的作用是记录XXX的总量或者计数值,适用于一些增长类型的统计,例如下单、支付次数、Http请求总量记录等等

        // 定制基于Metrics的计数器
        Metrics.counter("order.counter","produntNo",produntNo).increment();
    
    • 1
    • 2

    测试

    127.0.0.1:8100/actuator/metrics/order.counter

    {"name":"order.counter","description":null,"baseUnit":null,"measurements":[{"statistic":"COUNT","value":3.0}],"availableTags":[{"tag":"productNo","values":["G122390"]}]}
    
    • 1

    • Timer

    Timer(计时器)适用于记录耗时比较短的事件的执行时间,通过时间分布展示事件的序列和发生频率。
    使用场景:
    根据个人经验和实践,总结如下:

    • 记录指定方法的执行时间用于展示。

    • 记录一些任务的执行时间,从而确定某些数据来源的速率,例如消息队列消息的消费速率等。

      // 定制基于Metrics的定时器
      Timer timer = Metrics.timer(“xushu.timer”);
      timer.record(()->{
      System.out.println(“success”);
      });

    • Summary

    Summary(摘要)主要用于跟踪事件的分布,在Micrometer中,对应的类是DistributionSummary(分发摘要)。它的使用方式和Timer
    十分相似,但是它的记录值并不依赖于时间单位。
    使用场景:
    根据个人经验和实践,总结如下:

    1. 不依赖于时间单位的记录值的测量,例如服务器有效负载值,缓存的命中率等。

      // 定制基于Metrics的摘要 一般用来记录 百分比数值(缓存命中率)
      DistributionSummary summary = Metrics.summary(“redis.hitRate”);
      summary.record(1.5);

  • 相关阅读:
    WebRTC janus安装编译教程
    请描述JDK和JRE的区别 ?
    Spring的开幕式——Spring概述与设计思想
    Git小乌龟不弹add push commit的方法
    彻底搞懂Mybatis
    JavaScript系列之字符串类型
    以“数字化渠道”撬动家用电器消费蓝海,经销商在线系统让企业生意更进一步
    MySQL:12-Java中使用MySQL(JDBC)
    Go中的泛型和反射以及序列化
    安装Karmada
  • 原文地址:https://blog.csdn.net/m0_67391121/article/details/126040667