2 * Copyright 2016 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.msb.sdclient.wrapper;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.msb.sdclient.core.KeyVaulePair;
34 import org.onap.msb.sdclient.core.MicroServiceFullInfo;
35 import org.onap.msb.sdclient.core.NodeInfo;
36 import org.onap.msb.sdclient.core.PublishAddress;
37 import org.onap.msb.sdclient.core.PublishFullAddress;
38 import org.onap.msb.sdclient.core.exception.ExtendedNotFoundException;
39 import org.onap.msb.sdclient.core.exception.UnprocessableEntityException;
40 import org.onap.msb.sdclient.wrapper.util.DiscoverUtil;
41 import org.onap.msb.sdclient.wrapper.util.RegExpTestUtil;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class PublishAddressWrapper {
47 private static PublishAddressWrapper instance = new PublishAddressWrapper();
50 private PublishAddressWrapper() {}
52 public static PublishAddressWrapper getInstance() {
56 private final String ROUTE_DEFAULT_WAY = "ip";
58 private final String ROUTE_IP = "ip";
60 private final String ROUTE_DOMAIN = "domain";
62 private final String ROUTE_DEFAULT_SUBDOMAIN = "openpalette.zte.com.cn";
64 private final String METADATA_ROUTE_WAY = "routeWay";
66 private final String METADATA_ROUTE_SUBDOMAIN = "routeSubdomain";
70 private static final Logger LOGGER = LoggerFactory.getLogger(PublishAddressWrapper.class);
72 public static volatile Map<String, List<MicroServiceFullInfo>> publishApigateWayList =
73 new HashMap<String, List<MicroServiceFullInfo>>();
75 private ConsulClientApp consulClientApp;
78 ExecutorService exec = Executors.newCachedThreadPool();
81 public void setConsulClientApp(ConsulClientApp consulClientApp) {
82 this.consulClientApp = consulClientApp;
88 * @Title getAllPublishaddress
89 * @Description TODO(get all publishaddresss list by service,rest-interface master methods)
95 * @return List<PublishFullAddress>
97 public Set<PublishFullAddress> getAllPublishaddress(String serviceName, String version,
98 String namespace, String visualRange) {
100 if ("null".equals(version)) {
105 // 1.Check input parameter format efficacy
106 checkServiceInputFormat(serviceName, version, visualRange);
110 // 2.get service Info
111 MicroServiceFullInfo serviceInfo =
112 ConsulServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version, namespace);
114 if (!DiscoverUtil.checkExist(DiscoverUtil.PUBLISH_PROTOCOL, serviceInfo.getProtocol())) {
115 throw new ExtendedNotFoundException("This service's Protocol (" + serviceInfo.getProtocol()
116 + ") is not published to apigateway");
119 if ("TCP".equals(serviceInfo.getProtocol()) || "UDP".equals(serviceInfo.getProtocol())) {
120 if (StringUtils.isBlank(serviceInfo.getPublish_port())) {
121 throw new ExtendedNotFoundException("This service's Protocol ("
122 + serviceInfo.getProtocol() + ") is not published to apigateway");
126 Set<PublishFullAddress> publishFullAddressList = new HashSet<PublishFullAddress>();
128 // 3.get in-system apigateway publish address (visualRange=1)
129 if (DiscoverUtil.checkVisualRangeIn(visualRange)) {
130 Set<PublishFullAddress> publishFullAddressInList =
131 getPublishFullAddress(namespace, DiscoverUtil.VISUAL_RANGE_IN, serviceInfo);
132 if (publishFullAddressInList != null && publishFullAddressInList.size() > 0) {
133 publishFullAddressList.addAll(publishFullAddressInList);
138 // 4.get out-system apigateway publish address (visualRange=0)
139 if (DiscoverUtil.checkVisualRangeOut(visualRange)) {
140 Set<PublishFullAddress> publishFullAddressOutList =
141 getPublishFullAddress(namespace, DiscoverUtil.VISUAL_RANGE_OUT, serviceInfo);
142 if (publishFullAddressOutList != null && publishFullAddressOutList.size() > 0) {
143 publishFullAddressList.addAll(publishFullAddressOutList);
147 if (publishFullAddressList.size() > 0) {
148 return publishFullAddressList;
151 throw new ExtendedNotFoundException("This service's publish Address is not found");
155 * @Title getApigatewayServiceInfo
156 * @Description TODO(get one apigatewayServiceInfo by namespace,rest-interface master methods)
160 * @return List<MicroServiceFullInfo>
162 public Set<MicroServiceFullInfo> getApigatewayServiceInfo(String namespace, String visualRange) {
164 if (!DiscoverUtil.checkExist(DiscoverUtil.VISUAL_RANGE_LIST, visualRange, ",")) {
165 throw new UnprocessableEntityException(
166 "get ApigatewayServiceInfo FAIL:visualRange is wrong,value range:("
167 + DiscoverUtil.VISUAL_RANGE_LIST + ")");
170 List<MicroServiceFullInfo> apigatewayList;
172 if (DiscoverUtil.checkVisualRangeIn(visualRange)) {
173 apigatewayList = getApiGateWayFromCache(DiscoverUtil.APIGATEWAY_SERVINCE, namespace);
176 apigatewayList = getApiGateWayFromCache(DiscoverUtil.ROUTER_SERVINCE, namespace);
178 if (apigatewayList != null) {
179 if (StringUtils.isNotBlank(System.getenv("ROUTER_IP"))) {
180 for (MicroServiceFullInfo routerInfo : apigatewayList) {
181 for (NodeInfo node : routerInfo.getNodes()) {
182 node.setIp(System.getenv("ROUTER_IP"));
191 if (apigatewayList == null || apigatewayList.isEmpty()) {
192 throw new ExtendedNotFoundException("This service's publish Address is not found");
195 Set<MicroServiceFullInfo> apigatewaySet= new HashSet<MicroServiceFullInfo>(apigatewayList);
196 return apigatewaySet;
206 * @Title convert2PublishFullAddress
207 * @Description TODO(convert to PublishFullAddress from MicroServiceFullInfo )
208 * @param apigatewayInfo
210 * @return List<PublishFullAddress>
212 private List<PublishFullAddress> convert2PublishFullAddress(MicroServiceFullInfo apigatewayInfo,
213 MicroServiceFullInfo serviceInfo) {
215 List<PublishFullAddress> publishFullAddressList = new ArrayList<PublishFullAddress>();
219 String routeWay = this.ROUTE_DEFAULT_WAY, routeSubdomain = this.ROUTE_DEFAULT_SUBDOMAIN;
221 List<KeyVaulePair> metadata = apigatewayInfo.getMetadata();
222 if (metadata != null) {
224 for (KeyVaulePair keyVaulePair : metadata) {
225 if (this.METADATA_ROUTE_WAY.equals(keyVaulePair.getKey())) {
226 routeWay = keyVaulePair.getValue();
228 if (this.METADATA_ROUTE_SUBDOMAIN.equals(keyVaulePair.getKey())) {
229 routeSubdomain = keyVaulePair.getValue();
234 NodeInfo apigatewayNode = (NodeInfo) apigatewayInfo.getNodes().toArray()[0];
236 String[] routeWays = StringUtils.split(routeWay, DiscoverUtil.SPLIT_LINE);
237 for (int i = 0; i < routeWays.length; i++) {
238 PublishFullAddress publishFullAddress = new PublishFullAddress();
239 // set service publish visualRange
240 publishFullAddress.setVisualRange(apigatewayInfo.getVisualRange());
241 if (this.ROUTE_IP.equals(routeWays[i])) {
242 // ----routeWay:ip-----
244 // set service publish ip
245 publishFullAddress.setIp(apigatewayNode.getIp());
246 if (DiscoverUtil.VISUAL_RANGE_OUT.equals(apigatewayInfo.getVisualRange())) {
247 if (StringUtils.isNotBlank(System.getenv("ROUTER_IP"))) {
248 publishFullAddress.setIp(System.getenv("ROUTER_IP"));
254 // set service publish url
255 publishFullAddress.setPublish_url(getPublishUrl4IP(serviceInfo));
258 if (DiscoverUtil.VISUAL_RANGE_IN.equals(apigatewayInfo.getVisualRange())) {
259 publishFullAddress.setPort(apigatewayNode.getPort());
260 publishFullAddress.setPublish_protocol("http");
261 publishFullAddressList.add(publishFullAddress);
265 String[] publishPorts =
266 StringUtils.split(serviceInfo.getPublish_port(), DiscoverUtil.SPLIT_LINE);
267 if (publishPorts.length == 2) {
268 // multiPublishPort: https|http
269 publishFullAddress.setPort(publishPorts[0]);
270 publishFullAddress.setPublish_protocol("https");
271 publishFullAddressList.add(publishFullAddress);
274 PublishFullAddress publishFullAddress2 =
275 new PublishFullAddress(publishFullAddress.getIp(), publishPorts[1],
276 publishFullAddress.getPublish_url(), publishFullAddress.getVisualRange(), "http");
277 publishFullAddressList.add(publishFullAddress2);
282 if (StringUtils.isNotBlank(serviceInfo.getPublish_port())) {
283 publishFullAddress.setPort(serviceInfo.getPublish_port());
284 publishFullAddress.setPublish_protocol("https");
286 publishFullAddress.setPort(apigatewayNode.getPort());
287 publishFullAddress.setPublish_protocol("http");
290 if ("TCP".equals(serviceInfo.getProtocol()) || "UDP".equals(serviceInfo.getProtocol())) {
291 publishFullAddress.setPublish_protocol(serviceInfo.getProtocol());
294 publishFullAddressList.add(publishFullAddress);
300 } else if (this.ROUTE_DOMAIN.equals(routeWays[i])) {
301 // ----routeWay:domain-----
303 // set service domain
304 String host = getHost4Domain(serviceInfo);
305 publishFullAddress.setDomain(host + "." + routeSubdomain);
308 if ("TCP".equals(serviceInfo.getProtocol()) || "UDP".equals(serviceInfo.getProtocol())) {
309 publishFullAddress.setPort(serviceInfo.getPublish_port());
310 publishFullAddress.setPublish_protocol(serviceInfo.getProtocol());
312 publishFullAddress.setPublish_protocol("http");
313 publishFullAddress.setPort(apigatewayNode.getPort());
316 // set service publish url
317 publishFullAddress.setPublish_url(getPublishUrl4Domain(serviceInfo));
320 publishFullAddressList.add(publishFullAddress);
327 return publishFullAddressList;
332 * @Title getPublishFullAddress
333 * @Description TODO(get PublishFullAddress List for namespace and visualRange)
337 * @return List<PublishFullAddress>
339 private Set<PublishFullAddress> getPublishFullAddress(String namespace, String visualRange,
340 MicroServiceFullInfo serviceInfo) {
342 if (DiscoverUtil.checkVisualRangeIn(visualRange)) {
343 if (!DiscoverUtil.checkVisualRangeIn(serviceInfo.getVisualRange())) {
347 if (!DiscoverUtil.checkVisualRangeOut(serviceInfo.getVisualRange())) {
353 Set<PublishFullAddress> publishFullAddressList = new HashSet<PublishFullAddress>();
354 List<MicroServiceFullInfo> apigatewayList = getApigatewayInfo4Service(namespace, visualRange);
355 if (apigatewayList != null && !apigatewayList.isEmpty()) {
356 for (MicroServiceFullInfo apigatewayInfo : apigatewayList) {
357 if (isPublish2apigateway(apigatewayInfo, serviceInfo)) {
358 publishFullAddressList.addAll(convert2PublishFullAddress(apigatewayInfo, serviceInfo));
362 return publishFullAddressList;
367 private String getHost4Domain(MicroServiceFullInfo serviceInfo) {
369 if (StringUtils.isNotBlank(serviceInfo.getHost())) {
370 host = serviceInfo.getHost();
372 if (StringUtils.isNotBlank(serviceInfo.getNamespace())) {
374 serviceInfo.getServiceName() + DiscoverUtil.SERVICENAME_LINE_NAMESPACE
375 + serviceInfo.getNamespace();
377 host = serviceInfo.getServiceName();
384 private String getPublishPort(MicroServiceFullInfo apigatewayInfo,
385 MicroServiceFullInfo serviceInfo) {
387 NodeInfo node = (NodeInfo) apigatewayInfo.getNodes().toArray()[0];
390 if ("TCP".equals(serviceInfo.getProtocol()) || "UDP".equals(serviceInfo.getProtocol())) {
391 return serviceInfo.getPublish_port();
394 if (DiscoverUtil.VISUAL_RANGE_IN.equals(apigatewayInfo.getVisualRange())) {
395 port = node.getPort();
397 if (StringUtils.isNotBlank(serviceInfo.getPublish_port())) {
398 port = serviceInfo.getPublish_port();
400 port = node.getPort();
410 private String getPublishUrl4Domain(MicroServiceFullInfo serviceInfo) {
411 String publish_url = "/";
412 if (StringUtils.isNotBlank(serviceInfo.getPath()) && !"/".equals(serviceInfo.getPath())) {
413 publish_url = serviceInfo.getPath();
415 publish_url = serviceInfo.getUrl();
420 private String getPublishUrl4IP(MicroServiceFullInfo serviceInfo) {
422 String publish_url = "/";
423 if (StringUtils.isNotBlank(serviceInfo.getPath()) && !"/".equals(serviceInfo.getPath())) {
424 publish_url = serviceInfo.getPath();
426 String versionUrl = "";
427 String serviceNameUrl = serviceInfo.getServiceName();
429 if (StringUtils.isNotBlank(serviceInfo.getVersion())) {
430 versionUrl = "/" + serviceInfo.getVersion();
432 switch (serviceInfo.getProtocol()) {
434 publish_url = "/api/" + serviceNameUrl + versionUrl;
437 publish_url = "/iui/" + serviceNameUrl;
440 publish_url = "/" + serviceNameUrl + versionUrl;
443 publish_url = "/" + serviceNameUrl + versionUrl;
446 publish_url = serviceInfo.getUrl();
449 publish_url = serviceInfo.getUrl();
458 private void checkServiceInputFormat(String serviceName, String version, String visualRange) {
459 if (StringUtils.isBlank(serviceName)) {
460 throw new UnprocessableEntityException("serviceName can't be empty");
463 if (!RegExpTestUtil.serviceNameRegExpTest(serviceName)) {
464 throw new UnprocessableEntityException("get MicroServiceInfo FAIL:ServiceName("
465 + serviceName + ") format error");
468 if (StringUtils.isNotBlank(version)) {
469 if (!RegExpTestUtil.versionRegExpTest(version)) {
470 throw new UnprocessableEntityException("version (" + version + ") is not a valid format");
474 if (!DiscoverUtil.checkVisualRangeIn(visualRange)
475 && !DiscoverUtil.checkVisualRangeOut(visualRange)) {
476 throw new UnprocessableEntityException(
477 "get ApigatewayServiceInfo FAIL:visualRange is wrong,value range:("
478 + DiscoverUtil.VISUAL_RANGE_LIST + ")");
484 * @Title getApigatewayInfo4Service
485 * @Description TODO(get apigatewayServiceInfo List by namespaces[all & service-namespace])
489 * @return List<MicroServiceFullInfo>
491 private List<MicroServiceFullInfo> getApigatewayInfo4Service(String namespace, String visualRange) {
493 String apigatewayName;
494 if (DiscoverUtil.checkVisualRangeIn(visualRange)) {
495 apigatewayName = DiscoverUtil.APIGATEWAY_SERVINCE;
497 apigatewayName = DiscoverUtil.ROUTER_SERVINCE;
501 String apigateway_ns;
502 if (StringUtils.isBlank(namespace)) {
503 apigateway_ns = DiscoverUtil.APIGATEWAY_SERVINCE_DEFAULT;
505 apigateway_ns = namespace;
508 String[] apigateway_ns_array = {DiscoverUtil.APIGATEWAY_SERVINCE_ALL, apigateway_ns};
509 List<MicroServiceFullInfo> apigatewayList4Service = new ArrayList<MicroServiceFullInfo>();
510 for (int i = 0; i < apigateway_ns_array.length; i++) {
511 List<MicroServiceFullInfo> apigatewayList =
512 getApiGateWayFromCache(apigatewayName, apigateway_ns_array[i]);
513 if (apigatewayList != null) {
514 apigatewayList4Service.addAll(apigatewayList);
518 return apigatewayList4Service;
523 private boolean isPublish2apigateway(MicroServiceFullInfo apigatewayInfo,
524 MicroServiceFullInfo serviceInfo) {
525 return isPublishByNetwork_plane_typeMatches(apigatewayInfo.getNetwork_plane_type(),
526 serviceInfo.getNetwork_plane_type())
527 && isPublishByRouteLabels(apigatewayInfo.getLabels(), serviceInfo.getLabels());
531 * Determine whether the service needs to publish to apigateway TODO: according to the
532 * service_network_plane filter conditions
538 private boolean isPublishByNetwork_plane_typeMatches(String apigateway_network_plane,
539 String service_network_plane) {
541 if (StringUtils.isBlank(apigateway_network_plane))
543 String[] routeNetwork_plane_typeArray = StringUtils.split(apigateway_network_plane, "|");
544 String[] serviceVisualRangeArray = StringUtils.split(service_network_plane, "|");
545 if (DiscoverUtil.contain(serviceVisualRangeArray, routeNetwork_plane_typeArray)) {
554 * Determine whether the service needs to publish to apigateway TODO: according to the labels
560 private boolean isPublishByRouteLabels(List<String> apigatewayLabels, List<String> serviceLabels) {
561 if (apigatewayLabels == null || apigatewayLabels.isEmpty()) {
565 Map<String, String> apigateway_labelMap = new HashMap<String, String>();
566 Map<String, String> service_labelMap = new HashMap<String, String>();
567 for (String label : apigatewayLabels) {
568 String[] labelArray = label.split(":");
569 apigateway_labelMap.put(labelArray[0], labelArray[1]);
572 for (String label : serviceLabels) {
573 String[] labelArray = label.split(":");
574 service_labelMap.put(labelArray[0], labelArray[1]);
577 for (Map.Entry<String, String> entry : apigateway_labelMap.entrySet()) {
578 String key = entry.getKey();
579 String value = entry.getValue();
581 // Multiple values match
582 String[] routeLalelsArray = StringUtils.split(value, "|");
583 if (StringUtils.isBlank(service_labelMap.get(key))) {
587 String[] serviceLabelsArray = StringUtils.split(service_labelMap.get(key), "|");
589 if (DiscoverUtil.contain(routeLalelsArray, serviceLabelsArray)) {
600 private List<MicroServiceFullInfo> getApiGateWayFromCache(String apigatewayName,
601 String apigatewayNamespace) {
602 String apigatewayConsulName =
603 apigatewayName + DiscoverUtil.SERVICENAME_LINE_NAMESPACE + apigatewayNamespace;
604 if (publishApigateWayList.get(apigatewayConsulName) == null) {
607 List<MicroServiceFullInfo> apigatewayList =
608 ConsulServiceWrapper.getInstance().getMicroServiceForNodes(apigatewayName, "v1", true,
609 "", apigatewayNamespace);
610 if (!apigatewayList.isEmpty()) {
611 consulClientApp.startHealthNodeListen(apigatewayConsulName);
612 return apigatewayList;
614 } catch (ExtendedNotFoundException e) {
615 LOGGER.warn("ApiGateWay Info not found:[serviceName]" + apigatewayName + ",[namespace]"
616 + apigatewayNamespace);
620 return publishApigateWayList.get(apigatewayConsulName);
627 public PublishAddress getPublishaddress(String serviceName, String version, String namespace,
629 if ("null".equals(version)) {
633 // 1.Check input parameter format efficacy
634 checkServiceInputFormat(serviceName, version, DiscoverUtil.VISUAL_RANGE_IN);
637 MicroServiceFullInfo microServiceFullInfo =
638 ConsulServiceWrapper.getInstance().getMicroServiceInstance(serviceName, version, namespace);
640 if (!DiscoverUtil.checkVisualRangeIn(microServiceFullInfo.getVisualRange())) {
641 throw new ExtendedNotFoundException("This service is not published internally");
644 if (!DiscoverUtil.checkExist(DiscoverUtil.PUBLISH_PROTOCOL, microServiceFullInfo.getProtocol())) {
645 throw new ExtendedNotFoundException("This service's Protocol ("
646 + microServiceFullInfo.getProtocol() + ") is not published to apigateway");
649 List<PublishAddress> publishaddress_all = new ArrayList<PublishAddress>();
650 List<PublishAddress> publishaddress_ns = new ArrayList<PublishAddress>();
652 List<MicroServiceFullInfo> apigatewayList_in_all =
653 getApiGateWayFromCache(DiscoverUtil.APIGATEWAY_SERVINCE, "all");
654 if (apigatewayList_in_all != null && !apigatewayList_in_all.isEmpty()) {
655 for (MicroServiceFullInfo apigateway : apigatewayList_in_all) {
656 if (isPublish2apigateway(apigateway, microServiceFullInfo)) {
657 publishaddress_all.add(convert2PublishAddress(apigateway, microServiceFullInfo));
664 String apigateway_ns;
665 if (StringUtils.isBlank(namespace)) {
666 apigateway_ns = DiscoverUtil.APIGATEWAY_SERVINCE_DEFAULT;
668 apigateway_ns = namespace;
671 List<MicroServiceFullInfo> apigatewayList_in_ns =
672 getApiGateWayFromCache(DiscoverUtil.APIGATEWAY_SERVINCE, apigateway_ns);
673 if (apigatewayList_in_ns != null && !apigatewayList_in_ns.isEmpty()) {
674 for (MicroServiceFullInfo apigateway : apigatewayList_in_ns) {
675 if (isPublish2apigateway(apigateway, microServiceFullInfo)) {
676 publishaddress_ns.add(convert2PublishAddress(apigateway, microServiceFullInfo));
685 if (publishaddress_ns.size() > 0) {
686 return publishaddress_ns.get(0);
687 } else if (publishaddress_all.size() > 0) {
688 return publishaddress_all.get(0);
692 throw new ExtendedNotFoundException("This service's publish address is not found");
700 // get service publish url
701 String publish_url = "/";
702 if (StringUtils.isNotBlank(microServiceFullInfo.getPath())) {
703 publish_url = microServiceFullInfo.getPath();
705 String versionUrl = "";
706 String serviceNameUrl = microServiceFullInfo.getServiceName();
709 if (StringUtils.isNotBlank(microServiceFullInfo.getVersion())) {
710 versionUrl = "/" + microServiceFullInfo.getVersion();
712 switch (microServiceFullInfo.getProtocol()) {
714 publish_url = "/api/" + serviceNameUrl + versionUrl;
717 publish_url = "/iui/" + serviceNameUrl;
720 publish_url = "/" + serviceNameUrl + versionUrl;
723 publish_url = microServiceFullInfo.getUrl();
726 publish_url = microServiceFullInfo.getUrl();
732 Future<PublishAddress> f = exec.submit(new TimeTask(namespace, publish_url));
734 return f.get(wait, TimeUnit.SECONDS);
735 } catch (InterruptedException e) {
736 // TODO Auto-generated catch block
737 LOGGER.error(e.getMessage());
738 } catch (ExecutionException e) {
739 // TODO Auto-generated catch block
740 LOGGER.error(e.getMessage());
741 } catch (TimeoutException e) {
743 // LOGGER.error(e.getMessage());
744 LOGGER.error(e.getMessage());
750 if (publishaddress_ns.size() > 0) {
751 return publishaddress_ns.get(0);
752 } else if (publishaddress_all.size() > 0) {
753 return publishaddress_all.get(0);
756 throw new ExtendedNotFoundException("This service's apigatewayInfo is not found");
761 private PublishAddress convert2PublishAddress(MicroServiceFullInfo apigatewayInfo,
762 MicroServiceFullInfo serviceInfo) {
763 PublishAddress publishAddress = new PublishAddress();
765 NodeInfo node = (NodeInfo) apigatewayInfo.getNodes().toArray()[0];
766 publishAddress.setIp(node.getIp());
768 if (DiscoverUtil.VISUAL_RANGE_IN.equals(apigatewayInfo.getVisualRange())) {
769 publishAddress.setPort(node.getPort());
771 if (StringUtils.isNotBlank(serviceInfo.getPublish_port())) {
772 publishAddress.setPort(serviceInfo.getPublish_port());
774 publishAddress.setPort(node.getPort());
779 // get service publish url
780 String publish_url = "/";
781 if (StringUtils.isNotBlank(serviceInfo.getPath())) {
782 publish_url = serviceInfo.getPath();
784 String versionUrl = "";
785 String serviceNameUrl = serviceInfo.getServiceName();
788 if (StringUtils.isNotBlank(serviceInfo.getVersion())) {
789 versionUrl = "/" + serviceInfo.getVersion();
791 switch (serviceInfo.getProtocol()) {
793 publish_url = "/api/" + serviceNameUrl + versionUrl;
796 publish_url = "/iui/" + serviceNameUrl;
799 publish_url = "/" + serviceNameUrl + versionUrl;
802 publish_url = serviceInfo.getUrl();
805 publish_url = serviceInfo.getUrl();
810 publishAddress.setPublish_url(publish_url);
813 return publishAddress;
818 public class TimeTask implements Callable<PublishAddress> {
820 private String namespace;
821 private String publish_url;
824 public PublishAddress call() throws Exception {
828 List<PublishAddress> oldAddress = getApigatewayListFromCache(namespace, publish_url);
832 // LOGGER.info("oldAddress:"+oldAddress);
833 List<PublishAddress> newAddress = getApigatewayListFromCache(namespace, publish_url);
834 if (!oldAddress.equals(newAddress)) {
835 // LOGGER.info("CHANGED:"+oldAddress+"-"+apigatewayAddress);
837 return newAddress.get(0);
843 TimeTask(String namespace, String publish_url) {
844 this.namespace = namespace;
845 this.publish_url = publish_url;
850 private List<PublishAddress> getApigatewayListFromCache(String namespace, String publish_url) {
851 List<PublishAddress> fullAddress = new ArrayList<PublishAddress>();
852 String apigatewayName4ns =
853 DiscoverUtil.APIGATEWAY_SERVINCE + DiscoverUtil.SERVICENAME_LINE_NAMESPACE + namespace;
854 if (publishApigateWayList.get(apigatewayName4ns) != null) {
855 List<MicroServiceFullInfo> publishaddress4ns = publishApigateWayList.get(apigatewayName4ns);
856 for (MicroServiceFullInfo address : publishaddress4ns) {
857 NodeInfo node = (NodeInfo) address.getNodes().toArray()[0];
858 fullAddress.add(new PublishAddress(node.getIp(), node.getPort(), publish_url));
862 if (publishApigateWayList.get(DiscoverUtil.APIGATEWAY_SERVINCE_ALL) != null) {
863 List<MicroServiceFullInfo> publishaddress4all =
864 publishApigateWayList.get(DiscoverUtil.APIGATEWAY_SERVINCE_ALL);
865 for (MicroServiceFullInfo address : publishaddress4all) {
866 NodeInfo node = (NodeInfo) address.getNodes().toArray()[0];
867 fullAddress.add(new PublishAddress(node.getIp(), node.getPort(), publish_url));