Supports /hystrix.stream for webflux

fixes gh-2629
This commit is contained in:
Spencer Gibb
2018-01-09 23:35:58 -05:00
parent 31b4646882
commit 6a3c509768
9 changed files with 254 additions and 15 deletions

11
pom.xml
View File

@@ -22,7 +22,6 @@
<properties>
<bintray.package>netflix</bintray.package>
<main.basedir>${basedir}</main.basedir>
<netty.version>4.0.27.Final</netty.version>
<jackson.version>2.7.3</jackson.version>
<spring-cloud-commons.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-commons.version>
<spring-cloud-config.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-config.version>
@@ -119,16 +118,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-smile</artifactId>

View File

@@ -39,6 +39,21 @@
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-reactor-netty</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
@@ -108,6 +123,11 @@
<artifactId>hystrix-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-serialization</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>

View File

@@ -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;

View File

@@ -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<String> stream;
public HystrixWebfluxController(Publisher<String> dashboardData) {
stream = Flux.interval(Duration.ofMillis(500)).map(aLong -> "{\"type\":\"ping\"}")
.mergeWith(dashboardData).share();
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> hystrixStream() {
return stream;
}
}

View File

@@ -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<String> serializedDashboardData = HystrixDashboardStream.getInstance().observe()
.concatMap(dashboardData -> Observable.from(SerialHystrixDashboardData.toMultipleJsonStrings(dashboardData)));
Publisher<String> publisher = RxReactiveStreams.toPublisher(serializedDashboardData);
return new HystrixWebfluxController(publisher);
}
@Bean
public HasFeatures hystrixStreamFeature() {
return HasFeatures.namedFeature("Hystrix Stream Webflux", HystrixMetricsStreamServlet.class);
}
}

View File

@@ -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
org.springframework.cloud.netflix.hystrix.HystrixServletManagementContextConfiguration,\
org.springframework.cloud.netflix.hystrix.HystrixWebfluxManagementContextConfiguration

View File

@@ -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<String> 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";
}
}
}

View File

@@ -357,6 +357,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-serialization</artifactId>
<version>${hystrix.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>

View File

@@ -38,6 +38,10 @@
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-serialization</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>