Commit 8375e3b4 by Dave Syer

Adapt to changes in commons that put onus on user with @LoadBalanced

parent 790ac76a
/*
* Copyright 2013-2015 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.metrics;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.ClassUtils;
/**
* @author Dave Syer
*/
public class ServoEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
if (ClassUtils.isPresent("com.netflix.servo.monitor.Monitors", null)) {
// Make spring AOP default to target class so RestTemplates can be customized
addDefaultProperty(environment, "spring.aop.proxyTargetClass", "true");
}
}
private void addDefaultProperty(ConfigurableEnvironment environment, String name,
String value) {
MutablePropertySources sources = environment.getPropertySources();
Map<String, Object> map = null;
if (sources.contains("defaultProperties")) {
PropertySource<?> source = sources.get("defaultProperties");
if (source instanceof MapPropertySource) {
map = ((MapPropertySource) source).getSource();
}
}
else {
map = new LinkedHashMap<>();
sources.addLast(new MapPropertySource("defaultProperties", map));
}
if (map != null) {
map.put(name, value);
}
}
}
...@@ -38,7 +38,7 @@ import com.netflix.servo.tag.BasicTagList; ...@@ -38,7 +38,7 @@ import com.netflix.servo.tag.BasicTagList;
@Configuration @Configuration
@ConditionalOnClass(AtlasMetricObserver.class) @ConditionalOnClass(AtlasMetricObserver.class)
@Import(ServoMetricsAutoConfiguration.class) @Import(ServoMetricsAutoConfiguration.class)
public class AtlasAutoConfiguration { public class AtlasConfiguration {
@Autowired(required = false) @Autowired(required = false)
private Collection<AtlasTagProvider> tagProviders; private Collection<AtlasTagProvider> tagProviders;
......
...@@ -29,6 +29,6 @@ import org.springframework.context.annotation.Import; ...@@ -29,6 +29,6 @@ import org.springframework.context.annotation.Import;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Inherited @Inherited
@Import(AtlasAutoConfiguration.class) @Import(AtlasConfiguration.class)
public @interface EnableAtlas { public @interface EnableAtlas {
} }
...@@ -11,3 +11,6 @@ org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration ...@@ -11,3 +11,6 @@ org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\ org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.netflix.metrics.ServoEnvironmentPostProcessor
\ No newline at end of file
/* /*
* Copyright 2013-2015 the original author or authors. * Copyright 2013-2015 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 * 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 * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * 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 * 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 * 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. * specific language governing permissions and limitations under the License.
...@@ -20,6 +20,7 @@ import org.junit.runner.RunWith; ...@@ -20,6 +20,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
...@@ -47,14 +48,16 @@ public class AtlasExporterTests { ...@@ -47,14 +48,16 @@ public class AtlasExporterTests {
@Test @Test
public void exportMetricsAtPeriodicIntervals() { public void exportMetricsAtPeriodicIntervals() {
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); MockRestServiceServer mockServer = MockRestServiceServer
.createServer(this.restTemplate);
mockServer.expect(MockRestRequestMatchers.requestTo("atlas/api/v1/publish")) mockServer.expect(MockRestRequestMatchers.requestTo("atlas/api/v1/publish"))
.andExpect(MockRestRequestMatchers.method(HttpMethod.POST)) .andExpect(MockRestRequestMatchers.method(HttpMethod.POST))
.andRespond(MockRestResponseCreators.withSuccess("{\"status\" : \"OK\"}", MediaType.APPLICATION_JSON)); .andRespond(MockRestResponseCreators.withSuccess("{\"status\" : \"OK\"}",
MediaType.APPLICATION_JSON));
DynamicCounter.increment("counterThatWillBeSentToAtlas"); DynamicCounter.increment("counterThatWillBeSentToAtlas");
atlasExporter.export(); this.atlasExporter.export();
mockServer.verify(); mockServer.verify();
} }
...@@ -64,6 +67,13 @@ public class AtlasExporterTests { ...@@ -64,6 +67,13 @@ public class AtlasExporterTests {
@Configuration @Configuration
@EnableAtlas @EnableAtlas
class AtlasExporterConfiguration { class AtlasExporterConfiguration {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean @Bean
public static PropertySourcesPlaceholderConfigurer properties() throws Exception { public static PropertySourcesPlaceholderConfigurer properties() throws Exception {
final PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer(); final PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer();
......
...@@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest; import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -47,11 +48,11 @@ import org.springframework.web.client.RestTemplate; ...@@ -47,11 +48,11 @@ 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
*/ */
...@@ -70,12 +71,13 @@ public class RibbonClientHttpRequestFactoryTests { ...@@ -70,12 +71,13 @@ public class RibbonClientHttpRequestFactoryTests {
@Test @Test
public void requestFactoryIsRibbon() { public void requestFactoryIsRibbon() {
assertTrue("wrong RequestFactory type", restTemplate.getRequestFactory() instanceof RibbonClientHttpRequestFactory); assertTrue("wrong RequestFactory type", this.restTemplate
.getRequestFactory() instanceof RibbonClientHttpRequestFactory);
} }
@Test @Test
public void vanillaRequestWorks() { public void vanillaRequestWorks() {
ResponseEntity<String> response = restTemplate.getForEntity("http://simple/", ResponseEntity<String> response = this.restTemplate.getForEntity("http://simple/",
String.class); 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());
...@@ -83,37 +85,41 @@ public class RibbonClientHttpRequestFactoryTests { ...@@ -83,37 +85,41 @@ public class RibbonClientHttpRequestFactoryTests {
@Test @Test
public void requestWithPathParamWorks() { public void requestWithPathParamWorks() {
ResponseEntity<String> response = restTemplate.getForEntity("http://simple/path/{param}", ResponseEntity<String> response = this.restTemplate
String.class, "world"); .getForEntity("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());
} }
@Test @Test
public void requestWithEncodedPathParamWorks() { public void requestWithEncodedPathParamWorks() {
ResponseEntity<String> response = restTemplate.getForEntity("http://simple/path/{param}", ResponseEntity<String> response = this.restTemplate.getForEntity(
String.class, "world & everyone else"); "http://simple/path/{param}", String.class, "world & everyone else");
assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode()); assertEquals("wrong response code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello world & everyone else", response.getBody()); assertEquals("wrong response body", "hello world & everyone else",
response.getBody());
} }
@Test @Test
public void requestWithRequestParamWorks() { public void requestWithRequestParamWorks() {
ResponseEntity<String> response = restTemplate.getForEntity("http://simple/request?param={param}", String.class, "world"); ResponseEntity<String> response = this.restTemplate.getForEntity(
"http://simple/request?param={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());
} }
@Test @Test
public void requestWithPostWorks() { public void requestWithPostWorks() {
ResponseEntity<String> response = 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 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 = 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 code", HttpStatus.OK, response.getStatusCode());
assertEquals("wrong response body", "hello empty", response.getBody()); assertEquals("wrong response body", "hello empty", response.getBody());
} }
...@@ -122,18 +128,18 @@ public class RibbonClientHttpRequestFactoryTests { ...@@ -122,18 +128,18 @@ public class RibbonClientHttpRequestFactoryTests {
@SneakyThrows @SneakyThrows
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") .header("X-Param", "world").build();
.build(); ResponseEntity<String> response = this.restTemplate.exchange(entity,
ResponseEntity<String> response = restTemplate.exchange(entity, String.class); 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 invalidHostNameError() { public void invalidHostNameError() {
exceptionRule.expect(ResourceAccessException.class); this.exceptionRule.expect(ResourceAccessException.class);
exceptionRule.expectMessage("Invalid hostname"); this.exceptionRule.expectMessage("Invalid hostname");
restTemplate.getForEntity("http://simple_bad", String.class); this.restTemplate.getForEntity("http://simple_bad", String.class);
} }
@Configuration @Configuration
...@@ -142,6 +148,12 @@ public class RibbonClientHttpRequestFactoryTests { ...@@ -142,6 +148,12 @@ public class RibbonClientHttpRequestFactoryTests {
@RibbonClient(value = "simple", configuration = SimpleRibbonClientConfiguration.class) @RibbonClient(value = "simple", configuration = SimpleRibbonClientConfiguration.class)
protected static class App { protected static class App {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@RequestMapping("/") @RequestMapping("/")
public String hi() { public String hi() {
return "hello"; return "hello";
...@@ -149,17 +161,17 @@ public class RibbonClientHttpRequestFactoryTests { ...@@ -149,17 +161,17 @@ public class RibbonClientHttpRequestFactoryTests {
@RequestMapping("/path/{param}") @RequestMapping("/path/{param}")
public String hiParam(@PathVariable("param") String param) { public String hiParam(@PathVariable("param") String param) {
return "hello "+param; return "hello " + param;
} }
@RequestMapping("/request") @RequestMapping("/request")
public String hiRequest(@RequestParam("param") String param) { public String hiRequest(@RequestParam("param") String param) {
return "hello "+param; return "hello " + param;
} }
@RequestMapping(value = "/post", method = RequestMethod.POST) @RequestMapping(value = "/post", method = RequestMethod.POST)
public String hiPost(@RequestBody String param) { public String hiPost(@RequestBody String param) {
return "hello "+param; return "hello " + param;
} }
@RequestMapping(value = "/emptypost", method = RequestMethod.POST) @RequestMapping(value = "/emptypost", method = RequestMethod.POST)
...@@ -169,7 +181,7 @@ public class RibbonClientHttpRequestFactoryTests { ...@@ -169,7 +181,7 @@ public class RibbonClientHttpRequestFactoryTests {
@RequestMapping("/header") @RequestMapping("/header")
public String hiHeader(@RequestHeader("X-Param") String param) { public String hiHeader(@RequestHeader("X-Param") String param) {
return "hello "+param; return "hello " + param;
} }
} }
} }
......
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