Commit 6e2238f2 by Fahim Farook Committed by Ryan Baxter

Update Eureka Server's service-urls upon EnvironmentChangeEvent. (#2455)

Fixes gh-2421
parent f640ffd2
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,7 +35,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; ...@@ -35,7 +35,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.actuator.HasFeatures; 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.EurekaConstants;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -67,6 +69,7 @@ import com.sun.jersey.spi.container.servlet.ServletContainer; ...@@ -67,6 +69,7 @@ import com.sun.jersey.spi.container.servlet.ServletContainer;
/** /**
* @author Gunnar Hillert * @author Gunnar Hillert
* @author Biju Kunjummen * @author Biju Kunjummen
* @author Fahim Farook
*/ */
@Configuration @Configuration
@Import(EurekaServerInitializerConfiguration.class) @Import(EurekaServerInitializerConfiguration.class)
...@@ -169,9 +172,68 @@ public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter { ...@@ -169,9 +172,68 @@ public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public PeerEurekaNodes peerEurekaNodes(PeerAwareInstanceRegistry registry, public PeerEurekaNodes peerEurekaNodes(PeerAwareInstanceRegistry registry,
ServerCodecs serverCodecs) { ServerCodecs serverCodecs) {
return new PeerEurekaNodes(registry, this.eurekaServerConfig, return new RefreshablePeerEurekaNodes(registry, this.eurekaServerConfig,
this.eurekaClientConfig, serverCodecs, this.applicationInfoManager); this.eurekaClientConfig, serverCodecs, this.applicationInfoManager);
} }
/**
* {@link PeerEurekaNodes} which updates peers when /refresh is invoked.
* Peers are updated only if
* <code>eureka.client.use-dns-for-fetching-service-urls</code> is
* <code>false</code> and one of following properties have changed.
* </p>
* <ul>
* <li><code>eureka.client.availability-zones</code></li>
* <li><code>eureka.client.region</code></li>
* <li><code>eureka.client.service-url.&lt;zone&gt;</code></li>
* </ul>
*/
static class RefreshablePeerEurekaNodes extends PeerEurekaNodes
implements ApplicationListener<EnvironmentChangeEvent> {
public RefreshablePeerEurekaNodes(
final PeerAwareInstanceRegistry registry,
final EurekaServerConfig serverConfig,
final EurekaClientConfig clientConfig,
final ServerCodecs serverCodecs,
final ApplicationInfoManager applicationInfoManager) {
super(registry, serverConfig, clientConfig, serverCodecs, applicationInfoManager);
}
@Override
public void onApplicationEvent(final EnvironmentChangeEvent event) {
if (shouldUpdate(event.getKeys())) {
updatePeerEurekaNodes(resolvePeerUrls());
}
}
/*
* Check whether specific properties have changed.
*/
protected boolean shouldUpdate(final Set<String> changedKeys) {
assert changedKeys != null;
// if eureka.client.use-dns-for-fetching-service-urls is true, then
// service-url will not be fetched from environment.
if (clientConfig.shouldUseDnsForFetchingServiceUrls()) {
return false;
}
if (changedKeys.contains("eureka.client.region")) {
return true;
}
for (final String key : changedKeys) {
// property keys are not expected to be null.
if (key.startsWith("eureka.client.service-url.") ||
key.startsWith("eureka.client.availability-zones.")) {
return true;
}
}
return false;
}
}
@Bean @Bean
public EurekaServerContext eurekaServerContext(ServerCodecs serverCodecs, public EurekaServerContext eurekaServerContext(ServerCodecs serverCodecs,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment