2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.portal.service;
\r
22 import java.util.List;
\r
24 import org.springframework.stereotype.Component;
\r
26 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
27 import org.openecomp.portalsdk.core.util.SystemProperties;
\r
28 import com.ecwid.consul.ConsulException;
\r
29 import com.orbitz.consul.Consul;
\r
30 import com.orbitz.consul.HealthClient;
\r
31 import com.orbitz.consul.model.health.ServiceHealth;
\r
34 public class ConsulHealthServiceImpl implements ConsulHealthService {
\r
36 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulHealthServiceImpl.class);
\r
39 public String getServiceLocation(String service){
\r
41 List<ServiceHealth> nodes = null;
\r
44 Consul consul = Consul.builder().build();
\r
45 HealthClient healthClient = consul.healthClient();
\r
46 nodes = healthClient.getHealthyServiceInstances(service).getResponse();
\r
49 //using both loggers.
\r
50 logger.debug(logger.debugLogger, " problem getting nodes for service - " + service + e.getMessage() + " - Defaulting to localhost", e);
\r
51 logger.error(logger.errorLogger, " problem getting nodes for service - " + service + e.getMessage() + " - Defaulting to localhost", e);
\r
53 return "localhost:" + SystemProperties.getProperty("microservices.widget.local.port");
\r
56 if(nodes == null || nodes.size() == 0){
\r
57 logger.debug(logger.debugLogger, "No healthy node found in the consul cluster running service " + service + ". Defaulting to localhost");
\r
58 return "localhost:" + SystemProperties.getProperty("microservices.widget.local.port");
\r
61 String locationFromConsul;
\r
62 ServiceHealth node = nodes.get(0);
\r
63 locationFromConsul = node.getNode().getNode() + ":" + node.getService().getPort();
\r
64 logger.debug(logger.debugLogger, "Found healthy service location using consul - returning location " + locationFromConsul);
\r
66 //if locationFromConsul is null for some reason (very unlikely at this point), default to localhost
\r
67 if(null == locationFromConsul || "".equals(locationFromConsul)){
\r
68 logger.debug(logger.debugLogger, "Couldn't get location from consul for service " + service + ". Defaulting to localhost");
\r
69 return "localhost:" + SystemProperties.getProperty("microservices.widget.local.port");
\r
72 logger.debug(logger.debugLogger, "Found service location from consul for service " + service + ". Location is " + locationFromConsul);
\r
73 return locationFromConsul;
\r
79 public List<ServiceHealth> getAllHealthyNodes(String service) throws ConsulException{
\r
80 Consul consul = Consul.builder().build();
\r
81 HealthClient healthClient = consul.healthClient();
\r
82 return healthClient.getHealthyServiceInstances(service).getResponse();
\r
86 public List<ServiceHealth> getAllNodes(String service){
\r
87 Consul consul = Consul.builder().build();
\r
88 HealthClient healthClient = consul.healthClient();
\r
89 return healthClient.getAllServiceInstances(service).getResponse();
\r