mirror of https://github.com/longtai-cn/hippo4j
parent
e0af0ac9ad
commit
d3828028bd
@ -0,0 +1,31 @@
|
|||||||
|
package cn.hippo4j.console.controller;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.constant.Constants;
|
||||||
|
import cn.hippo4j.common.web.base.Result;
|
||||||
|
import cn.hippo4j.common.web.base.Results;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import static cn.hippo4j.common.constant.Constants.UP;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health check controller.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/12/8 21:02
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@AllArgsConstructor
|
||||||
|
@RequestMapping(Constants.BASE_PATH + "/health/check")
|
||||||
|
public class HealthCheckController {
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public Result<String> healthCheck() {
|
||||||
|
return Results.success(UP);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,113 @@
|
|||||||
|
package cn.hippo4j.starter.remote;
|
||||||
|
|
||||||
|
import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.Condition;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract health check.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/12/8 20:19
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public abstract class AbstractHealthCheck implements InitializingBean, ServerHealthCheck {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health status
|
||||||
|
*/
|
||||||
|
private volatile boolean healthStatus = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health main lock
|
||||||
|
*/
|
||||||
|
private final ReentrantLock healthMainLock = new ReentrantLock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health condition
|
||||||
|
*/
|
||||||
|
private final Condition healthCondition = healthMainLock.newCondition();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health check executor.
|
||||||
|
*/
|
||||||
|
private final ScheduledThreadPoolExecutor healthCheckExecutor = new ScheduledThreadPoolExecutor(
|
||||||
|
new Integer(1),
|
||||||
|
ThreadFactoryBuilder.builder().daemon(true).prefix("health-check-scheduled").build()
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send health check.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected abstract boolean sendHealthCheck();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health check.
|
||||||
|
*/
|
||||||
|
public void healthCheck() {
|
||||||
|
boolean healthCheckStatus = sendHealthCheck();
|
||||||
|
if (healthCheckStatus) {
|
||||||
|
if (Objects.equals(healthStatus, false)) {
|
||||||
|
healthStatus = true;
|
||||||
|
log.info("🚀 The client reconnects to the server successfully.");
|
||||||
|
signalAllBizThread();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
healthStatus = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public boolean isHealthStatus() {
|
||||||
|
while (!healthStatus) {
|
||||||
|
healthMainLock.lock();
|
||||||
|
try {
|
||||||
|
healthCondition.await();
|
||||||
|
} finally {
|
||||||
|
healthMainLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return healthStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setHealthStatus(boolean healthStatus) {
|
||||||
|
healthMainLock.lock();
|
||||||
|
try {
|
||||||
|
this.healthStatus = healthStatus;
|
||||||
|
log.warn("The server health status setting is unavailable.");
|
||||||
|
} finally {
|
||||||
|
healthMainLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signal all biz thread.
|
||||||
|
*/
|
||||||
|
private void signalAllBizThread() {
|
||||||
|
healthMainLock.lock();
|
||||||
|
try {
|
||||||
|
healthCondition.signalAll();
|
||||||
|
} finally {
|
||||||
|
healthMainLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
healthCheckExecutor.scheduleWithFixedDelay(() -> healthCheck(), 0, 5, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package cn.hippo4j.starter.remote;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.web.base.Result;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static cn.hippo4j.common.constant.Constants.HEALTH_CHECK_PATH;
|
||||||
|
import static cn.hippo4j.common.constant.Constants.UP;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server health check.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/12/8 20:16
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class HttpScheduledHealthCheck extends AbstractHealthCheck {
|
||||||
|
|
||||||
|
private final HttpAgent httpAgent;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean sendHealthCheck() {
|
||||||
|
boolean healthStatus = false;
|
||||||
|
try {
|
||||||
|
Result healthResult = httpAgent.httpGetSimple(HEALTH_CHECK_PATH);
|
||||||
|
if (healthResult != null && Objects.equals(healthResult.getData(), UP)) {
|
||||||
|
healthStatus = true;
|
||||||
|
}
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
log.error("Failed to periodically check the health status of the server.", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return healthStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package cn.hippo4j.starter.remote;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server health check.
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/12/8 20:08
|
||||||
|
*/
|
||||||
|
public interface ServerHealthCheck {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is health status.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean isHealthStatus();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set health status.
|
||||||
|
*
|
||||||
|
* @param healthStatus
|
||||||
|
*/
|
||||||
|
void setHealthStatus(boolean healthStatus);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue