Updates to new ServletEndpoint and ControllerEndpoint

This commit is contained in:
Spencer Gibb
2018-01-29 18:56:11 -05:00
parent eb0681f9c2
commit b2ebfc0ecb
9 changed files with 131 additions and 194 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-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.
@@ -12,19 +12,38 @@
* 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.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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.context.annotation.Configuration;
import org.springframework.web.reactive.DispatcherHandler;
import com.netflix.hystrix.Hystrix;
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 static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;
import rx.Observable;
import rx.RxReactiveStreams;
/**
* Auto configuration for Hystrix.
@@ -43,4 +62,47 @@ public class HystrixAutoConfiguration {
return new HystrixHealthIndicator();
}
/**
* See original {@link org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration}
*/
@Configuration
@ConditionalOnWebApplication(type = SERVLET)
@ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled
@ConditionalOnClass({ Health.class, HystrixMetricsStreamServlet.class })
@EnableConfigurationProperties(HystrixProperties.class)
protected static class HystrixServletAutoConfiguration {
@Bean
@ConditionalOnEnabledEndpoint
public HystrixStreamEndpoint hystrixStreamEndpoint(HystrixProperties properties) {
return new HystrixStreamEndpoint(properties.getConfig());
}
@Bean
public HasFeatures hystrixStreamFeature() {
return HasFeatures.namedFeature("Hystrix Stream Servlet", HystrixMetricsStreamServlet.class);
}
}
@Configuration
@ConditionalOnWebApplication(type = REACTIVE)
@ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled
@ConditionalOnClass({ Health.class, DispatcherHandler.class })
@EnableConfigurationProperties(HystrixProperties.class)
protected static class HystrixWebfluxManagementContextConfiguration {
@Bean
@ConditionalOnEnabledEndpoint
public HystrixWebfluxEndpoint hystrixWebfluxController() {
Observable<String> serializedDashboardData = HystrixDashboardStream.getInstance().observe()
.concatMap(dashboardData -> Observable.from(SerialHystrixDashboardData.toMultipleJsonStrings(dashboardData)));
Publisher<String> publisher = RxReactiveStreams.toPublisher(serializedDashboardData);
return new HystrixWebfluxEndpoint(publisher);
}
@Bean
public HasFeatures hystrixStreamFeature() {
return HasFeatures.namedFeature("Hystrix Stream Webflux", HystrixMetricsStreamServlet.class);
}
}
}

View File

@@ -1,17 +1,18 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-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
* 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;
@@ -27,41 +28,15 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @author Spencer Gibb
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "management.hystrix")
@ConfigurationProperties(prefix = "management.endpoint.hystrix")
public class HystrixProperties {
/**
* Enable Hystrix Stream.
*/
private boolean enabled;
/**
* Path at which Hystrix Stream will be available.
*/
private String path = "/hystrix.stream";
/**
* Hystrix settings. These are traditionally set using servlet parameters. Refer to
* the documentation of Hystrix for more details.
*/
private final Map<String, String> config = new HashMap<>();
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public Map<String, String> getConfig() {
return this.config;
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2013-2017 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.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.servlet.ManagementServletContext;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.actuator.HasFeatures;
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(type = SERVLET)
@ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled
@ConditionalOnClass({ Health.class, HystrixMetricsStreamServlet.class })
@EnableConfigurationProperties(HystrixProperties.class)
class HystrixServletManagementContextConfiguration {
private final ManagementServletContext managementServletContext;
private final HystrixProperties properties;
public HystrixServletManagementContextConfiguration(
ManagementServletContext managementServletContext,
HystrixProperties properties) {
this.managementServletContext = managementServletContext;
this.properties = properties;
}
@Bean
public ServletRegistrationBean<HystrixMetricsStreamServlet> hystrixMetricsStreamServlet() {
String path = this.managementServletContext.getServletPath()
+ this.properties.getPath();
String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
ServletRegistrationBean<HystrixMetricsStreamServlet> registration = new ServletRegistrationBean<>(
new HystrixMetricsStreamServlet(), urlMapping);
registration.setInitParameters(this.properties.getConfig());
return registration;
}
@Bean
public HasFeatures hystrixStreamFeature() {
return HasFeatures.namedFeature("Hystrix Stream Servlet", HystrixMetricsStreamServlet.class);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2013-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 com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.actuate.endpoint.web.EndpointServlet;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint;
import java.util.Map;
import java.util.function.Supplier;
/**
* {@link org.springframework.boot.actuate.endpoint.annotation.Endpoint} to expose a Jolokia {@link HystrixMetricsStreamServlet}.
*
* @author Phillip Webb
* @since 2.0.0
*/
@ServletEndpoint(id = "hystrix.stream")
public class HystrixStreamEndpoint implements Supplier<EndpointServlet> {
private final Map<String, String> initParameters;
public HystrixStreamEndpoint(Map<String, String> initParameters) {
this.initParameters = initParameters;
}
@Override
public EndpointServlet get() {
return new EndpointServlet(HystrixMetricsStreamServlet.class)
.withInitParameters(this.initParameters);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2013-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.
@@ -12,35 +12,35 @@
* 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.time.Duration;
import org.reactivestreams.Publisher;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
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 {
@RestControllerEndpoint(id = "hystrix.stream")
public class HystrixWebfluxEndpoint {
private final Flux<String> stream;
public HystrixWebfluxController(Publisher<String> dashboardData) {
public HystrixWebfluxEndpoint(Publisher<String> dashboardData) {
stream = Flux.interval(Duration.ofMillis(500)).map(aLong -> "{\"type\":\"ping\"}")
.mergeWith(dashboardData).share();
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
// path needs to be empty, so it registers correct as /actuator/hystrix.stream
@GetMapping(path = "", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> hystrixStream() {
return stream;
}

View File

@@ -1,64 +0,0 @@
/*
* 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

@@ -9,7 +9,3 @@ org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\
org.springframework.cloud.netflix.hystrix.HystrixServletManagementContextConfiguration,\
org.springframework.cloud.netflix.hystrix.HystrixWebfluxManagementContextConfiguration

View File

@@ -1,17 +1,18 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-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
* 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;
@@ -63,12 +64,6 @@ public class HystrixStreamEndpointTests {
@LocalServerPort
private int port = 0;
@Test
public void pathStartsWithSlash() {
HystrixProperties properties = new HystrixProperties();
assertEquals("/hystrix.stream", properties.getPath());
}
@Test
public void hystrixStreamWorks() throws Exception {
String url = "http://localhost:" + port;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2013-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.
@@ -12,6 +12,7 @@
* 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;
@@ -23,10 +24,8 @@ 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;
@@ -55,9 +54,9 @@ import reactor.test.StepVerifier;
"spring.main.web-application-type=reactive",
"spring.application.name=hystrixstreamwebfluxtest", /*"debug=true"*/ })
@DirtiesContext
public class HystrixWebfluxControllerTests {
public class HystrixWebfluxEndpointTests {
private static final String BASE_PATH = new WebEndpointProperties().getBasePath();
private static final Log log = LogFactory.getLog(HystrixWebfluxControllerTests.class);
private static final Log log = LogFactory.getLog(HystrixWebfluxEndpointTests.class);
@LocalServerPort
private int port;