Skip to content

Commit

Permalink
增加主动健康检查功能
Browse files Browse the repository at this point in the history
  • Loading branch information
anjia0532 committed Jun 26, 2024
1 parent a1f498a commit e61b1d2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gateway等网关插件的高扩展性
### 通过docker运行

```bash
docker run anjia0532/discovery-syncer-python:v2.5.2
docker run anjia0532/discovery-syncer-python:v2.5.3
```

特别的,`-c ` 支持配置远端http[s]的地址,比如读取静态资源的,比如读取nacos的
Expand Down
5 changes: 3 additions & 2 deletions app/model/syncer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from app.model.config import HealthCheckType
from core.lib.logger import for_model
from core.lib.util import http_status_in_array

logger = for_model(__name__)

Expand Down Expand Up @@ -100,10 +101,10 @@ def health_check(self, healthcheck: dict, sqla_helper: SqlaReflectHelper):
resp = httpx.request(method=healthcheck.get("method", "GET").upper(),
url=f"{schema}{self.instance}{healthcheck.get('uri')}",
timeout=healthcheck.get("timeout-sec", 30))
if resp.status_code in healthcheck.get("healthy", {}).get("http_statuses", []):
if http_status_in_array(resp.status_code, healthcheck.get("healthy", {}).get("http_statuses", [])):
params['successes'] = 1
success = True
if resp.status_code in healthcheck.get("unhealthy", {}).get("http_statuses", []):
elif http_status_in_array(resp.status_code, healthcheck.get("unhealthy", {}).get("http_statuses", [])):
params['failures'] = 1
except TimeoutException as e:
params['timeouts'] = 1
Expand Down
4 changes: 4 additions & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ targets:
# 健康检查http方法,默认是GET
method: GET
# 健康断言 实例状态默认是 unknown
# 特别的 相同百位数的状态码,可以缩写成 xx,比如 1xx[100-199], 2xx[200-299], 3xx[300-399], 4xx[400-499], 5xx[500-599]
# 健康检查返回的状态码同时满足 healthy.http_statuses 和 unhealthy.http_statuses 交集 status,则认为是健康节点
# 比如 http_status 是 401,healthy.http_statuses 是['4xx'], unhealthy.http_statuses 是[401],则认为是健康节点
# 比如 http_status 是 401,healthy.http_statuses 是[400], unhealthy.http_statuses 是['4xx'],则认为是不健康节点
healthy:
# 如果健康检查 访问 http[s]://ip:port+uri,在timeout超时之前,返回了 http_statuses 指定的状态码,则视为健康节点
http_statuses: [200]
Expand Down
14 changes: 13 additions & 1 deletion core/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

T = typing.TypeVar('T')


# global encoding
ENCODING = 'utf-8'

Expand Down Expand Up @@ -53,3 +52,16 @@ def pfmt(o: Any, *args, **kwargs) -> str:
:return: object string
"""
return pprint.pformat(o, *args, **kwargs)


def http_status_in_array(status: int, arr: []) -> bool:
"""
check if http status is in array
:param status: http status code
:param arr: array of status codes
:return: if status is in array
"""
if not arr:
return False
status_str = str(status)[0:1] + "xx"
return status in arr or status_str in arr or status_str.upper() in arr

0 comments on commit e61b1d2

Please sign in to comment.