2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.msb.sdclient.health;
16 import org.apache.commons.lang3.StringUtils;
17 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.codahale.metrics.health.HealthCheck;
23 public class ConsulLinkHealthCheck extends HealthCheck {
26 private static final Logger LOGGER = LoggerFactory.getLogger(ConsulLinkHealthCheck.class);
28 private String CHECK_IP = "127.0.0.1";
29 private String CHECK_PORT = "8500";
30 private String CHECK_URL = "http://" + CHECK_IP + ":" + CHECK_PORT + "/v1/status/leader";
34 protected Result check() throws Exception {
35 // TODO Auto-generated method stub
37 if (!StringUtils.isBlank(System.getenv("CONSUL_IP"))) {
38 CHECK_IP = System.getenv("CONSUL_IP");
39 CHECK_URL = "http://" + CHECK_IP + ":" + CHECK_PORT + "/v1/status/leader";
40 LOGGER.info("check consul URL:" + CHECK_URL);
43 int resultStatus = HttpClientUtil.httpGetStatus(CHECK_URL);
45 if (resultStatus == 200) {
46 return Result.healthy();
48 return Result.unhealthy("check consul link {} fail:{}", CHECK_URL, resultStatus);