sequence

This article focuses on HystrixAutoConfiguration of spring cloud

HystrixAutoConfiguration

Spring – the cloud – netflix – core – 2.0.0. RELEASE – sources. The jar! /org/springframework/cloud/netflix/hystrix/HystrixAutoConfiguration.java

/**
 * Auto configuration for Hystrix.
 *
 * @author Christian Dupuis
 * @author Dave Syer
 */
@Configuration
@ConditionalOnClass({ Hystrix.class, HealthIndicator.class })
@AutoConfigureAfter({ HealthIndicatorAutoConfiguration.class })
public class HystrixAutoConfiguration {

	@Bean
	@ConditionalOnEnabledHealthIndicator("hystrix")
	public HystrixHealthIndicator hystrixHealthIndicator() {
		return new HystrixHealthIndicator();
	}

	@Bean
	@ConditionalOnProperty(value = "management.metrics.hystrix.enabled", matchIfMissing = true)
	public HystrixMetricsBinder hystrixMetricsBinder() {
		return new HystrixMetricsBinder();
	}

	/**
	 * See original {@link org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration}
	 */
	@Configuration
	@ConditionalOnWebApplication(type = SERVLET)
	@ConditionalOnBean(HystrixCommandAspect.class) // only install the stream if enabled
	@ConditionalOnClass({ 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({ 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", HystrixWebfluxEndpoint.class); }}}Copy the code

There are several beans, one is HystrixHealthIndicator, and HystrixMetricsBinder. In addition, the servlet and WebFlux versions are distinguished for the endpoint.

HystrixHealthIndicator

Spring – the cloud – netflix – core – 2.0.0. RELEASE – sources. The jar! /org/springframework/cloud/netflix/hystrix/HystrixHealthIndicator.java

/**
 * A {@link HealthIndicator} implementation for Hystrix circuit breakers.
 * <p>
 * This default implementation will not change the system state (e.g.  <code>OK</code>) but
 * includes all open circuits by name.
 *
 * @author Christian Dupuis
 */
public class HystrixHealthIndicator extends AbstractHealthIndicator {

	private static final Status CIRCUIT_OPEN = new Status("CIRCUIT_OPEN");

	@Override
	protected void doHealthCheck(Builder builder) throws Exception {
		List<String> openCircuitBreakers = new ArrayList<>();

		// Collect all open circuit breakers from Hystrix
		for (HystrixCommandMetrics metrics : HystrixCommandMetrics.getInstances()) {
			HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory
					.getInstance(metrics.getCommandKey());
			if(circuitBreaker ! = null && circuitBreaker.isOpen()) { openCircuitBreakers.add(metrics.getCommandGroup().name() +"... ""
						+ metrics.getCommandKey().name());
			}
		}

		// If there is at least one open circuit report OUT_OF_SERVICE adding the command
		// group
		// and key name
		if(! openCircuitBreakers.isEmpty()) { builder.status(CIRCUIT_OPEN).withDetail("openCircuitBreakers",
					openCircuitBreakers);
		}
		else{ builder.up(); }}}Copy the code

Check the status of circuit breakers

HystrixMetricsBinder

Micrometer – core – 1.0.5 – sources jar! /io/micrometer/core/instrument/binder/hystrix/HystrixMetricsBinder.java

@NonNullApi
@NonNullFields
public class HystrixMetricsBinder implements MeterBinder {

    @Override
    public void bindTo(MeterRegistry registry) {
        // Keeps references of existing Hystrix plugins.
        HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
        HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
        HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
        HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();

        HystrixPlugins.reset();

        // Registers existing plugins except the new MicroMeter Strategy plugin.
        HystrixPlugins.getInstance().registerMetricsPublisher(new MicrometerMetricsPublisher(registry));
        HystrixPlugins.getInstance().registerConcurrencyStrategy(concurrencyStrategy);
        HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
        HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
        HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook); }}Copy the code

Here we export metrics to Micrometer

HystrixStreamEndpoint

Spring – the cloud – netflix – core – 2.0.0. RELEASE – sources. The jar! /org/springframework/cloud/netflix/hystrix/HystrixStreamEndpoint.java

/** * {@link org.springframework.boot.actuate.endpoint.annotation.Endpoint} to expose a Jolokia {@link * * @ HystrixMetricsStreamServlet}. The 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() {
        returnnew EndpointServlet(HystrixMetricsStreamServlet.class) .withInitParameters(this.initParameters); }}Copy the code

This is servlet-based

HystrixWebfluxEndpoint

Spring – the cloud – netflix – core – 2.0.0. RELEASE – sources. The jar! /org/springframework/cloud/netflix/hystrix/HystrixWebfluxEndpoint.java

@RestControllerEndpoint(id = "hystrix.stream")
public class HystrixWebfluxEndpoint {

    private final Flux<String> stream;

    public HystrixWebfluxEndpoint(Publisher<String> dashboardData) {
        stream = Flux.interval(Duration.ofMillis(500)).map(aLong -> "{\"type\":\"ping\"}")
                .mergeWith(dashboardData).share();
    }

    // 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() {
        returnstream; }}Copy the code

This is based on WebFlux

summary

HystrixAutoConfiguration mainly configures HystrixHealthIndicator, HystrixMetricsBinder, and HystrixEndpoint(servlet and WebFlux versions).

doc

  • 14. Circuit Breaker: Hystrix Dashboard