From 3f5bdff96ca84b2472ce48415dc9e0f693895459 Mon Sep 17 00:00:00 2001 From: Robert Bleyl Date: Fri, 21 Apr 2023 12:09:43 +0200 Subject: [PATCH] Introduce skipForcedClientInitialization property. Fixes gh-4152 --- .../eureka/EurekaInstanceConfigBean.java | 18 ++++- .../server/EurekaServerAutoConfiguration.java | 14 +++- .../EurekaServerAutoConfigurationTests.java | 72 +++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfigurationTests.java diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java index 430f53cc36..d928b67768 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java @@ -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. */ @@ -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; } @@ -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); } @@ -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 @@ -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=") diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java index 6aed5e520d..7e850337f8 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java @@ -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; @@ -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()); diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfigurationTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfigurationTests.java new file mode 100644 index 0000000000..489d5cfc51 --- /dev/null +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfigurationTests.java @@ -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(); + } +}