主要是為了讓 k8s
識(shí)別到網(wǎng)關(guān)項(xiàng)目已經(jīng)就緒,但是又不想在里面通過 Controller
實(shí)現(xiàn)。因?yàn)樵?Controller
中這樣做并不是最佳實(shí)踐,因?yàn)?Gateway
的設(shè)計(jì)初衷是專注于路由和過濾,而不是業(yè)務(wù)邏輯的處理。
在 Gateway
中配置健康檢查端點(diǎn)可以通過以下方式進(jìn)行(可根據(jù)實(shí)際需求進(jìn)行擴(kuò)展):
1. 自定義路由配置(推薦)
可以使用 Spring Cloud Gateway
的 Java DSL 配置自定義的路由,以在網(wǎng)關(guān)中添加一個(gè)專門用于健康檢查的路由。例如:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
@Configuration
public class GatewayConfig {
@Bean
public RouterFunction<ServerResponse> healthCheckRoute() {
return route(GET("/health-check"),
request -> ServerResponse.status(HttpStatus.OK)
.body(BodyInserters.fromValue("Gateway is healthy")));
}
}
這樣,就可以通過訪問 http://localhost:8080/health-check
來執(zhí)行健康檢查。
上面代碼的描述說明:
-
route
方法是Spring Cloud Gateway
提供的Java DSL
(領(lǐng)域特定語言,Domain-Specific Language
)中的一部分。這是一種聲明性的路由配置方式,允許使用流暢的 API 配置路由規(guī)則。 - 在
GatewayConfig
類中,healthCheckRoute
方法返回一個(gè)RouterFunction<ServerResponse>
,這個(gè)函數(shù)式接口是用來配置路由規(guī)則的。route
方法是用于創(chuàng)建路由規(guī)則的,它接收一個(gè)請(qǐng)求謂詞(RequestPredicate
)和一個(gè)處理函數(shù)(HandlerFunction
)作為參數(shù)。 - 具體來說,
route(GET("/health-check"), ...)
表示創(chuàng)建一個(gè)滿足GET
請(qǐng)求謂詞,并且路徑為/health-check
的路由規(guī)則。接著,通過ServerResponse
構(gòu)建響應(yīng),這里設(shè)置為 HTTP 狀態(tài)碼HttpStatus.OK
,并返回一條消息 “Gateway is healthy
”。
這種方式更加直觀和類型安全,相比于配置文件,可以在代碼中清晰地看到路由規(guī)則的定義。這是 Spring WebFlux
框架提供的一種路由方式,用于構(gòu)建響應(yīng)式的、非阻塞的 Web 應(yīng)用程序。
2. 利用 Actuator 健康檢查
可以通過配置屬性來啟用 Actuator
,并將其端口設(shè)置為用于健康檢查的端口。
要想使用 Actuator
,項(xiàng)目中需要引入 Actuator
的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在 application.properties
或 application.yml
配置文件中添加以下配置:
# application.properties
management.endpoints.web.exposure.include=health
management.server.port=8081 # 可與 server.port 相同或不同
或者
# application.yaml
management:
endpoints:
web:
exposure:
include: health
server:
port: 8081 # 可與 server.port 相同或不同
上述配置將 Actuator
暴露的端點(diǎn)配置為僅包含 health
,并將健康檢查端口設(shè)置為 8081
。此時(shí),可以通過訪問 http://localhost:8081/actuator/health
來執(zhí)行健康檢查。
【注】Actuator
中還有其他配置,實(shí)際使用過程中建議查看 官方文檔 了解相應(yīng)的配置,以免給自己挖坑。比如 /env
和 /beans
,它們可能會(huì)泄漏應(yīng)用程序的敏感信息。確保只在受信任的環(huán)境中啟用這些端點(diǎn),并謹(jǐn)慎處理它們的輸出。
3. 通過 RestController 實(shí)現(xiàn)(不推薦)
如果有業(yè)務(wù)邏輯需要處理,更推薦將業(yè)務(wù)邏輯集中在后端微服務(wù)中,而將 Gateway
專注于路由和過濾。這樣可以更好地保持清晰的代碼結(jié)構(gòu)和單一責(zé)任原則。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author roc
* @date 2024/1/16 14:14
*/
@RestController
@RequestMapping("/health")
public class HealthController {
@GetMapping("/check")
public String check() {
return "Gateway is healthy";
}
}
如果還有其他方式請(qǐng)大佬們分享一下。文章來源:http://www.zghlxwxcb.cn/news/detail-797822.html
個(gè)人博客:Roc’s Blog文章來源地址http://www.zghlxwxcb.cn/news/detail-797822.html
到了這里,關(guān)于Spring Cloud 微服務(wù)中 gateway 網(wǎng)關(guān)如何設(shè)置健康檢測(cè)端點(diǎn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!