Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 362|回复: 0

[默认分类] spring - boot 监控管理模块搭建

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-3-28 10:33:04 | 显示全部楼层 |阅读模式
    Spring-Actuator是Spring-boot对应用监控的集成模块,提供了我们对服务器进行监控的支持,使我们更直观的获取应用程序中加载的应用配置、环境变量、自动化配置报告等。
    使用Spring-Actuator
     1、引入Maven依赖

      

    1. <!-- 监控管理模块 -->
    2. <dependency>
    3.     <groupId>org.springframework.boot</groupId>
    4.     <artifactId>spring-boot-starter-actuator</artifactId>
    5. </dependency>
    复制代码


      引入依赖后启动项目即可。
    项目启动时会打印出我们所需的url

    1. 2018-03-26 23:19:00.169  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
    2. 2018-03-26 23:19:00.169  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    3. 2018-03-26 23:19:00.170  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    4. 2018-03-26 23:19:00.170  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    5. 2018-03-26 23:19:00.171  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
    6. 2018-03-26 23:19:00.171  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    7. 2018-03-26 23:19:00.172  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    8. 2018-03-26 23:19:00.173  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
    9. 2018-03-26 23:19:00.173  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    10. 2018-03-26 23:19:00.173  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/pause || /pause.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
    11. 2018-03-26 23:19:00.174  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/service-registry/instance-status],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint.setStatus(java.lang.String)
    12. 2018-03-26 23:19:00.174  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/service-registry/instance-status],methods=[GET]}" onto public org.springframework.http.ResponseEntity org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint.getStatus()
    13. 2018-03-26 23:19:00.175  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
    14. 2018-03-26 23:19:00.175  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
    15. 2018-03-26 23:19:00.175  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    16. 2018-03-26 23:19:00.176  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
    17. 2018-03-26 23:19:00.176  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/refresh || /refresh.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
    18. 2018-03-26 23:19:00.177  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/resume || /resume.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
    19. 2018-03-26 23:19:00.178  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    20. 2018-03-26 23:19:00.178  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/archaius || /archaius.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    21. 2018-03-26 23:19:00.179  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
    22. 2018-03-26 23:19:00.179  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint.value(java.util.Map<java.lang.String, java.lang.String>)
    23. 2018-03-26 23:19:00.179  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/reset],methods=[POST]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint.reset()
    24. 2018-03-26 23:19:00.179  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/features || /features.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    25. 2018-03-26 23:19:00.180  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/restart || /restart.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.context.restart.RestartMvcEndpoint.invoke()
    26. 2018-03-26 23:19:00.181  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/entity || /entity.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    27. 2018-03-26 23:19:00.181  INFO 6148 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
    28. 2018-03-26 23:19:00.193  INFO 6148 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.actuate.endpoint.mvc.ManagementErrorEndpoint.invoke()
    复制代码


      
      2、yml配置文件的配置
      (1)配置端口号和不启用某功能模块
      

    1. #制定端口号,不制定则和该服务器server.port相同
    2. management:
    3.   port: 4321
    4. #去掉某项不需要查看内容的功能,如不需要health
    5.   health:
    6.     mail:
    7.     #false代表不启用该功能
    8.       enabled: false
    复制代码


    上述配置表示配置的端口为http端口为4321 并且禁用 /health功能。
      (2)修改映射路径

    1. #修改某配置的映射id的路径
    2. endpoints:
    3.   beans:
    4.     id: entity
    复制代码


    上述配置表示将/beans获取应用上下文创建bean的路径改为/entity
      (3)配置Actuator安全配置
      配置安全需要借助Spring-security
      引入依赖
      

    1. <!-- 用于注册中心访问账号认证 -->
    2.         <dependency>
    3.             <groupId>org.springframework.boot</groupId>
    4.             <artifactId>spring-boot-starter-security</artifactId>
    5.         </dependency>
    复制代码


      配置账号密码:

    1. security:
    2.   basic:
    3.     #代表开启账号密码认证
    4.     enabled: true
    5.     #配置账号和密码
    6.   user:
    7.     name: root
    8.     password: 123
    复制代码



     配置完账号密码后即可通过 http://127.0.0.1:4321进行访问,并且输入配置的账号和密码即可,输入一次当浏览器不关闭时都有效。
      (4)将展示报文显示为json格式
        json格式展示拥有更好的可读性和美观性 

    1. #将acyuator显示的报文打印为json格式
    2. spring:
    3.   jackson:
    4.     serialization:
    5.       indent-output: true
    复制代码


      将上述配置加至application.yml文件中即可
    3、启动类配置
      

    1. @SpringBootApplication
    2. @EnableEurekaServer//此行注解代表为一个服务注册组件,此注解仅适用于eureka
    3. public class EurekaServerApplication {
    4.     public static void main(String[] args) {
    5.         SpringApplication.run(EurekaServerApplication.class, args);
    6.     }
    7. }
    复制代码


    无须做配置即可运行
      4、一些主要的配置模块

      
       
       HTTP请求方式
       路径
       描述
       
       
       GET
       /autoconfig
       用于获取应用的自动化配置信息
       
       
       GET
       /beans
       用于获取应用上下文创建的所有bean
       
       
       GET
       /configprops    
       获取应用中配置的属性信息报告
       
       
       GET
       /env  
       用于获取应用所有可用的环境变量属性报告
       
       
       GET
       /mappings
       用于获取所有SpringMVC的控制器映射关系报告
       
       
       GET
       /info  
       用于获取应用自定义的信息
       
      

      
      
      
      
      
      
      
      
      
      
      
      (1) /autoconfig
        该配置下有两个主节点。
          positiveMatches :返回的是条件匹配成功的自动化配置信息
          negativeMatches :返回的是条件匹配不成功的配置信息
      (2) /beans
        该配置文件用于展示由ApplicationContext应用上下文加载的bean对象,包含以下几个节点:
        

    1. "context" : "bootstrap",
    2.   "parent" : null,
    3.   "beans" : [ {
    4.     "bean" : "propertySourceBootstrapConfiguration",
    5.     "aliases" : [ ],
    6.     "scope" : "singleton",
    7.     "type" : "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$$EnhancerBySpringCGLIB$$82fdf928",
    8.     "resource" : "null",
    9.     "dependencies" : [ ]
    10.   }]
    复制代码


      beans为应用上下文加载的类的集合,其包括以下几个标签:
        1)、bean : 应用上下文创建的bean的名称
        2)、scope:创建的bean的作用域  
          singleton  : 单例 , 是spring默认的作用域
          prototype : 每一次请求都会产生一个新的实例 ,相当于new的操作
          request  : 每次http请求都会产生一个新的实例,当前bean只在同一个 HttpRequest中有效
          session : 同一个HttpSession中有效
        3)、type : 应用上下文管理bean的类型
           为类的全限定类名
        4)、resource : class文件的具体路径
        5)、dependencies : 依赖bean的名称 ,即bean中有关联关系bean的名称
      (3) /configprops : 获取应用中配置的属性信息报告
        其下有两个子节点
        prefix : 代表属性的配置前缀
        properties : 代表各个属性的名称和值    
      (4) /env 用于获取应用上下文的环境变量属性报告,如环境变量、JVM属性、应用的配置信息、命令行中的参数等。
        1)、server.ports 代表该应用配置的端口号
        2) 、systemProperties获取的为应用的jvm等信息
      (5) /mappings SpringMvc控制器映射的报告,

    1. {
    2.   "{[/error]}" : {
    3.     "bean" : "requestMappingHandlerMapping",
    4.     "method" : "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
    5.   }}
    复制代码


         1)  /error代表springMvc的映射路径
           2)  /bean 代表用于处理器映射器映射的类
           3)  /method 代表映射类中的方法
      (6)、info 用于获取application.yml中配置的自定义配置信息
      
    5、度量指标类的配置
      度量指标提供的报告内容是动态变化的,提供了一些快照信息;如内存使用情况、Http请求次数统计、外部资源指标统计等。

      (1)、/metrics 返回当前应用的各类重要度量指标。有以下内容

        1)、系统信息: processors  ----  处理器数量
                 instance.uptime  ------  运行时间   
               systemload.average  --系统平均负载 等。
        2) 、 men.* : 系统内存概要信息
        3)  、 heap.*  :堆内存使用情况
        4) 、 nonheap.* : 非堆内存使用情况
        5) 、 threads.*  : 线程使用情况
        6) 、 classess.*  : 类加载和卸载情况
        7) 、  gc.*  : 垃圾收集器的详细信息
        8) 、 httpsession.*  : tomcat等容器的绘画情况
        9) 、 guage.*  : 返回一个映射数值,如延迟时间等
        10) 、 conter.*  : 作为计数器使用,记录了增加量与减少量
      (2) /health :各类健康指标

        如下述显示了内存的一些信息.


    1. "diskSpace" : {
    2.     "status" : "UP",
    3.     "total" : 21475880960,
    4.     "free" : 8916258816,
    5.     "threshold" : 10485760
    6.   }
    复制代码


      (3) /dump :用于暴露程序中的线程信息
        threadName -- 暴露的线程名称
        threadId  --暴露的线程id 等等
      (4) /trace :用于跟踪基本的http信息
    6、操作控制类配置
       (1) /shutdowm 用于关闭该应用的远程操作  --为post请求

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-21 15:08 , Processed in 0.396757 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表