All Downloads are FREE. Search and download functionalities are using the official Maven repository.

club.zhcs.lina.starter.monitor.health.HealthChecker Maven / Gradle / Ivy

package club.zhcs.lina.starter.monitor.health;

import org.nutz.lang.Stopwatch;
import org.nutz.lang.random.R;
import org.springframework.boot.actuate.health.Status;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import club.zhcs.lina.starter.monitor.MonitorConfigurationProperties.Chain.Service;
import club.zhcs.lina.starter.monitor.MonitorConfigurationProperties.Chain.Service.Endpoint;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

/**
 * 
 * @author Kerbores([email protected])
 *
 */
@RequiredArgsConstructor
public class HealthChecker {

    private final RestTemplate restTemplate;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ServiceHealth {
        @Default
        Status status = Status.UP;
        @Default
        long time = R.random(10, 100);
        String service;
    }

    public ServiceHealth check(Service service) {
        String checkEndpoint = service.getEndpoint() == Endpoint.CUSTOMER ? service.getCustomerCheckUrl() : service.getEndpoint().getUrl();
        Stopwatch stopwatch = Stopwatch.begin();
        ResponseEntity result = restTemplate.getForEntity(String.format("%s%s", service.getDomain(), checkEndpoint), String.class);
        stopwatch.stop();
        return ServiceHealth.builder()
                            .service(service.getName())
                            .time(stopwatch.du())
                            .status(result.getStatusCode() == HttpStatus.OK ? Status.UP : Status.OUT_OF_SERVICE)
                            .build();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy