From 6a3c509768b4ac9b17d338699d6a2cf99782a7c5 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 9 Jan 2018 23:35:58 -0500 Subject: [PATCH] Supports /hystrix.stream for webflux fixes gh-2629 --- pom.xml | 11 -- spring-cloud-netflix-core/pom.xml | 20 ++++ ...ervletManagementContextConfiguration.java} | 8 +- .../hystrix/HystrixWebfluxController.java | 47 ++++++++ ...WebfluxManagementContextConfiguration.java | 64 +++++++++++ .../main/resources/META-INF/spring.factories | 3 +- .../HystrixWebfluxControllerTests.java | 101 ++++++++++++++++++ spring-cloud-netflix-dependencies/pom.xml | 11 ++ .../pom.xml | 4 + 9 files changed, 254 insertions(+), 15 deletions(-) rename spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/{HystrixManagementContextConfiguration.java => HystrixServletManagementContextConfiguration.java} (92%) create mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxController.java create mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxManagementContextConfiguration.java create mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxControllerTests.java diff --git a/pom.xml b/pom.xml index 312fdc99..e95e37b6 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,6 @@ netflix ${basedir} - 4.0.27.Final 2.7.3 2.0.0.BUILD-SNAPSHOT 2.0.0.BUILD-SNAPSHOT @@ -119,16 +118,6 @@ pom import - - io.netty - netty-codec-http - ${netty.version} - - - io.netty - netty-transport-native-epoll - ${netty.version} - com.fasterxml.jackson.dataformat jackson-dataformat-smile diff --git a/spring-cloud-netflix-core/pom.xml b/spring-cloud-netflix-core/pom.xml index 75f7670c..f21c9f6d 100644 --- a/spring-cloud-netflix-core/pom.xml +++ b/spring-cloud-netflix-core/pom.xml @@ -39,6 +39,21 @@ spring-boot-starter-web true + + org.springframework.boot + spring-boot-starter-webflux + true + + + org.springframework.boot + spring-boot-starter-reactor-netty + true + + + io.netty + netty-codec-http + true + io.projectreactor reactor-core @@ -108,6 +123,11 @@ hystrix-core true + + com.netflix.hystrix + hystrix-serialization + true + com.netflix.hystrix hystrix-metrics-event-stream diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixManagementContextConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixServletManagementContextConfiguration.java similarity index 92% rename from spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixManagementContextConfiguration.java rename to spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixServletManagementContextConfiguration.java index 35a53402..e6e7e11c 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixManagementContextConfiguration.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixServletManagementContextConfiguration.java @@ -31,22 +31,24 @@ import org.springframework.context.annotation.Bean; import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; +import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET; + /** * See original {@link org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaManagementContextConfiguration} */ @ManagementContextConfiguration @ConditionalOnProperty(value = "management.hystrix.enabled", matchIfMissing = true) -@ConditionalOnWebApplication +@ConditionalOnWebApplication(type = SERVLET) @ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled @ConditionalOnClass({ Health.class, HystrixMetricsStreamServlet.class }) @EnableConfigurationProperties(HystrixProperties.class) -class HystrixManagementContextConfiguration { +class HystrixServletManagementContextConfiguration { private final ManagementServletContext managementServletContext; private final HystrixProperties properties; - public HystrixManagementContextConfiguration( + public HystrixServletManagementContextConfiguration( ManagementServletContext managementServletContext, HystrixProperties properties) { this.managementServletContext = managementServletContext; diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxController.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxController.java new file mode 100644 index 00000000..c5d12583 --- /dev/null +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxController.java @@ -0,0 +1,47 @@ +/* + * Copyright 2014-2018 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.reactivestreams.Publisher; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import reactor.core.publisher.Flux; + +import java.time.Duration; + +/** + * @author Spencer Gibb + */ +@RestController +@RequestMapping("${management.endpoints.web.base-path:/actuator}${management.hystrix.path:/hystrix.stream}") +public class HystrixWebfluxController { + + private final Flux stream; + + public HystrixWebfluxController(Publisher dashboardData) { + stream = Flux.interval(Duration.ofMillis(500)).map(aLong -> "{\"type\":\"ping\"}") + .mergeWith(dashboardData).share(); + } + + @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE) + public Flux hystrixStream() { + return stream; + } +} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxManagementContextConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxManagementContextConfiguration.java new file mode 100644 index 00000000..d1e243d7 --- /dev/null +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxManagementContextConfiguration.java @@ -0,0 +1,64 @@ +/* + * Copyright 2014-2018 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.reactivestreams.Publisher; +import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +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.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.actuator.HasFeatures; +import org.springframework.context.annotation.Bean; +import org.springframework.web.reactive.DispatcherHandler; + +import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; +import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; +import com.netflix.hystrix.metric.consumer.HystrixDashboardStream; +import com.netflix.hystrix.serial.SerialHystrixDashboardData; + +import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.REACTIVE; + +import rx.Observable; +import rx.RxReactiveStreams; + +/** + * See original {@link org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaManagementContextConfiguration} + */ +@ManagementContextConfiguration +@ConditionalOnProperty(value = "management.hystrix.enabled", matchIfMissing = true) +@ConditionalOnWebApplication(type = REACTIVE) +@ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled +@ConditionalOnClass({ Health.class, DispatcherHandler.class }) +@EnableConfigurationProperties(HystrixProperties.class) +class HystrixWebfluxManagementContextConfiguration { + + @Bean + public HystrixWebfluxController hystrixWebfluxController() { + Observable serializedDashboardData = HystrixDashboardStream.getInstance().observe() + .concatMap(dashboardData -> Observable.from(SerialHystrixDashboardData.toMultipleJsonStrings(dashboardData))); + Publisher publisher = RxReactiveStreams.toPublisher(serializedDashboardData); + return new HystrixWebfluxController(publisher); + } + + @Bean + public HasFeatures hystrixStreamFeature() { + return HasFeatures.namedFeature("Hystrix Stream Webflux", HystrixMetricsStreamServlet.class); + } +} diff --git a/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories b/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories index 5d0df973..3f56702d 100644 --- a/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories @@ -11,4 +11,5 @@ org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\ org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\ -org.springframework.cloud.netflix.hystrix.HystrixManagementContextConfiguration \ No newline at end of file +org.springframework.cloud.netflix.hystrix.HystrixServletManagementContextConfiguration,\ +org.springframework.cloud.netflix.hystrix.HystrixWebfluxManagementContextConfiguration diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxControllerTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxControllerTests.java new file mode 100644 index 00000000..af0710fd --- /dev/null +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixWebfluxControllerTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2014-2018 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 java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.cloud.netflix.test.TestAutoConfiguration; +import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.reactive.function.client.WebClient; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +/** + * @author Dave Syer + * @author Spencer Gibb + */ +@RunWith(SpringRunner.class) +@SpringBootTest( webEnvironment = RANDOM_PORT, properties = { + "spring.main.web-application-type=reactive", + "spring.application.name=hystrixstreamwebfluxtest", /*"debug=true"*/ }) +@DirtiesContext +public class HystrixWebfluxControllerTests { + private static final String BASE_PATH = new WebEndpointProperties().getBasePath(); + private static final Log log = LogFactory.getLog(HystrixWebfluxControllerTests.class); + + @LocalServerPort + private int port; + + @Test + public void hystrixStreamWorks() { + String url = "http://localhost:" + port; + // you have to hit a Hystrix circuit breaker before the stream sends anything + WebTestClient testClient = WebTestClient.bindToServer().baseUrl(url).build(); + testClient.get().uri("/").exchange().expectStatus().isOk(); + + WebClient client = WebClient.create(url); + + Flux result = client.get().uri(BASE_PATH + "/hystrix.stream") + .accept(MediaType.TEXT_EVENT_STREAM) + .exchange() + .flatMapMany(res -> res.bodyToFlux(Map.class)) + .take(5) + .filter(map -> "HystrixCommand".equals(map.get("type"))) + .map(map -> (String)map.get("type")); + + StepVerifier.create(result) + .expectNext("HystrixCommand") + .thenCancel() + .verify(); + } + + @RestController + @EnableCircuitBreaker + @EnableAutoConfiguration(exclude = TestAutoConfiguration.class, + excludeName = {"org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration", + "org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration"}) + @SpringBootConfiguration + protected static class Config { + @HystrixCommand + @RequestMapping("/") + public String hi() { + return "hi"; + } + } +} diff --git a/spring-cloud-netflix-dependencies/pom.xml b/spring-cloud-netflix-dependencies/pom.xml index 4a62ae9a..c69c145c 100644 --- a/spring-cloud-netflix-dependencies/pom.xml +++ b/spring-cloud-netflix-dependencies/pom.xml @@ -357,6 +357,17 @@ + + com.netflix.hystrix + hystrix-serialization + ${hystrix.version} + + + com.google.code.findbugs + annotations + + + com.netflix.hystrix hystrix-metrics-event-stream diff --git a/spring-cloud-starter-netflix/spring-cloud-starter-netflix-hystrix/pom.xml b/spring-cloud-starter-netflix/spring-cloud-starter-netflix-hystrix/pom.xml index c4eeeb1b..e063c85d 100644 --- a/spring-cloud-starter-netflix/spring-cloud-starter-netflix-hystrix/pom.xml +++ b/spring-cloud-starter-netflix/spring-cloud-starter-netflix-hystrix/pom.xml @@ -38,6 +38,10 @@ com.netflix.hystrix hystrix-core + + com.netflix.hystrix + hystrix-serialization + com.netflix.hystrix hystrix-metrics-event-stream