Make RibbonClientHttpRequestFactory disabled by default.

fixes gh-961
parent e87d9432
......@@ -73,7 +73,7 @@ public class RibbonAutoConfiguration {
@Configuration
@ConditionalOnClass(HttpRequest.class)
@ConditionalOnProperty(value = "ribbon.http.client.enabled", matchIfMissing = true)
@ConditionalOnProperty(value = "ribbon.http.client.enabled", matchIfMissing = false)
protected static class RibbonClientConfig {
@Autowired
......@@ -83,11 +83,12 @@ public class RibbonAutoConfiguration {
private LoadBalancerClient loadBalancerClient;
@Bean
public RestTemplateCustomizer restTemplateCustomizer() {
public RestTemplateCustomizer restTemplateCustomizer(
final RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory) {
return new RestTemplateCustomizer() {
@Override
public void customize(RestTemplate restTemplate) {
restTemplate.setRequestFactory(ribbonClientHttpRequestFactory());
restTemplate.setRequestFactory(ribbonClientHttpRequestFactory);
}
};
}
......
......@@ -74,6 +74,9 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) {
ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
Server server = getServer(loadBalancer);
if (server == null) {
throw new IllegalStateException("No instances available for " + serviceId);
}
RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server,
serviceId), serverIntrospector(serviceId).getMetadata(server));
......
......@@ -42,7 +42,7 @@ import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MetricsRestTemplateTests.App.class)
@WebIntegrationTest(value = { "spring.application.name=ribbonclienttest",
"spring.jmx.enabled=true" }, randomPort = true)
"spring.jmx.enabled=true", "ribbon.http.client.enabled=true" }, randomPort = true)
@DirtiesContext
public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTests {
......@@ -51,12 +51,14 @@ public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTest
public void requestFactoryIsRibbon() {
ClientHttpRequestFactory requestFactory = this.restTemplate.getRequestFactory();
assertThat("wrong RequestFactory type: " + requestFactory.getClass(),
requestFactory, is(instanceOf(InterceptingClientHttpRequestFactory.class)));
requestFactory,
is(instanceOf(InterceptingClientHttpRequestFactory.class)));
InterceptingClientHttpRequestFactory intercepting = (InterceptingClientHttpRequestFactory) requestFactory;
Object interceptorsField = ReflectionTestUtils.getField(intercepting,
"interceptors");
assertThat("wrong interceptors type: " + interceptorsField.getClass(), interceptorsField, is(instanceOf(List.class)));
assertThat("wrong interceptors type: " + interceptorsField.getClass(),
interceptorsField, is(instanceOf(List.class)));
@SuppressWarnings("unchecked")
List<ClientHttpRequestInterceptor> interceptors = (List<ClientHttpRequestInterceptor>) interceptorsField;
assertThat("interceptors is wrong size", interceptors, hasSize(1));
......
......@@ -49,18 +49,19 @@ import org.springframework.web.client.RestTemplate;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import lombok.SneakyThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import lombok.SneakyThrows;
/**
* @author Spencer Gibb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RibbonClientHttpRequestFactoryTests.App.class)
@WebIntegrationTest(value = { "spring.application.name=ribbonclienttest",
"spring.jmx.enabled=true", "spring.cloud.netflix.metrics.enabled=false" }, randomPort = true)
"spring.jmx.enabled=true", "spring.cloud.netflix.metrics.enabled=false",
"ribbon.http.client.enabled=true" }, randomPort = true)
@DirtiesContext
public class RibbonClientHttpRequestFactoryTests {
......@@ -73,23 +74,23 @@ public class RibbonClientHttpRequestFactoryTests {
@Test
public void requestFactoryIsRibbon() {
ClientHttpRequestFactory requestFactory = this.restTemplate
.getRequestFactory();
assertTrue("wrong RequestFactory type: " + requestFactory.getClass(), requestFactory instanceof RibbonClientHttpRequestFactory);
ClientHttpRequestFactory requestFactory = this.restTemplate.getRequestFactory();
assertTrue("wrong RequestFactory type: " + requestFactory.getClass(),
requestFactory instanceof RibbonClientHttpRequestFactory);
}
@Test
public void vanillaRequestWorks() {
ResponseEntity<String> response = this.restTemplate.getForEntity("http://simple/",
String.class);
ResponseEntity<String> response = this.restTemplate.getForEntity(
"http://simple/", String.class);
assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello", response.getBody());
}
@Test
public void requestWithPathParamWorks() {
ResponseEntity<String> response = this.restTemplate
.getForEntity("http://simple/path/{param}", String.class, "world");
ResponseEntity<String> response = this.restTemplate.getForEntity(
"http://simple/path/{param}", String.class, "world");
assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello world", response.getBody());
}
......@@ -113,16 +114,16 @@ public class RibbonClientHttpRequestFactoryTests {
@Test
public void requestWithPostWorks() {
ResponseEntity<String> response = this.restTemplate
.postForEntity("http://simple/post", "world", String.class);
ResponseEntity<String> response = this.restTemplate.postForEntity(
"http://simple/post", "world", String.class);
assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello world", response.getBody());
}
@Test
public void requestWithEmptyPostWorks() {
ResponseEntity<String> response = this.restTemplate
.postForEntity("http://simple/emptypost", "", String.class);
ResponseEntity<String> response = this.restTemplate.postForEntity(
"http://simple/emptypost", "", String.class);
assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello empty", response.getBody());
}
......@@ -132,8 +133,8 @@ public class RibbonClientHttpRequestFactoryTests {
public void requestWithHeaderWorks() {
RequestEntity<Void> entity = RequestEntity.get(new URI("http://simple/header"))
.header("X-Param", "world").build();
ResponseEntity<String> response = this.restTemplate.exchange(entity,
String.class);
ResponseEntity<String> response = this.restTemplate
.exchange(entity, String.class);
assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello world", response.getBody());
}
......
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