springboot 監(jiān)控管理模塊搭建的方法
Spring-Actuator是Spring-boot對應(yīng)用監(jiān)控的集成模塊,提供了我們對服務(wù)器進行監(jiān)控的支持,使我們更直觀的獲取應(yīng)用程序中加載的應(yīng)用配置、環(huán)境變量、自動化配置報告等。
使用Spring-Actuator
1、引入Maven依賴
<!-- 監(jiān)控管理模塊 --> <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)配置端口號和不啟用某功能模塊
#制定端口號,不制定則和該服務(wù)器server.port相同 management: port: 4321 #去掉某項不需要查看內(nèi)容的功能,如不需要health health: mail: #false代表不啟用該功能 enabled: false
上述配置表示配置的端口為http端口為4321 并且禁用 /health功能。
(2)修改映射路徑
#修改某配置的映射id的路徑 endpoints: beans: id: entity
上述配置表示將/beans獲取應(yīng)用上下文創(chuàng)建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進行訪問,并且輸入配置的賬號和密碼即可,輸入一次當(dāng)瀏覽器不關(guān)閉時都有效。
(4)將展示報文顯示為json格式
json格式展示擁有更好的可讀性和美觀性
#將acyuator顯示的報文打印為json格式 spring: jackson: serialization: indent-output: true
將上述配置加至application.yml文件中即可
3、啟動類配置
@SpringBootApplication
@EnableEurekaServer//此行注解代表為一個服務(wù)注冊組件,此注解僅適用于eureka
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
無須做配置即可運行
4、一些主要的配置模塊
| HTTP請求方式 | 路徑 | 描述 |
| GET | /autoconfig | 用于獲取應(yīng)用的自動化配置信息 |
| GET | /beans | 用于獲取應(yīng)用上下文創(chuàng)建的所有bean |
| GET | /configprops | 獲取應(yīng)用中配置的屬性信息報告 |
| GET | /env | 用于獲取應(yīng)用所有可用的環(huán)境變量屬性報告 |
| GET | /mappings | 用于獲取所有SpringMvc的控制器映射關(guān)系報告 |
| GET | /info | 用于獲取應(yīng)用自定義的信息 |
(1) /autoconfig
該配置下有兩個主節(jié)點。
positiveMatches :返回的是條件匹配成功的自動化配置信息
negativeMatches :返回的是條件匹配不成功的配置信息
(2) /beans
該配置文件用于展示由ApplicationContext應(yīng)用上下文加載的bean對象,包含以下幾個節(jié)點:
"context" : "bootstrap",
"parent" : null,
"beans" : [ {
"bean" : "propertySourceBootstrapConfiguration",
"aliases" : [ ],
"scope" : "singleton",
"type" : "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$$EnhancerBySpringCGLIB$$82fdf928",
"resource" : "null",
"dependencies" : [ ]
}]
beans為應(yīng)用上下文加載的類的集合,其包括以下幾個標簽:
1)、bean : 應(yīng)用上下文創(chuàng)建的bean的名稱
2)、scope:創(chuàng)建的bean的作用域
singleton : 單例 , 是spring默認的作用域
prototype : 每一次請求都會產(chǎn)生一個新的實例 ,相當(dāng)于new的操作
request : 每次http請求都會產(chǎn)生一個新的實例,當(dāng)前bean只在同一個 HttpRequest中有效
session : 同一個HttpSession中有效
3)、type : 應(yīng)用上下文管理bean的類型
為類的全限定類名
4)、resource : class文件的具體路徑
5)、dependencies : 依賴bean的名稱 ,即bean中有關(guān)聯(lián)關(guān)系bean的名稱
(3) /configprops : 獲取應(yīng)用中配置的屬性信息報告
其下有兩個子節(jié)點
prefix : 代表屬性的配置前綴
properties : 代表各個屬性的名稱和值
(4) /env 用于獲取應(yīng)用上下文的環(huán)境變量屬性報告,如環(huán)境變量、JVM屬性、應(yīng)用的配置信息、命令行中的參數(shù)等。
1)、server.ports 代表該應(yīng)用配置的端口號
2) 、systemProperties獲取的為應(yīng)用的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、度量指標類的配置
度量指標提供的報告內(nèi)容是動態(tài)變化的,提供了一些快照信息;如內(nèi)存使用情況、Http請求次數(shù)統(tǒng)計、外部資源指標統(tǒng)計等。
(1)、/metrics 返回當(dāng)前應(yīng)用的各類重要度量指標。有以下內(nèi)容
1)、系統(tǒng)信息: processors ---- 處理器數(shù)量
instance.uptime ------ 運行時間
systemload.average --系統(tǒng)平均負載 等。
2) 、 men.* : 系統(tǒng)內(nèi)存概要信息
3) 、 heap.* :堆內(nèi)存使用情況
4) 、 nonheap.* : 非堆內(nèi)存使用情況
5) 、 threads.* : 線程使用情況
6) 、 classess.* : 類加載和卸載情況
7) 、 gc.* : 垃圾收集器的詳細信息
8) 、 httpsession.* : tomcat等容器的繪畫情況
9) 、 guage.* : 返回一個映射數(shù)值,如延遲時間等
10) 、 conter.* : 作為計數(shù)器使用,記錄了增加量與減少量
(2) /health :各類健康指標
如下述顯示了內(nèi)存的一些信息.
"diskSpace" : {
"status" : "UP",
"total" : 21475880960,
"free" : 8916258816,
"threshold" : 10485760
}
(3) /dump :用于暴露程序中的線程信息
threadName -- 暴露的線程名稱
threadId --暴露的線程id 等等
(4) /trace :用于跟蹤基本的http信息
6、操作控制類配置
(1) /shutdowm 用于關(guān)閉該應(yīng)用的遠程操作 --為post請求
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談springboot多模塊(modules)開發(fā)
- 詳解Maven 搭建spring boot多模塊項目(附源碼)
- SpringBoot+Maven 多模塊項目的構(gòu)建、運行、打包實戰(zhàn)
- springboot 多模塊將dao(mybatis)項目拆分出去
- SpringBoot 監(jiān)控管理模塊actuator沒有權(quán)限的問題解決方法
- SpringBoot創(chuàng)建maven多模塊項目實戰(zhàn)代碼
- 深入淺析Spring-boot-starter常用依賴模塊
- springboot+gradle 構(gòu)建多模塊項目的步驟
- Spring Boot實現(xiàn)模塊化的幾種方法
- spring boot添加新模塊的方法教程
相關(guān)文章
java使用bitmap實現(xiàn)可回收自增id的示例
本文主要介紹了java使用bitmap實現(xiàn)可回收自增id的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
Java配置JDK開發(fā)環(huán)境及環(huán)境變量
這篇文章主要為大家詳細介紹了Java配置JDK開發(fā)環(huán)境及環(huán)境變量,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Java利用Map實現(xiàn)計算文本中字符個數(shù)
這篇文章主要為大家詳細介紹了Java如何利用Map集合實現(xiàn)計算文本中字符個數(shù),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08
使用Spring的FactoryBean創(chuàng)建和獲取Bean對象方式
這篇文章主要介紹了使用Spring的FactoryBean創(chuàng)建和獲取Bean對象方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03

