spring - boot 监控管理模块搭建

Spring-Actuator是Spring-boot对应用监控的集成模块,提供了我们对服务器进行监控的支持,使我们更直观的获取应用程序中加载的应用配置、环境变量、自动化配置报告等。

使用Spring-Actuator

 1、引入Maven依赖

  

<!-- 监控管理模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

  引入依赖后启动项目即可。

项目启动时会打印出我们所需的url

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)
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()
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()
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()
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)
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()
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()
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)
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()
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()
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)
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()
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)
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>)
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()
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
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()
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()
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()
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()
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)
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>)
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()
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()
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()
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()
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()
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)配置端口号和不启用某功能模块

  

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

上述配置表示配置的端口为http端口为4321 并且禁用 /health功能。

  (2)修改映射路径

#修改某配置的映射id的路径
endpoints:
  beans:
    id: entity

上述配置表示将/beans获取应用上下文创建bean的路径改为/entity

  (3)配置Actuator安全配置

  配置安全需要借助Spring-security

  引入依赖

  

<!-- 用于注册中心访问账号认证 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

  配置账号密码:

security:
  basic:
    #代表开启账号密码认证
    enabled: true 
    #配置账号和密码
  user:
    name: root
    password: 123

 配置完账号密码后即可通过 http://127.0.0.1:4321进行访问,并且输入配置的账号和密码即可,输入一次当浏览器不关闭时都有效。

  (4)将展示报文显示为json格式

    json格式展示拥有更好的可读性和美观性 

#将acyuator显示的报文打印为json格式
spring:
  jackson:
    serialization:
      indent-output: true

  将上述配置加至application.yml文件中即可

 3、启动类配置

  

@SpringBootApplication
@EnableEurekaServer//此行注解代表为一个服务注册组件,此注解仅适用于eureka
public class EurekaServerApplication {

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

无须做配置即可运行

  4、一些主要的配置模块

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

 

 

 

 

 

 

 

 

 

 

 

   (1) /autoconfig

    该配置下有两个主节点。

      positiveMatches :返回的是条件匹配成功的自动化配置信息

      negativeMatches :返回的是条件匹配不成功的配置信息

  (2) /beans

    该配置文件用于展示由ApplicationContext应用上下文加载的bean对象,包含以下几个节点:

    

 "context" : "bootstrap",
  "parent" : null,
  "beans" : [ {
    "bean" : "propertySourceBootstrapConfiguration",
    "aliases" : [ ],
    "scope" : "singleton",
    "type" : "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$$EnhancerBySpringCGLIB$$82fdf928",
    "resource" : "null",
    "dependencies" : [ ]
  }]

  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控制器映射的报告,

{
  "{[/error]}" : {
    "bean" : "requestMappingHandlerMapping",
    "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)"
  }}

     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 :各类健康指标

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

 "diskSpace" : {
    "status" : "UP",
    "total" : 21475880960,
    "free" : 8916258816,
    "threshold" : 10485760
  }

  (3) /dump :用于暴露程序中的线程信息

    threadName -- 暴露的线程名称

    threadId  --暴露的线程id 等等

  (4) /trace :用于跟踪基本的http信息

6、操作控制类配置

   (1) /shutdowm 用于关闭该应用的远程操作  --为post请求

 

posted @ 2018-03-27 00:03  浅浅_浅默  阅读(5565)  评论(0编辑  收藏  举报