Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve EurekaConfigServerInstanceProvider #4267

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,9 @@
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.http.HttpStatus;

/**
* @author Tang Xiong
*/
public class EurekaConfigServerInstanceProvider {

private final Log log;
Expand All @@ -53,7 +56,11 @@ public List<ServiceInstance> getInstances(String serviceId) {
if (log.isDebugEnabled()) {
log.debug("eurekaConfigServerInstanceProvider finding instances for " + serviceId);
}
EurekaHttpResponse<Applications> response = client.getApplications(config.getRegion());
String remoteRegionsStr = config.fetchRegistryForRemoteRegions();
String[] remoteRegions = remoteRegionsStr == null ? null : remoteRegionsStr.split(",");
EurekaHttpResponse<Applications> response = config.getRegistryRefreshSingleVipAddress() == null
? client.getApplications(remoteRegions)
: client.getVip(config.getRegistryRefreshSingleVipAddress(), remoteRegions);
List<ServiceInstance> instances = new ArrayList<>();
if (!isSuccessful(response) || response.getEntity() == null) {
return instances;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,6 +51,7 @@

/**
* @author Spencer Gibb
* @author Tang Xiong
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("spring-webflux-*")
Expand Down Expand Up @@ -163,7 +164,34 @@ public void eurekaConfigServerInstanceProviderCalled() {
"eureka.client.enabled=true",
"spring.main.sources=" + TestConfigDiscoveryBootstrapConfiguration.class.getName(),
"logging.level.org.springframework.cloud.netflix.eureka.config=DEBUG")
.run();
.run().close();
assertThat(output).contains("eurekaConfigServerInstanceProvider finding instances for configserver")
Puppy4C marked this conversation as resolved.
Show resolved Hide resolved
.contains("eurekaConfigServerInstanceProvider found 1 instance(s) for configserver");
}

@Test
public void eurekaConfigServerInstanceProviderCalledWithRemoteRegions() {
TomcatURLStreamHandlerFactory.disable();
new SpringApplicationBuilder(TestConfigDiscoveryConfiguration.class)
.properties("spring.config.use-legacy-processing=true", "spring.cloud.config.discovery.enabled=true",
"eureka.client.enabled=true", "eureka.client.fetchRemoteRegionsRegistry=us-east-1,us-east-2",
"spring.main.sources=" + TestConfigDiscoveryBootstrapConfiguration.class.getName(),
"logging.level.org.springframework.cloud.netflix.eureka.config=DEBUG")
.run().close();
assertThat(output).contains("eurekaConfigServerInstanceProvider finding instances for configserver")
.contains("eurekaConfigServerInstanceProvider found 1 instance(s) for configserver");
}

@Test
public void eurekaConfigServerInstanceProviderCalledWithVipAddress() {
TomcatURLStreamHandlerFactory.disable();
new SpringApplicationBuilder(TestConfigDiscoveryConfiguration.class)
.properties("spring.config.use-legacy-processing=true", "spring.cloud.config.discovery.enabled=true",
"eureka.client.enabled=true", "eureka.client.registryRefreshSingleVipAddress=vip1",
"eureka.client.fetchRemoteRegionsRegistry=us-east-1,us-east-2",
"spring.main.sources=" + TestConfigDiscoveryBootstrapConfiguration.class.getName(),
"logging.level.org.springframework.cloud.netflix.eureka.config=DEBUG")
.run().close();
assertThat(output).contains("eurekaConfigServerInstanceProvider finding instances for configserver")
.contains("eurekaConfigServerInstanceProvider found 1 instance(s) for configserver");
}
Expand Down Expand Up @@ -208,7 +236,9 @@ public EurekaHttpClient mockEurekaHttpClient() {
when(response.getEntity()).thenReturn(applications);

EurekaHttpClient client = mock(EurekaHttpClient.class);
when(client.getApplications("us-east-1")).thenReturn(response);
when(client.getApplications("us-east-1", "us-east-2")).thenReturn(response);
when(client.getApplications((String) null)).thenReturn(response);
when(client.getVip("vip1", "us-east-1", "us-east-2")).thenReturn(response);
return client;
}

Expand Down