From 29bee28db5328f7703ca25373c3a956ce141778a Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Thu, 29 Jan 2015 13:28:33 -0700 Subject: [PATCH] move /hystrix.stream from an endpoint to a servlet fixes gh-187 --- .../HystrixCircuitBreakerConfiguration.java | 10 +- .../hystrix/HystrixStreamEndpoint.java | 33 ------- .../hystrix/HystrixStreamEndpointTests.java | 34 ------- .../netflix/hystrix/HystrixStreamTests.java | 98 +++++++++++++++++++ 4 files changed, 103 insertions(+), 72 deletions(-) delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java delete mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java create mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamTests.java diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java index d449d97d..8c7d0465 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.java @@ -27,11 +27,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.metrics.GaugeService; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -62,14 +62,14 @@ public class HystrixCircuitBreakerConfiguration { } @Configuration - @ConditionalOnProperty(value = "hystrix.stream.endpoint.enabled", matchIfMissing = true) + @ConditionalOnProperty(value = "hystrix.stream.enabled", matchIfMissing = true) @ConditionalOnWebApplication - @ConditionalOnClass({ Endpoint.class, HystrixMetricsStreamServlet.class }) + @ConditionalOnClass(HystrixMetricsStreamServlet.class) protected static class HystrixWebConfiguration { @Bean - public HystrixStreamEndpoint hystrixStreamEndpoint() { - return new HystrixStreamEndpoint(); + public ServletRegistrationBean hystrixServet() { + return new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream"); } } diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java deleted file mode 100644 index ebd9c362..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2013-2014 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.hystrix; - -import org.springframework.cloud.netflix.endpoint.ServletWrappingEndpoint; - -import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; - -/** - * @author Spencer Gibb - */ -public class HystrixStreamEndpoint extends ServletWrappingEndpoint { - - public HystrixStreamEndpoint() { - super(HystrixMetricsStreamServlet.class, "hystrixStream", "/hystrix.stream", - false, true); - } - -} diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java deleted file mode 100644 index e03d5135..00000000 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2013-2014 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.hystrix; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * @author Dave Syer - */ -public class HystrixStreamEndpointTests { - - @Test - public void pathStartsWithSlash() { - HystrixStreamEndpoint endpoint = new HystrixStreamEndpoint(); - assertEquals("/hystrix.stream", endpoint.getPath()); - } - -} diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamTests.java new file mode 100644 index 00000000..a2d65452 --- /dev/null +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamTests.java @@ -0,0 +1,98 @@ +/* + * 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.hystrix; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.InputStream; +import java.net.URL; + +import static org.junit.Assert.*; + +/** + * @author Spencer Gibb + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HystrixStreamTests.Application.class) +@WebAppConfiguration +@IntegrationTest({ "server.port=0", "spring.application.name=hystrixstreamtest" }) +@DirtiesContext +public class HystrixStreamTests { + + @Value("${local.server.port}") + private int port = 0; + + @Test + public void hystrixStreamWorks() throws Exception { + String url = "http://localhost:" + port; + //you have to hit a Hystrix circuit breaker before the stream sends anything + ResponseEntity response = new TestRestTemplate().getForEntity(url, String.class); + assertEquals("bad response code", HttpStatus.OK, response.getStatusCode()); + + URL hystrixUrl = new URL(url + "/hystrix.stream"); + InputStream in = hystrixUrl.openStream(); + byte[] buffer = new byte[1024]; + in.read(buffer); + String contents = new String(buffer); + assertTrue(contents.contains("ping")); + in.close(); + } + + @Configuration + @EnableAutoConfiguration + @RestController + @EnableCircuitBreaker + protected static class Application { + @Autowired + Service service; + + @Bean + Service service() { + return new Service(); + } + + @RequestMapping("/") + public String hello() { + return service.hello(); + } + } + + protected static class Service { + @HystrixCommand + public String hello() { + return "Hello World"; + } + } +}