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.portalapp.portal.utils.EcompPortalUtils;
25 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.springframework.stereotype.Component;
28 import com.ecwid.consul.ConsulException;
29 import com.orbitz.consul.Consul;
30 import com.orbitz.consul.HealthClient;
31 import com.orbitz.consul.model.health.ServiceHealth;
34 public class ConsulHealthServiceImpl implements ConsulHealthService {
36 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulHealthServiceImpl.class);
39 public String getServiceLocation(String service, String fallbackPortOnLocalHost) {
41 List<ServiceHealth> nodes = null;
44 Consul consul = Consul.builder().build();
45 HealthClient healthClient = consul.healthClient();
46 nodes = healthClient.getHealthyServiceInstances(service).getResponse();
47 } catch (Exception e) {
48 String localFallbackServiceLocation = EcompPortalUtils.localOrDockerHost() + ":" + fallbackPortOnLocalHost;
49 logger.debug(EELFLoggerDelegate.debugLogger,
50 " problem getting nodes for service {1}. Defaulting to {2}. Exception: {3}", service,
51 localFallbackServiceLocation, e.getMessage());
52 logger.error(EELFLoggerDelegate.errorLogger,
53 " problem getting nodes for service {1}. Defaulting to {2}. Exception: {3}", service,
54 localFallbackServiceLocation, e);
55 return localFallbackServiceLocation;
58 if (nodes == null || nodes.size() == 0) {
59 logger.debug(EELFLoggerDelegate.debugLogger, "No healthy node found in the consul cluster running service " + service
60 + ". Defaulting to localhost");
61 return EcompPortalUtils.localOrDockerHost() + ":" + fallbackPortOnLocalHost;
63 String locationFromConsul;
64 ServiceHealth node = nodes.get(0);
65 locationFromConsul = node.getNode().getNode() + ":" + node.getService().getPort();
66 logger.debug(EELFLoggerDelegate.debugLogger,
67 "Found healthy service location using consul - returning location " + locationFromConsul);
69 // if locationFromConsul is null for some reason (very unlikely at
70 // this point), default to localhost
71 if (null == locationFromConsul || "".equals(locationFromConsul)) {
72 logger.debug(EELFLoggerDelegate.debugLogger,
73 "Couldn't get location from consul for service " + service + ". Defaulting to localhost");
74 return "localhost:" + fallbackPortOnLocalHost;
76 logger.debug(EELFLoggerDelegate.debugLogger, "Found service location from consul for service " + service
77 + ". Location is " + locationFromConsul);
78 return locationFromConsul;
84 public List<ServiceHealth> getAllHealthyNodes(String service) throws ConsulException {
85 Consul consul = Consul.builder().build();
86 HealthClient healthClient = consul.healthClient();
87 return healthClient.getHealthyServiceInstances(service).getResponse();
91 public List<ServiceHealth> getAllNodes(String service) {
92 Consul consul = Consul.builder().build();
93 HealthClient healthClient = consul.healthClient();
94 return healthClient.getAllServiceInstances(service).getResponse();