2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.util.List;
24 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.springframework.stereotype.Component;
27 import com.ecwid.consul.ConsulException;
28 import com.orbitz.consul.Consul;
29 import com.orbitz.consul.HealthClient;
30 import com.orbitz.consul.model.health.ServiceHealth;
33 public class ConsulHealthServiceImpl implements ConsulHealthService {
35 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulHealthServiceImpl.class);
38 public String getServiceLocation(String service, String fallbackPortOnLocalHost) {
40 List<ServiceHealth> nodes = null;
43 Consul consul = Consul.builder().build();
44 HealthClient healthClient = consul.healthClient();
45 nodes = healthClient.getHealthyServiceInstances(service).getResponse();
46 } catch (Exception e) {
47 String localFallbackServiceLocation = "localhost:" + fallbackPortOnLocalHost;
48 logger.debug(EELFLoggerDelegate.debugLogger,
49 " problem getting nodes for service {1}. Defaulting to {2}. Exception: {3}", service,
50 localFallbackServiceLocation, e.getMessage());
51 logger.error(EELFLoggerDelegate.errorLogger,
52 " problem getting nodes for service {1}. Defaulting to {2}. Exception: {3}", service,
53 localFallbackServiceLocation, e);
54 return localFallbackServiceLocation;
57 if (nodes == null || nodes.size() == 0) {
58 logger.debug(EELFLoggerDelegate.debugLogger, "No healthy node found in the consul cluster running service " + service
59 + ". Defaulting to localhost");
60 return "localhost:" + fallbackPortOnLocalHost;
62 String locationFromConsul;
63 ServiceHealth node = nodes.get(0);
64 locationFromConsul = node.getNode().getNode() + ":" + node.getService().getPort();
65 logger.debug(EELFLoggerDelegate.debugLogger,
66 "Found healthy service location using consul - returning location " + locationFromConsul);
68 // if locationFromConsul is null for some reason (very unlikely at
69 // this point), default to localhost
70 if (null == locationFromConsul || "".equals(locationFromConsul)) {
71 logger.debug(EELFLoggerDelegate.debugLogger,
72 "Couldn't get location from consul for service " + service + ". Defaulting to localhost");
73 return "localhost:" + fallbackPortOnLocalHost;
75 logger.debug(EELFLoggerDelegate.debugLogger, "Found service location from consul for service " + service
76 + ". Location is " + locationFromConsul);
77 return locationFromConsul;
83 public List<ServiceHealth> getAllHealthyNodes(String service) throws ConsulException {
84 Consul consul = Consul.builder().build();
85 HealthClient healthClient = consul.healthClient();
86 return healthClient.getHealthyServiceInstances(service).getResponse();
90 public List<ServiceHealth> getAllNodes(String service) {
91 Consul consul = Consul.builder().build();
92 HealthClient healthClient = consul.healthClient();
93 return healthClient.getAllServiceInstances(service).getResponse();