Skip to content

Commit

Permalink
Introduce skipForcedClientInitialization property.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Bleyl committed Apr 21, 2023
1 parent ccb0f5c commit 3f5bdff
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, Envi
*/
private boolean preferIpAddress = false;

/**
* If true the EurekaClient will not be forced to initialize when the InstanceRegistry
* bean is created.
*/
private boolean skipForcedClientInitialization;

/**
* Initial status to register with remote Eureka server.
*/
Expand Down Expand Up @@ -547,6 +553,14 @@ public void setPreferIpAddress(boolean preferIpAddress) {
this.preferIpAddress = preferIpAddress;
}

public boolean isSkipForcedClientInitialization() {
return skipForcedClientInitialization;
}

public void setSkipForcedClientInitialization(boolean skipForcedClientInitialization) {
this.skipForcedClientInitialization = skipForcedClientInitialization;
}

public InstanceStatus getInitialStatus() {
return initialStatus;
}
Expand Down Expand Up @@ -597,6 +611,7 @@ public boolean equals(Object o) {
&& Objects.equals(namespace, that.namespace) && Objects.equals(hostname, that.hostname)
&& preferIpAddress == that.preferIpAddress && Objects.equals(initialStatus, that.initialStatus)
&& Arrays.equals(defaultAddressResolutionOrder, that.defaultAddressResolutionOrder)
&& skipForcedClientInitialization == that.skipForcedClientInitialization
&& Objects.equals(environment, that.environment);
}

Expand All @@ -607,7 +622,7 @@ public int hashCode() {
leaseExpirationDurationInSeconds, virtualHostName, instanceId, secureVirtualHostName, aSGName,
metadataMap, dataCenterInfo, ipAddress, statusPageUrlPath, statusPageUrl, homePageUrlPath, homePageUrl,
healthCheckUrlPath, healthCheckUrl, secureHealthCheckUrl, namespace, hostname, preferIpAddress,
initialStatus, Arrays.hashCode(defaultAddressResolutionOrder), environment);
skipForcedClientInitialization, initialStatus, Arrays.hashCode(defaultAddressResolutionOrder), environment);
}

@Override
Expand All @@ -631,6 +646,7 @@ public String toString() {
.append("', ").append("healthCheckUrl='").append(healthCheckUrl).append("', ")
.append("secureHealthCheckUrl='").append(secureHealthCheckUrl).append("', ").append("namespace='")
.append(namespace).append("', ").append("hostname='").append(hostname).append("', ")
.append("skipForcedClientInitialization=").append(skipForcedClientInitialization).append(", ")
.append("preferIpAddress=").append(preferIpAddress).append(", ").append("initialStatus=")
.append(initialStatus).append(", ").append("defaultAddressResolutionOrder=")
.append(Arrays.toString(defaultAddressResolutionOrder)).append(", ").append("environment=")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.netflix.eureka.EurekaConstants;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
Expand Down Expand Up @@ -198,9 +199,16 @@ public Jersey3EurekaServerHttpClientFactory jersey3EurekaServerHttpClientFactory

@Bean
public PeerAwareInstanceRegistry peerAwareInstanceRegistry(ServerCodecs serverCodecs,
EurekaServerHttpClientFactory eurekaServerHttpClientFactory) {
this.eurekaClient.getApplications(); // force initialization
return new InstanceRegistry(this.eurekaServerConfig, this.eurekaClientConfig, serverCodecs, this.eurekaClient,
EurekaServerHttpClientFactory eurekaServerHttpClientFactory,
EurekaInstanceConfigBean eurekaInstanceConfigBean) {
if (!eurekaInstanceConfigBean.isSkipForcedClientInitialization()) {
this.eurekaClient.getApplications(); // force initialization
}

return new InstanceRegistry(this.eurekaServerConfig,
this.eurekaClientConfig,
serverCodecs,
this.eurekaClient,
eurekaServerHttpClientFactory,
this.instanceRegistryProperties.getExpectedNumberOfClientsSendingRenews(),
this.instanceRegistryProperties.getDefaultOpenForTrafficCount());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.springframework.cloud.netflix.eureka.server;

import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.resources.ServerCodecs;
import com.netflix.eureka.transport.EurekaServerHttpClientFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class EurekaServerAutoConfigurationTests {

@Mock
private EurekaServerConfig eurekaServerConfig;

@Mock
private EurekaClientConfig eurekaClientConfig;

@Mock
private EurekaClient eurekaClient;

@Mock
private InstanceRegistryProperties instanceRegistryProperties;

@Mock
private ServerCodecs serverCodecs;

@Mock
private EurekaServerHttpClientFactory eurekaServerHttpClientFactory;

@Mock
private EurekaInstanceConfigBean eurekaInstanceConfigBean;

@InjectMocks
private EurekaServerAutoConfiguration eurekaServerAutoConfiguration;

@BeforeEach
void setup() {
when(eurekaServerConfig.getDeltaRetentionTimerIntervalInMs())
.thenReturn(1L);
}

@Test
void shouldForceEurekaClientInit() {
eurekaServerAutoConfiguration.peerAwareInstanceRegistry(serverCodecs,
eurekaServerHttpClientFactory, eurekaInstanceConfigBean);

verify(eurekaClient).getApplications();
}

@Test
void shouldNotForceEurekaClientInit() {
when(eurekaInstanceConfigBean.isSkipForcedClientInitialization())
.thenReturn(true);

eurekaServerAutoConfiguration.peerAwareInstanceRegistry(serverCodecs,
eurekaServerHttpClientFactory, eurekaInstanceConfigBean);

verify(eurekaClient, never()).getApplications();
}
}

0 comments on commit 3f5bdff

Please sign in to comment.