Skip to content

Commit

Permalink
Moves EurekaServerConfiguration to auto-configuration
Browse files Browse the repository at this point in the history
Eureka ServerConfiguration is now auto-configuration based, instead of being explicitly specified via configuration pulled in through @EnableEurekaServer. This way specific beans can be conditionally overridden, configuration can be re-ordered.
Supports PeerEurekaNodes bean to be overridden by the user

Fixes gh-1717
  • Loading branch information
bijukunjummen authored and spencergibb committed Feb 28, 2017
1 parent 208a741 commit 99d9b17
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 8 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2017 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 All @@ -22,16 +22,22 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Import;

/**
* Annotation to activate Eureka Server related configuration {@link EurekaServerAutoConfiguration}
*
* @author Dave Syer
* @author Biju Kunjummen
*
*/

@EnableDiscoveryClient
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerConfiguration.class)
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {

}
Expand Up @@ -29,12 +29,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
Expand Down Expand Up @@ -66,13 +66,15 @@

/**
* @author Gunnar Hillert
* @author Biju Kunjummen
*/
@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@EnableDiscoveryClient
@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class })
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerConfiguration extends WebMvcConfigurerAdapter {
public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter {
/**
* List of packages containing Jersey resources required by the Eureka server
*/
Expand All @@ -98,7 +100,8 @@ public class EurekaServerConfiguration extends WebMvcConfigurerAdapter {

@Bean
public HasFeatures eurekaServerFeature() {
return HasFeatures.namedFeature("Eureka Server", EurekaServerConfiguration.class);
return HasFeatures.namedFeature("Eureka Server",
EurekaServerAutoConfiguration.class);
}

@Configuration
Expand Down Expand Up @@ -163,6 +166,7 @@ public PeerAwareInstanceRegistry peerAwareInstanceRegistry(
}

@Bean
@ConditionalOnMissingBean
public PeerEurekaNodes peerEurekaNodes(PeerAwareInstanceRegistry registry,
ServerCodecs serverCodecs) {
return new PeerEurekaNodes(registry, this.eurekaServerConfig,
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.netflix.eureka.server;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Responsible for adding in a marker bean to activate
* {@link EurekaServerAutoConfiguration}
*
* @author Biju Kunjummen
*/
@Configuration
public class EurekaServerMarkerConfiguration {

@Bean
public Marker eurekaServerMarkerBean() {
return new Marker();
}

class Marker {
}
}
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration
Expand Up @@ -106,7 +106,7 @@ public void cssParsedByLess() {
@Test
public void customCodecWorks() throws Exception {
assertThat("serverCodecs is wrong type", this.serverCodecs,
is(instanceOf(EurekaServerConfiguration.CloudServerCodecs.class)));
is(instanceOf(EurekaServerAutoConfiguration.CloudServerCodecs.class)));
CodecWrapper codec = this.serverCodecs.getFullJsonCodec();
assertThat("codec is wrong type", codec, is(instanceOf(CloudJacksonJson.class)));

Expand Down
@@ -0,0 +1,87 @@
/*
* Copyright 2013-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.springframework.cloud.netflix.eureka.server;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.cluster.PeerEurekaNodes;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import com.netflix.eureka.resources.ServerCodecs;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = EurekaCustomPeerNodesTest.Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, value = {
"spring.application.name=eureka", "server.contextPath=/context",
"management.security.enabled=false" })
public class EurekaCustomPeerNodesTest {

@Autowired
private PeerEurekaNodes peerEurekaNodes;

@Test
public void testCustomPeerNodesShouldTakePrecedenceOverDefault() {
assertTrue("PeerEurekaNodes should be the user created one",
peerEurekaNodes instanceof CustomEurekaPeerNodes);
}

@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
protected static class Application {

public static void main(String[] args) {
new SpringApplicationBuilder(ApplicationContextTests.Application.class)
.properties("spring.application.name=eureka",
"server.contextPath=/context")
.run(args);
}

@Bean
public PeerEurekaNodes myPeerEurekaNodes(PeerAwareInstanceRegistry registry,
EurekaServerConfig eurekaServerConfig,
EurekaClientConfig eurekaClientConfig, ServerCodecs serverCodecs,
ApplicationInfoManager applicationInfoManager) {
return new CustomEurekaPeerNodes(registry, eurekaServerConfig,
eurekaClientConfig, serverCodecs, applicationInfoManager);
}

}

private static class CustomEurekaPeerNodes extends PeerEurekaNodes {

public CustomEurekaPeerNodes(PeerAwareInstanceRegistry registry,
EurekaServerConfig serverConfig, EurekaClientConfig clientConfig,
ServerCodecs serverCodecs,
ApplicationInfoManager applicationInfoManager) {
super(registry, serverConfig, clientConfig, serverCodecs,
applicationInfoManager);
}
}

}

0 comments on commit 99d9b17

Please sign in to comment.