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請求

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM