16
16
import com .netflix .eureka .registry .AbstractInstanceRegistry ;
17
17
import com .netflix .eureka .registry .PeerAwareInstanceRegistryImpl ;
18
18
import com .netflix .eureka .resources .ServerCodecs ;
19
+ import lombok .extern .slf4j .Slf4j ;
19
20
import org .springframework .cloud .netflix .eureka .server .InstanceRegistry ;
20
21
import org .springframework .cloud .netflix .eureka .server .InstanceRegistryProperties ;
21
22
import org .springframework .context .ApplicationContext ;
23
+ import org .zowe .apiml .discovery .config .EurekaConfig ;
22
24
23
25
import java .lang .invoke .MethodHandle ;
24
26
import java .lang .invoke .MethodHandles ;
37
39
* #2659 Race condition with registration events in Eureka server
38
40
* https://github.com/spring-cloud/spring-cloud-netflix/issues/2659
39
41
*/
42
+ @ Slf4j
40
43
public class ApimlInstanceRegistry extends InstanceRegistry {
41
44
42
45
private static final String EXCEPTION_MESSAGE = "Implementation of InstanceRegistry changed, please verify fix of order sending events" ;
@@ -49,21 +52,25 @@ public class ApimlInstanceRegistry extends InstanceRegistry {
49
52
private MethodHandle register3ArgsMethodHandle ;
50
53
private MethodHandle cancelMethodHandle ;
51
54
52
- private ApplicationContext appCntx ;
55
+ private final ApplicationContext appCntx ;
56
+ private final EurekaConfig .Tuple tuple ;
53
57
54
58
public ApimlInstanceRegistry (
55
59
EurekaServerConfig serverConfig ,
56
60
EurekaClientConfig clientConfig ,
57
61
ServerCodecs serverCodecs ,
58
62
EurekaClient eurekaClient ,
59
63
InstanceRegistryProperties instanceRegistryProperties ,
60
- ApplicationContext appCntx
64
+ ApplicationContext appCntx ,
65
+ EurekaConfig .Tuple tuple
61
66
) {
67
+
62
68
super (serverConfig , clientConfig , serverCodecs , eurekaClient ,
63
69
instanceRegistryProperties .getExpectedNumberOfClientsSendingRenews (),
64
70
instanceRegistryProperties .getDefaultOpenForTrafficCount ()
65
71
);
66
72
this .appCntx = appCntx ;
73
+ this .tuple = tuple ;
67
74
init ();
68
75
}
69
76
@@ -143,6 +150,7 @@ protected int resolveInstanceLeaseDurationRewritten(final InstanceInfo info) {
143
150
144
151
@ Override
145
152
public void register (InstanceInfo info , int leaseDuration , boolean isReplication ) {
153
+ info = changeServiceId (info );
146
154
try {
147
155
register3ArgsMethodHandle .invokeWithArguments (this , info , leaseDuration , isReplication );
148
156
handleRegistrationMethod .invokeWithArguments (this , info , leaseDuration , isReplication );
@@ -156,7 +164,8 @@ public void register(InstanceInfo info, int leaseDuration, boolean isReplication
156
164
}
157
165
158
166
@ Override
159
- public void register (final InstanceInfo info , final boolean isReplication ) {
167
+ public void register (InstanceInfo info , final boolean isReplication ) {
168
+ info = changeServiceId (info );
160
169
try {
161
170
register2ArgsMethodHandle .invokeWithArguments (this , info , isReplication );
162
171
handleRegistrationMethod .invokeWithArguments (this , info , resolveInstanceLeaseDurationRewritten (info ), isReplication );
@@ -172,8 +181,9 @@ public void register(final InstanceInfo info, final boolean isReplication) {
172
181
@ Override
173
182
public boolean cancel (String appName , String serverId , boolean isReplication ) {
174
183
try {
175
- final boolean out = (boolean ) cancelMethodHandle .invokeWithArguments (this , appName , serverId , isReplication );
176
- handleCancelationMethod .invokeWithArguments (this , appName , serverId , isReplication );
184
+ String [] updatedValues = replaceValues (appName , serverId );
185
+ final boolean out = (boolean ) cancelMethodHandle .invokeWithArguments (this , updatedValues [0 ], updatedValues [1 ], isReplication );
186
+ handleCancelationMethod .invokeWithArguments (this , updatedValues [0 ], updatedValues [1 ], isReplication );
177
187
return out ;
178
188
} catch (ClassCastException | WrongMethodTypeException e ) {
179
189
throw new IllegalArgumentException (EXCEPTION_MESSAGE , e );
@@ -184,10 +194,53 @@ public boolean cancel(String appName, String serverId, boolean isReplication) {
184
194
}
185
195
}
186
196
197
+ @ Override
198
+ public boolean renew (String appName , String serverId , boolean isReplication ) {
199
+ String [] updatedValues = replaceValues (appName , serverId );
200
+ return super .renew (updatedValues [0 ], updatedValues [1 ], isReplication );
201
+ }
202
+
187
203
@ Override
188
204
public boolean statusUpdate (String appName , String instanceId , InstanceInfo .InstanceStatus newStatus , String lastDirtyTimestamp , boolean isReplication ) {
189
- boolean isUpdated = super .statusUpdate (appName , instanceId , newStatus , lastDirtyTimestamp , isReplication );
205
+ String [] updatedValues = replaceValues (appName ,instanceId );
206
+ boolean isUpdated = super .statusUpdate (updatedValues [0 ], updatedValues [1 ], newStatus , lastDirtyTimestamp , isReplication );
190
207
this .appCntx .publishEvent (new EurekaStatusUpdateEvent (this , appName , instanceId ));
191
208
return isUpdated ;
192
209
}
210
+
211
+ private String [] replaceValues (String appName , String instanceId ) {
212
+ if (tuple .isValid ()) {
213
+ String servicePrefix = tuple .getOldPrefix ();
214
+ String targetValue = tuple .getNewPrefix ();
215
+ appName = appName .replace (servicePrefix .toUpperCase (), targetValue .toUpperCase ());
216
+ instanceId = instanceId .replace (servicePrefix , targetValue );
217
+ }
218
+ return new String []{appName ,instanceId };
219
+ }
220
+
221
+ /**
222
+ * Change the service ID prefix according to the mapper before the service registers to Eureka.
223
+ * @param info the instance info
224
+ * @return instance info with the modified service ID
225
+ */
226
+ protected InstanceInfo changeServiceId (final InstanceInfo info ) {
227
+ if (tuple .isValid ()) {
228
+ String servicePrefix = tuple .getOldPrefix ();
229
+ String instanceId = info .getInstanceId ();
230
+ if (instanceId .contains (servicePrefix )) {
231
+ String appName = info .getAppName ();
232
+ String [] updatedValues = replaceValues (appName ,instanceId );
233
+ log .debug ("The instance ID of {} service has been changed to {}." , info .getAppName (), updatedValues [1 ]);
234
+ return new InstanceInfo .Builder (info )
235
+ .setInstanceId (updatedValues [1 ])
236
+ .setAppGroupName (updatedValues [0 ])
237
+ .setAppName (updatedValues [0 ])
238
+ .setVIPAddress (updatedValues [0 ])
239
+ .build ();
240
+ }
241
+ return info ;
242
+ }
243
+ return info ;
244
+ }
245
+
193
246
}
0 commit comments