Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Eureka Ribbon Auto Config conditions.
Specifically, add `@ConditionalOnBean(EurekaClient.class)`. Fixes an issue where eureka and ribbon are on the classpath, eureka.client.enabled=false which would cause a bean not found exception. fixes gh-1511 (cherry picked from commit d1f8c03)
- Loading branch information
1 parent
35528a7
commit 111a95d
Showing
6 changed files
with
152 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...a/org/springframework/cloud/netflix/ribbon/eureka/RibbonEurekaAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2013-2016 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.ribbon.eureka; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
/** | ||
* @author Spencer Gibb | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = RibbonEurekaAutoConfigurationTests.EurekaClientDisabledApp.class, | ||
properties = { "eureka.client.enabled=false", "spring.application.name=eurekadisabledtest" }, | ||
webEnvironment = RANDOM_PORT) | ||
public class RibbonEurekaAutoConfigurationTests { | ||
|
||
@Autowired | ||
TestLoadbalancerClient testLoadbalancerClient; | ||
|
||
@Test | ||
public void contextLoads() { | ||
assertThat(testLoadbalancerClient.instanceFound).isFalse(); | ||
} | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
@EnableDiscoveryClient | ||
public static class EurekaClientDisabledApp { | ||
|
||
@Bean | ||
public TestLoadbalancerClient testLoadbalanceClient(LoadBalancerClient loadBalancerClient) { | ||
return new TestLoadbalancerClient(loadBalancerClient); | ||
} | ||
|
||
@Bean | ||
public CommandLineRunner commandLineRunner(final TestLoadbalancerClient testLoadbalancerClient) { | ||
return new CommandLineRunner() { | ||
@Override | ||
public void run(String... args) throws Exception { | ||
testLoadbalancerClient.doStuff(); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
private static class TestLoadbalancerClient { | ||
|
||
Log log = LogFactory.getLog(this.getClass()); | ||
|
||
private LoadBalancerClient loadBalancerClient; | ||
private boolean instanceFound = false; | ||
|
||
public TestLoadbalancerClient(LoadBalancerClient loadBalancerClient) { | ||
this.loadBalancerClient = loadBalancerClient; | ||
} | ||
|
||
public void doStuff() { | ||
ServiceInstance serviceInstance = loadBalancerClient.choose("http://host/doStuff"); | ||
if (serviceInstance != null) { | ||
log.info("There is a service instance, because Eureka discovery is enabled and the service is registered"); | ||
instanceFound = true; | ||
} | ||
else { | ||
log.warn("No instance found, because Eureka is disabled or there is no service matching."); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters