[英]How to work correctly with ThreadPoolExecutor in Spring Boot Application
[英]How to understand that spring boot application is ready to work?
我在 spring 上有幾個微服務。 其中之一是配置服務器。 我嘗試使用 docker-compose 完全啟動服務。 但是有問題。 微服務嘗試在初始化之前從配置服務器獲取配置。 我想為微服務的啟動編寫腳本,防止錯誤啟動。 我現在必須知道如何確定配置服務器准備就緒的時刻。 端口偵聽不起作用。 Docker 隱藏了有關其網絡的信息。 我相信有比標准輸出解析更好的方法。
有人知道我如何確定 Spring Boot 應用程序啟動的時間嗎?
有人知道我如何確定 Spring Boot 應用程序啟動的時間嗎?
當您的應用程序從/health
端點返回代碼200
並帶有這樣的負載時,它必須准備就緒
{
"status": "UP"
}
這意味着您的應用程序沒有問題並且可以工作了,無論如何這個端點通常被一個監視器應用程序用來了解您的應用程序健康狀況,這樣它就可以維護集群,這個端點不常被其他應用程序使用,這個監視器通常
/health
返回DOWN狀態時解決它的問題看看docker healthcheck ,它使用與 spring 相同的概念
要使該端點可用,您需要添加彈簧執行器依賴項,這是一個 gradle 示例
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.10.RELEASE'
微服務嘗試在配置服務器初始化之前從配置服務器獲取配置......
這里有一些要點要明確
所以我的建議是讓你的應用程序在嘗試從配置服務器應用程序獲取信息時失敗,如果失敗,你可以做一些事情:
/health
添加一個自定義檢查,將此信息暴露給某個監控微服務System.exit(-1)
並讓docker-compose一次又一次地重新啟動您的應用程序,直到配置服務器回答有用的內容這是一些模擬您的案例並使用 docker 解決該問題的緊湊示例
config-server需要比app-1更長的時間來准備好,然后app-1將保持不健康狀態,直到config-server正確響應
version: '3.4'
services:
mg-config-server:
image: nginx:1.10
healthcheck:
test: ["CMD", "bash", "-c", "sleep 15; exit 0"]
interval: 10s
timeout: 17s
retries: 3
start_period: 10s
networks:
- my-net
command: bash -c "echo starting; sleep 20; nginx -g 'daemon off;'"
mg-app-1:
image: alpine:3.7
healthcheck:
test: ["CMD", "curl", "http://mg-config-server"]
interval: 5s
timeout: 5s
retries: 3
start_period: 1s
command: sh -c 'echo starting; apk add --update curl; tail -f /dev/null'
restart: always
networks:
- my-net
dns:
- 8.8.8.8
networks:
my-net:
driver: overlay
然后就開始吧
docker-compose up
docker ps | grep "mg"
無論如何,在這種情況下使用 docker swarm 更有意義,因為它會檢查健康檢查端點並在不健康的情況下重新啟動容器
docker swarm init --advertise-addr <your-machine-ip>
docker stack deploy --compose-file docker-compose.yml my-stack && docker ps | grep "my-stack"
Docker 版本: 18.02.0-ce
抱歉回答太長,希望能幫到你
如果您的問題特定於配置服務器,您還可以實現 spring 重試機制
spring:
application:
name: test-service
cloud:
config:
enabled: true
uri: ${CONFIG_SERVER_URL:http://127.0.0.1:8761} #where the config-service is running
fail-fast: true #the service will not run if it can't reach the config-service
name: common,test-service
retry:
max-attempts: 10
max-interval: 5000
您還需要具有 spring-retry 依賴項
// https://mvnrepository.com/artifact/org.springframework.retry/spring-retry
compile group: 'org.springframework.retry', name: 'spring-retry', version: '1.2.4.RELEASE'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.