1 package org.onap.msb.apiroute.health;
3 import org.apache.commons.lang3.StringUtils;
4 import org.onap.msb.apiroute.ApiRouteApp;
5 import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
6 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
7 import org.onap.msb.apiroute.wrapper.util.HttpClientUtil;
8 import org.onap.msb.apiroute.wrapper.util.HttpGetResult;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import com.codahale.metrics.health.HealthCheck;
14 public class ConsulLinkHealthCheck extends HealthCheck implements Runnable {
16 private static final Logger LOGGER = LoggerFactory
17 .getLogger(ConsulLinkHealthCheck.class);
19 private final static String CONSUL_IP_ENV = ConfigUtil.getInstance().getConsul_ip();
21 private static int failedLoopCheckNum = 12;
22 private static int failedTimer = 5 * 1000;
24 private static int normalTimer = 20 * 1000;
25 private static Result result = Result.healthy();
27 private String CHECK_IP = "127.0.0.1";
28 private String CHECK_PORT = "8500";
29 private String CHECK_URL = "http://" + CHECK_IP + ":" + CHECK_PORT
30 + "/v1/status/leader";
32 public static Result getResult() {
37 protected Result check() {
38 // TODO Auto-generated method stub
40 if (!StringUtils.isBlank(CONSUL_IP_ENV)) {
41 CHECK_IP = CONSUL_IP_ENV;
42 CHECK_URL = "http://" + CHECK_IP + ":" + CHECK_PORT
43 + "/v1/status/leader";
45 if (LOGGER.isDebugEnabled()) {
46 LOGGER.debug("check consul URL:" + CHECK_URL);
51 HttpGetResult result = HttpClientUtil
52 .httpGetStatusAndBody(CHECK_URL);
54 //response format:"127.0.0.1:8300"
55 if (result.getStatusCode() == 200 && result.getBody() != null
56 && result.getBody().contains(":8300")) {
57 return Result.healthy();
59 return Result.unhealthy("check consul link " + CHECK_URL
60 + " fail:" + result.getStatusCode()+":"+result.getBody());
63 } catch (Exception e) {
65 "ConsulLinkHealthCheck:" + CHECK_URL + " execption", e);
66 return Result.unhealthy("check consul link " + CHECK_URL
72 return Result.healthy();
77 // TODO Auto-generated method stub
80 if (LOGGER.isDebugEnabled()) {
81 LOGGER.debug("consul link check starttime:"
82 + System.currentTimeMillis());
85 result = checkWithPolicy();
87 if (LOGGER.isDebugEnabled()) {
88 LOGGER.debug("consul link check result:" + result.isHealthy()
89 + " message:" + result.getMessage());
91 LOGGER.debug("consul link check endtime:"
92 + System.currentTimeMillis());
96 Thread.sleep(normalTimer);
97 } catch (InterruptedException e) {
98 // TODO Auto-generated catch block
99 LOGGER.warn("loop check consul,thread sleep excepiton", e);
104 private Result checkWithPolicy() {
106 Result temp = Result.healthy();
113 if (temp.isHealthy()) {
121 Thread.sleep(failedTimer);
122 } catch (InterruptedException e) {
123 // TODO Auto-generated catch block
124 LOGGER.warn("loop check consul,thread sleep excepiton", e);
127 } while (failedNum <= failedLoopCheckNum);