您好,登錄后才能下訂單哦!
Springboot中 actuator如何使用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Springboot actuator是一個追蹤各種springboot應用狀態的健康檢查機制,使用需要添加一個pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
在啟動springboot中,我們往往會看到這樣的一條日志 Exposing 20 endpoint(s) beneath base path '/actuator'
這個20是每一個springboot應用程序的健康檢查點的個數,他是隨著你配置文件中的配置而不同的。
由于本人配置的Server信息如下
server: port: 8001 servlet: context-path: /api-u
所以當我們訪問actuator Web信息的路徑如下
http://127.0.0.1:8001/api-u/actuator
這里需要注意的是,如果我們配置了oauth 2的資源訪問權限的時候,需要對該路徑放行
@EnableResourceServer@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.csrf().disable().exceptionHandling() .authenticationEntryPoint( (request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED)) .and().authorizeRequests().antMatchers(PermitAllUrl.permitAllUrl("/users-anon/**", "/sys/login","/actuator/**")).permitAll() //此處添加"/actuator/**"進行放行.anyRequest().authenticated().and().httpBasic(); } @Override public void configure(ResourceServerSecurityConfigurer resource) throws Exception { //這里把自定義異常加進去 resource.authenticationEntryPoint(new AuthExceptionEntryPoint()) .accessDeniedHandler(new CustomAccessDeniedHandler()); } @Bean public BCryptPasswordEncoder bCryptPasswordEncoder() { return new BCryptPasswordEncoder(); } }
在配置文件中做如下配置
management: endpoint: health: show-details: always
此時我們訪問http://127.0.0.1:8001/api-u/actuator的結果如下
{
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8001/api-u/actuator" ,
"templated" : false
},
"health" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/health" ,
"templated" : false
},
"info" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/info" ,
"templated" : false
}
}
}
具體大家可以參考這個官網說明 https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/中的
https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#production-ready-endpoints-enabling-endpoints以及 https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#_auto_configured_healthindicators
其中的http://127.0.0.1:8001/api-u/actuator/health就是我們需要的健康檢查的具體信息
{
"status" : "DOWN" ,
"details" : {
"rabbit" : {
"status" : "DOWN" ,
"details" : {
"error" : "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)"
}
},
"diskSpace" : {
"status" : "UP" ,
"details" : {
"total" : 499963174912 ,
"free" : 435209195520 ,
"threshold" : 10485760
}
},
"db" : {
"status" : "UP" ,
"details" : {
"database" : "MySQL" ,
"hello" : 1
}
},
"refreshScope" : {
"status" : "UP"
},
"discoveryComposite" : {
"status" : "UP" ,
"details" : {
"discoveryClient" : {
"status" : "UP" ,
"details" : {
"services" : [
"register-center" ,
"user-center"
]
}
},
"eureka" : {
"description" : "Remote status from Eureka server" ,
"status" : "UP" ,
"details" : {
"applications" : {
"REGISTER-CENTER" : 1 ,
"USER-CENTER" : 1
}
}
}
}
},
"configServer" : {
"status" : "UNKNOWN" ,
"details" : {
"error" : "no property sources located"
}
},
"hystrix" : {
"status" : "UP"
},
"redis" : {
"status" : "UP" ,
"details" : {
"version" : "3.2.12"
}
}
}
}
這里為該進程spring支持的各種服務的健康狀態,UP為在線,DOWN為下線狀態,UNKNOWN為不知道。
除了http://127.0.0.1:8001/api-u/actuator/health之外還有一個http://127.0.0.1:8001/api-u/actuator/info
這是一個描述信息的說明,如果我們在配置文件中做如下配置
info: app-name: user author: guanjian email: 12345@xy.com
則可以在http://127.0.0.1:8001/api-u/actuator/info的返回信息中看到
{
"app-name" : "user" ,
"author" : "guanjian" ,
"email" : "12345@xy.com"
}
但是我們在以上配置中可以看到的信息很少,需要激活所有的actuator端點,增加配置如下
management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always
此時再次訪問http://127.0.0.1:8001/api-u/actuator的結果如下
{
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8001/api-u/actuator" ,
"templated" : false
},
"archaius" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/archaius" ,
"templated" : false
},
"auditevents" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/auditevents" ,
"templated" : false
},
"beans" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/beans" ,
"templated" : false
},
"health" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/health" ,
"templated" : false
},
"conditions" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/conditions" ,
"templated" : false
},
"configprops" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/configprops" ,
"templated" : false
},
"env" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/env" ,
"templated" : false
},
"env-toMatch" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/env/{toMatch}" ,
"templated" : true
},
"info" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/info" ,
"templated" : false
},
"logfile" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/logfile" ,
"templated" : false
},
"loggers-name" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/loggers/{name}" ,
"templated" : true
},
"loggers" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/loggers" ,
"templated" : false
},
"heapdump" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/heapdump" ,
"templated" : false
},
"threaddump" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/threaddump" ,
"templated" : false
},
"metrics-requiredMetricName" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/metrics/{requiredMetricName}" ,
"templated" : true
},
"metrics" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/metrics" ,
"templated" : false
},
"scheduledtasks" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/scheduledtasks" ,
"templated" : false
},
"sessions-sessionId" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/sessions/{sessionId}" ,
"templated" : true
},
"sessions" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/sessions" ,
"templated" : false
},
"httptrace" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/httptrace" ,
"templated" : false
},
"mappings" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/mappings" ,
"templated" : false
},
"refresh" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/refresh" ,
"templated" : false
},
"features" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/features" ,
"templated" : false
},
"service-registry" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/service-registry" ,
"templated" : false
}
}
}
現在來挑幾個做一下說明 http://127.0.0.1:8001/api-u/actuator/configprops 查看所有的配置信息(當然密碼會隱藏) 片段
"spring.cloud.config-org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties" : {
"prefix" : "spring.cloud.config" ,
"properties" : {
"overrideSystemProperties" : true ,
"overrideNone" : false ,
"allowOverride" : true
}
},
"configClientProperties" : {
"prefix" : "spring.cloud.config" ,
"properties" : {
"headers" : {},
"discovery" : {
"enabled" : false ,
"serviceId" : "configserver"
},
"profile" : "default" ,
"name" : "user-center" ,
"uri" : "http://localhost:8888" ,
"enabled" : true ,
"failFast" : false ,
"username" : "user"
}
},
http://127.0.0.1:8001/api-u/actuator/metrics 查看某些指標的具體數值,比如JVM,tomcat等等
{
"names" : [
"rabbitmq.acknowledged" ,
"rabbitmq.consumed" ,
"jvm.buffer.memory.used" ,
"jvm.memory.used" ,
"jvm.gc.memory.allocated" ,
"rabbitmq.connections" ,
"jvm.memory.committed" ,
"tomcat.global.request.max" ,
"tomcat.sessions.created" ,
"tomcat.sessions.expired" ,
"jvm.gc.max.data.size" ,
"logback.events" ,
"rabbitmq.published" ,
"system.cpu.count" ,
"jvm.memory.max" ,
"rabbitmq.rejected" ,
"jvm.buffer.total.capacity" ,
"jvm.buffer.count" ,
"process.files.max" ,
"jvm.threads.daemon" ,
"rabbitmq.channels" ,
"process.start.time" ,
"tomcat.global.error" ,
"tomcat.sessions.active.max" ,
"http.server.requests" ,
"tomcat.global.sent" ,
"jvm.gc.live.data.size" ,
"process.files.open" ,
"process.cpu.usage" ,
"tomcat.global.received" ,
"tomcat.servlet.request" ,
"jvm.gc.pause" ,
"process.uptime" ,
"tomcat.threads.config.max" ,
"system.load.average.1m" ,
"tomcat.cache.hit" ,
"tomcat.servlet.error" ,
"tomcat.threads.current" ,
"tomcat.servlet.request.max" ,
"tomcat.cache.access" ,
"tomcat.sessions.active.current" ,
"system.cpu.usage" ,
"jvm.threads.live" ,
"jvm.classes.loaded" ,
"jvm.classes.unloaded" ,
"tomcat.threads.busy" ,
"jvm.threads.peak" ,
"jvm.gc.memory.promoted" ,
"tomcat.sessions.rejected" ,
"tomcat.sessions.alive.max" ,
"tomcat.global.request"
]
}
比方說我們要查看JVM的最大內存,訪問 http://127.0.0.1:8001/api-u/actuator/metrics/jvm.memory.max
{
"name" : "jvm.memory.max" ,
"measurements" : [
{
"statistic" : "VALUE" ,
"value" : 5.587337215E9
}
],
"availableTags" : [
{
"tag" : "area" ,
"values" : [
"heap" ,
"nonheap"
]
},
{
"tag" : "id" ,
"values" : [
"Compressed Class Space" ,
"PS Survivor Space" ,
"PS Old Gen" ,
"Metaspace" ,
"PS Eden Space" ,
"Code Cache"
]
}
]
}
http://127.0.0.1:8001/api-u/actuator/metrics/jvm.memory.used JVM已經使用的內存
{
"name" : "jvm.memory.used" ,
"measurements" : [
{
"statistic" : "VALUE" ,
"value" : 9.31419992E8
}
],
"availableTags" : [
{
"tag" : "area" ,
"values" : [
"heap" ,
"nonheap"
]
},
{
"tag" : "id" ,
"values" : [
"Compressed Class Space" ,
"PS Old Gen" ,
"PS Survivor Space" ,
"Metaspace" ,
"PS Eden Space" ,
"Code Cache"
]
}
]
}
http://127.0.0.1:8001/api-u/actuator/beans 查看spring中注入的所有bean
部分片段
"spring.cloud.config-org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties" : {
"aliases" : [],
"scope" : "singleton" ,
"type" : "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties" ,
"resource" : null ,
"dependencies" : []
},
"propertySourcesPlaceholderConfigurer" : {
"aliases" : [],
"scope" : "singleton" ,
"type" : "org.springframework.context.support.PropertySourcesPlaceholderConfigurer" ,
"resource" : "org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration" ,
"dependencies" : []
}
如果我們不想激活所有的端點,只激活部分端點,比如configprops,metrics,beans,health,info,配置如下
management: endpoints: web: exposure: include: "configprops,metrics,beans,health,info" endpoint: health: show-details: always
訪問http://127.0.0.1:8001/api-u/actuator結果如下
{
"_links" : {
"self" : {
"href" : "http://127.0.0.1:8001/api-u/actuator" ,
"templated" : false
},
"beans" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/beans" ,
"templated" : false
},
"health" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/health" ,
"templated" : false
},
"configprops" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/configprops" ,
"templated" : false
},
"info" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/info" ,
"templated" : false
},
"metrics-requiredMetricName" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/metrics/{requiredMetricName}" ,
"templated" : true
},
"metrics" : {
"href" : "http://127.0.0.1:8001/api-u/actuator/metrics" ,
"templated" : false
}
}
}
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。