9
9
*/
10
10
package org .zowe .apiml .apicatalog .instance ;
11
11
12
- import org .zowe .apiml .apicatalog .discovery .DiscoveryConfigProperties ;
13
- import org .zowe .apiml .message .log .ApimlLogger ;
14
- import org .zowe .apiml .product .instance .InstanceInitializationException ;
15
- import org .zowe .apiml .product .logging .annotations .InjectApimlLogger ;
16
- import org .zowe .apiml .product .registry .ApplicationWrapper ;
17
12
import com .fasterxml .jackson .annotation .JsonInclude ;
18
13
import com .fasterxml .jackson .databind .DeserializationFeature ;
19
14
import com .fasterxml .jackson .databind .ObjectMapper ;
27
22
import org .springframework .http .converter .StringHttpMessageConverter ;
28
23
import org .springframework .stereotype .Service ;
29
24
import org .springframework .web .client .RestTemplate ;
25
+ import org .zowe .apiml .apicatalog .discovery .DiscoveryConfigProperties ;
26
+ import org .zowe .apiml .message .log .ApimlLogger ;
27
+ import org .zowe .apiml .product .instance .InstanceInitializationException ;
28
+ import org .zowe .apiml .product .logging .annotations .InjectApimlLogger ;
29
+ import org .zowe .apiml .product .registry .ApplicationWrapper ;
30
30
31
31
import javax .validation .constraints .NotBlank ;
32
32
import java .io .IOException ;
33
33
import java .nio .charset .Charset ;
34
34
import java .util .ArrayList ;
35
35
import java .util .Base64 ;
36
36
import java .util .Collections ;
37
+ import java .util .List ;
37
38
38
39
/**
39
40
* Service for instance retrieval from Eureka
@@ -72,22 +73,21 @@ public InstanceInfo getInstanceInfo(@NotBlank(message = "Service Id must be supp
72
73
return null ;
73
74
}
74
75
75
- InstanceInfo instanceInfo = null ;
76
- try {
77
- Pair <String , Pair <String , String >> requestInfo = constructServiceInfoQueryRequest (serviceId , false );
78
-
79
- // call Eureka REST endpoint to fetch single or all Instances
80
- ResponseEntity <String > response = queryDiscoveryForInstances (requestInfo );
81
- if (response .getStatusCode ().is2xxSuccessful ()) {
82
- instanceInfo = extractSingleInstanceFromApplication (serviceId , requestInfo .getLeft (), response );
76
+ List <Pair <String , Pair <String , String >>> requestInfoList = constructServiceInfoQueryRequest (serviceId , false );
77
+ // iterate over list of discovery services, return at first success
78
+ for (Pair <String , Pair <String , String >> requestInfo : requestInfoList ) {
79
+ // call Eureka REST endpoint to fetch single or all Instances
80
+ try {
81
+ ResponseEntity <String > response = queryDiscoveryForInstances (requestInfo );
82
+ if (response .getStatusCode ().is2xxSuccessful ()) {
83
+ return extractSingleInstanceFromApplication (serviceId , requestInfo .getLeft (), response );
84
+ }
85
+ } catch (Exception e ) {
86
+ log .debug ("Error getting instance info from {}, error message: {}" , requestInfo .getLeft (), e .getMessage ());
87
+ }
83
88
}
84
- } catch (Exception e ) {
85
- String msg = "An error occurred when trying to get instance info for: " + serviceId ;
86
- log .debug (msg , e .getMessage ());
87
- throw new InstanceInitializationException (msg );
88
- }
89
-
90
- return instanceInfo ;
89
+ String msg = "An error occurred when trying to get instance info for: " + serviceId ;
90
+ throw new InstanceInitializationException (msg );
91
91
}
92
92
93
93
/**
@@ -98,12 +98,17 @@ public InstanceInfo getInstanceInfo(@NotBlank(message = "Service Id must be supp
98
98
*/
99
99
public Applications getAllInstancesFromDiscovery (boolean delta ) {
100
100
101
- Pair <String , Pair <String , String >> requestInfo = constructServiceInfoQueryRequest (null , delta );
102
-
101
+ List <Pair <String , Pair <String , String >>> requestInfoList = constructServiceInfoQueryRequest (null , delta );
102
+ for (Pair <String , Pair <String , String >> requestInfo : requestInfoList ) {
103
+ try {
104
+ ResponseEntity <String > response = queryDiscoveryForInstances (requestInfo );
105
+ return extractApplications (requestInfo , response );
106
+ } catch (Exception e ) {
107
+ log .debug ("Not able to contact discovery service: " + requestInfo .getKey (), e );
108
+ }
109
+ }
103
110
// call Eureka REST endpoint to fetch single or all Instances
104
- ResponseEntity <String > response = queryDiscoveryForInstances (requestInfo );
105
-
106
- return extractApplications (requestInfo , response );
111
+ return null ;
107
112
}
108
113
109
114
/**
@@ -188,28 +193,33 @@ private InstanceInfo extractSingleInstanceFromApplication(String serviceId, Stri
188
193
* @param serviceId optional service id
189
194
* @return request information
190
195
*/
191
- private Pair <String , Pair <String , String >> constructServiceInfoQueryRequest (String serviceId , boolean getDelta ) {
192
- String discoveryServiceLocatorUrl = discoveryConfigProperties .getLocations () + APPS_ENDPOINT ;
193
- if (getDelta ) {
194
- discoveryServiceLocatorUrl += DELTA_ENDPOINT ;
195
- } else {
196
- if (serviceId != null ) {
197
- discoveryServiceLocatorUrl += serviceId .toLowerCase ();
196
+ private List <Pair <String , Pair <String , String >>> constructServiceInfoQueryRequest (String serviceId , boolean getDelta ) {
197
+ String [] discoveryServiceUrls = discoveryConfigProperties .getLocations ().split ("," );
198
+ List <Pair <String , Pair <String , String >>> discoveryPairs = new ArrayList <>(discoveryServiceUrls .length );
199
+ for (String discoveryUrl : discoveryServiceUrls ) {
200
+ String discoveryServiceLocatorUrl = discoveryUrl + APPS_ENDPOINT ;
201
+ if (getDelta ) {
202
+ discoveryServiceLocatorUrl += DELTA_ENDPOINT ;
203
+ } else {
204
+ if (serviceId != null ) {
205
+ discoveryServiceLocatorUrl += serviceId .toLowerCase ();
206
+ }
198
207
}
199
- }
200
208
201
- String eurekaUsername = discoveryConfigProperties .getEurekaUserName ();
202
- String eurekaUserPassword = discoveryConfigProperties .getEurekaUserPassword ();
209
+ String eurekaUsername = discoveryConfigProperties .getEurekaUserName ();
210
+ String eurekaUserPassword = discoveryConfigProperties .getEurekaUserPassword ();
203
211
204
- Pair <String , String > discoveryServiceCredentials = Pair .of (eurekaUsername , eurekaUserPassword );
212
+ Pair <String , String > discoveryServiceCredentials = Pair .of (eurekaUsername , eurekaUserPassword );
205
213
206
- log .debug ("Eureka credentials retrieved for user: {} {}" ,
207
- eurekaUsername ,
208
- (!eurekaUserPassword .isEmpty () ? "*******" : "NO PASSWORD" )
209
- );
214
+ log .debug ("Eureka credentials retrieved for user: {} {}" ,
215
+ eurekaUsername ,
216
+ (!eurekaUserPassword .isEmpty () ? "*******" : "NO PASSWORD" )
217
+ );
210
218
211
- log .debug ("Checking instance info from: " + discoveryServiceLocatorUrl );
212
- return Pair .of (discoveryServiceLocatorUrl , discoveryServiceCredentials );
219
+ log .debug ("Checking instance info from: " + discoveryServiceLocatorUrl );
220
+ discoveryPairs .add (Pair .of (discoveryServiceLocatorUrl , discoveryServiceCredentials ));
221
+ }
222
+ return discoveryPairs ;
213
223
}
214
224
215
225
/**
0 commit comments