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.wrapper;
16 import java.net.MalformedURLException;
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
24 import org.onap.msb.sdclient.core.KeyVaulePair;
25 import org.onap.msb.sdclient.core.MicroServiceFullInfo;
26 import org.onap.msb.sdclient.core.NodeInfo;
27 import org.onap.msb.sdclient.wrapper.consul.Consul;
28 import org.onap.msb.sdclient.wrapper.consul.HealthClient;
29 import org.onap.msb.sdclient.wrapper.consul.cache.HealthCache;
30 import org.onap.msb.sdclient.wrapper.consul.model.health.Service;
31 import org.onap.msb.sdclient.wrapper.consul.model.health.ServiceHealth;
32 import org.onap.msb.sdclient.wrapper.util.JacksonJsonUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class ConsulClientApp {
38 private final Consul consul;
39 private final HealthClient healthClient;
40 // private AtomicReference<List<HealthCache>> cacheList = new
41 // AtomicReference<List<HealthCache>>(
42 // new ArrayList<HealthCache>());
45 private static final Logger LOGGER = LoggerFactory.getLogger(ConsulClientApp.class);
47 public ConsulClientApp(String ip, int port) {
50 url = new URL("http", ip, port, "");
51 } catch (MalformedURLException e1) {
52 // TODO Auto-generated catch block
53 LOGGER.error("start ConsulClientApp throw exception", e1);
54 throw new RuntimeException(e1);
56 this.consul = Consul.builder().withUrl(url).build(); // connect to Consul on localhost
57 this.healthClient = consul.healthClient();
60 public Consul getConsul() {
67 * @Title startHealthNodeListen
68 * @Description TODO(开启某个服务的node变化监听,只返回健康状态服务)
73 public HealthCache startHealthNodeListen(final String serviceName) {
74 final HealthCache healthCache = HealthCache.newCache(healthClient, serviceName, 30);
75 healthCache.addListener(new HealthCache.Listener<String, ServiceHealth>() {
77 public void notify(Map<String, ServiceHealth> newValues) {
78 // do Something with updated server map
79 LOGGER.info(serviceName + "--new node notify--");
82 if (newValues.isEmpty()) {
83 LOGGER.warn(serviceName + "--nodeList is Empty--");
84 PublishAddressWrapper.publishApigateWayList.remove(serviceName);
88 LOGGER.info(serviceName + " Node Listen stopped");
89 } catch (Exception e) {
90 LOGGER.error(serviceName + " Node Listen stop throw exception", e);
96 List<MicroServiceFullInfo> nodeAddressList = new ArrayList<MicroServiceFullInfo>();
97 for (Map.Entry<String, ServiceHealth> entry : newValues.entrySet()) {
99 MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
101 ServiceHealth value = (ServiceHealth) entry.getValue();
102 Service service = value.getService();
104 NodeInfo node = new NodeInfo();
105 node.setIp(service.getAddress());
106 node.setPort(String.valueOf(service.getPort()));
107 Set<NodeInfo> nodes = new HashSet<NodeInfo>();
109 microServiceInfo.setNodes(nodes);
112 microServiceInfo.setServiceName(serviceName);
115 List<String> tagList = service.getTags();
118 for (String tag : tagList) {
120 if (tag.startsWith("\"ns\"")) {
121 String ms_ns_json = tag.split("\"ns\":")[1];
122 Map<String, String> nsMap =
123 (Map<String, String>) JacksonJsonUtil.jsonToBean(ms_ns_json, Map.class);
125 if (nsMap.get("namespace") != null) {
126 microServiceInfo.setNamespace(nsMap.get("namespace"));
132 if (tag.startsWith("\"labels\"")) {
133 String ms_labels_json = "{" + tag.split("\"labels\":\\{")[1];
134 Map<String, String> labelMap = (Map<String, String>) JacksonJsonUtil
135 .jsonToBean(ms_labels_json, Map.class);
137 List<String> nodeLabels = new ArrayList<String>();
138 for (Map.Entry<String, String> labelEntry : labelMap.entrySet()) {
139 if ("visualRange".equals(labelEntry.getKey())) {
140 microServiceInfo.setVisualRange(labelEntry.getValue());
141 } else if ("network_plane_type".equals(labelEntry.getKey())) {
142 microServiceInfo.setNetwork_plane_type(labelEntry.getValue());
144 nodeLabels.add(labelEntry.getKey() + ":" + labelEntry.getValue());
149 microServiceInfo.setLabels(nodeLabels);
153 if (tag.startsWith("\"metadata\"")) {
154 String ms_metadata_json = "{" + tag.split("\"metadata\":\\{")[1];
155 Map<String, String> metadataMap = (Map<String, String>) JacksonJsonUtil
156 .jsonToBean(ms_metadata_json, Map.class);
158 List<KeyVaulePair> ms_metadata = new ArrayList<KeyVaulePair>();
161 for (Map.Entry<String, String> metadataEntry : metadataMap.entrySet()) {
162 KeyVaulePair keyVaulePair = new KeyVaulePair();
163 keyVaulePair.setKey(metadataEntry.getKey());
164 keyVaulePair.setValue(metadataEntry.getValue());
165 ms_metadata.add(keyVaulePair);
167 microServiceInfo.setMetadata(ms_metadata);
174 } catch (Exception e) {
175 LOGGER.error(serviceName + " read tag throw exception", e);
178 nodeAddressList.add(microServiceInfo);
181 PublishAddressWrapper.publishApigateWayList.put(serviceName, nodeAddressList);
186 LOGGER.info(serviceName + " Node Listen start");
187 // cacheList.get().add(healthCache);
190 } catch (Exception e) {
191 // TODO Auto-generated catch block
192 LOGGER.error(serviceName + " Node Listen start throw exception", e);
200 public static void main(String[] args) {
201 ConsulClientApp consulTest = new ConsulClientApp("127.0.0.1", 8500);
203 consulTest.startHealthNodeListen("apigateway");