首頁(yè) 資訊 Eureka健康檢查和安全配置

Eureka健康檢查和安全配置

來(lái)源:泰然健康網(wǎng) 時(shí)間:2025年06月10日 22:18

Eureka 健康檢查

由于server和client通過心跳保持 服務(wù)狀態(tài),而只有狀態(tài)為UP的服務(wù)才能被訪問??磂ureka界面中的status。

比如心跳一直正常,服務(wù)一直UP,但是此服務(wù)DB連不上了,無(wú)法正常提供服務(wù)。

此時(shí),我們需要將微服務(wù)的健康狀態(tài)也同步到server。只需要啟動(dòng)eureka的健康檢查就行。這樣微服務(wù)就會(huì)將自己的健康狀態(tài)同步到eureka。配置如下即可。

eureka.client.healthcheck.enabled=true

可以改變服務(wù)的狀態(tài),只需實(shí)現(xiàn)HealthIndicator接口即可。需要添加依賴

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

HealthStatusService
 

@Service public class HealthStatusService implements HealthIndicator { private Boolean status = true; public void setStatus(Boolean status) { this.status = status; } @Override public Health health() { if (status) return new Health.Builder().up().build(); return new Health.Builder().down().build(); } public String getStatus() { return this.status.toString(); } }

測(cè)試Controller:

@GetMapping("/health") public String health(@RequestParam("status") Boolean status) { healthStatusSrv.setStatus(status); return healthStatusSrv.getStatus(); }

訪問http://localhost:8001/health?status=false,在訪問http://localhost:8761/,等待30秒左右即可看到:

consumer服務(wù)的狀態(tài)變?yōu)閐own。

訪問http://localhost:8001/health?status=true,在訪問http://localhost:8761/,等待30秒左右即可看到:

 
可以通過這種方式控制服務(wù)的狀態(tài)。

安全配置

如果Eureka Service加入了Spring Security依賴,那么客戶端訪問的時(shí)候必須攜帶用戶名和密碼。比如:eureka.client.service-url.defaultZone=http://user:123@localhost:8761/eureka/。

并且Eureka Service必須關(guān)閉Spring Security的csrf配置:

@Configuration @EnableWebSecurity public class MySecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); super.configure(http); } }

否則注冊(cè)服務(wù)會(huì)報(bào)錯(cuò)。

相關(guān)知識(shí)

Eureka心跳健康檢查機(jī)制
Eureka自我保護(hù)機(jī)制、健康檢查的作用、actuator模塊監(jiān)控
Eureka心跳健康檢查機(jī)制與自我保護(hù)機(jī)制
揭秘微服務(wù)健康檢查:如何保障系統(tǒng)穩(wěn)定運(yùn)行的關(guān)鍵一步
健康檢查配置
為本地IDC和VBR配置健康檢查
VPN 連接 配置健康檢查
nginx配置后端服務(wù)健康檢查 nginx健康檢查模塊
容器啟動(dòng)健康檢查的配置 – PingCode
配置健康檢查探測(cè)物理專線連通性

網(wǎng)址: Eureka健康檢查和安全配置 http://www.u1s5d6.cn/newsview1393583.html

推薦資訊