自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何做好對Spring Boot項目全面監(jiān)控之Actuator

開發(fā) 架構
Actuator 為Spring Boot項目提供了很全面的監(jiān)控和審查,通過啟用和公開Endpoints,可以很方便的查看項目中的一些指標。

定義

Actuator 為springboot項目提供了很全面的監(jiān)控和審查,通過啟用和公開endpoints,可以很方便的查看項目中的一些指標。

添加依賴

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

關于Endpoints

Actuator提供了很多內置的Endpoints,每一個EP都可以啟用/禁用 和 公開。

當然,EP只有啟動并公開之后,才會真正生效。

如何啟用/禁用Endpoints?

除了 shutdown 之外,所有的EP都是默認啟用的,如果要啟用/禁用EP,可使用以下配置:

management.endpoint.<id>.enabled=true/false?。

比如啟用 shutdown :

management.endpoint.shutdown.enabled=true

為了安全,在生產(chǎn)環(huán)境不建議啟用所有的EP,而是按需啟用,所以要首先禁用默認行為,然后啟用需要的EP,如下:

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

這樣只有啟用的EP才會加載到上下文。

這樣只是啟用和禁用了EP,但是否暴露出去,還需要單獨的配置。

如何暴露/隱藏 Endpoints?

EP會暴露應用的很多指標,所以非常重要和敏感,在生產(chǎn)環(huán)境一定要做好選擇和安全防護。

暴露和隱藏使用以下配置:

# 針對 JMX 規(guī)范協(xié)議
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
# 針對http協(xié)議
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include health

使用 include 暴露Ep,使用 exclude 隱藏 Ep,其中 exclude 優(yōu)先級高于 include。

星號 * 表示全部的,另外星號在properties和yaml中有一點不同:

在YAML中星號(*)具有特殊的含義,所以需要用雙引號括起來,如 "*"

比如要隱藏所有的并僅僅啟用health和info:

management.endpoints.web.exposure.include=health,info

比如要暴露所有并禁用env和beans:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

前文有述,EP包括很多敏感信息,一定要注意安全,那應該如何確保安全?

如何實現(xiàn)Http Endpoints 安全加固

如果項目中使用了 spring security 或 shiro 等安全框架,可以把EP放在安全框架之后,和其他業(yè)務API一樣保護起來。

或者,也可以使用RequestMatcher對象來配合使用,以控制某些用戶具備訪問權限,比如:

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration(proxyBeanMethods = false)
public class MySecurityConfiguration {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
http.httpBasic();
return http.build();
}
}

這樣只有具備 ENDPOINT_ADMIN 角色的用戶才可以訪問EP。

如何通過頁面訪問各類指標

通過項目的 ip:port /actuator ,比如 localhost:8080/actuator:

可以通過配置修改路徑:

# 修改 /actutor  /manage
management.endpoints.web.base-path=/manage

# 修改特定指標 /health /myhealth
management.endpoints.web.path-mapping.health=myhealth

# 修改訪問端口(默認和server.port相同)
management.server.port=8036
責任編輯:姜華 來源: 今日頭條
相關推薦

2022-02-09 20:39:52

Actuator應用監(jiān)控

2022-06-22 08:02:01

業(yè)務監(jiān)控Web站點監(jiān)控

2017-01-19 18:06:19

聽云Network

2017-04-19 14:23:08

項目管理分配

2019-04-29 09:52:46

容器安全漏洞網(wǎng)絡安全

2011-05-26 16:27:24

SEO

2020-07-22 07:00:00

微服務架構

2019-08-19 09:01:54

項目管理

2023-04-11 16:04:19

Spring Boo端點運維

2021-01-19 09:59:02

招聘管理團隊

2011-04-18 13:20:40

單元測試軟件測試

2013-07-10 09:22:59

云配置云實踐云應用程序接口

2022-01-04 09:01:10

開源項目開源技術

2010-09-07 16:09:29

2022-05-07 19:18:16

防御性編碼代碼

2020-02-05 14:49:04

網(wǎng)絡性能優(yōu)化微調

2011-05-19 10:20:23

2014-10-30 10:53:22

Android內存優(yōu)化

2021-04-07 14:45:56

軟件測試編程

2018-05-15 15:33:07

Leader前端團隊
點贊
收藏

51CTO技術棧公眾號