Make RibbonClientHttpRequestFactory disabled by default.

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