文档章节

聊聊spring cloud netflix的HystrixCommands

go4it
 go4it
发布于 06/22 23:45
字数 884
阅读 29
收藏 1

本文主要研究一下spring cloud netflix的HystrixCommands。

maven

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

这个组件对hystrix进行了封装了,2.0.0.RELEASE全面支持了Reactor的Reactive Streams。

spring-cloud-starter-netflix-hystrix/pom.xml

spring-cloud-starter-netflix-hystrix-2.0.0.RELEASE.jar!/META-INF/maven/org.springframework.cloud/spring-cloud-starter-netflix-hystrix/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	<name>Spring Cloud Starter Netflix Hystrix</name>
	<description>Spring Cloud Starter Netflix Hystrix</description>
	<url>https://projects.spring.io/spring-cloud</url>
	<organization>
		<name>Pivotal Software, Inc.</name>
		<url>https://www.spring.io</url>
	</organization>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-netflix-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-netflix-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-archaius</artifactId>
		</dependency>
		<dependency>
			<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>
		</dependency>
		<dependency>
			<groupId>com.netflix.hystrix</groupId>
			<artifactId>hystrix-javanica</artifactId>
		</dependency>
		<dependency>
			<groupId>io.reactivex</groupId>
			<artifactId>rxjava-reactive-streams</artifactId>
		</dependency>
	</dependencies>
</project>

这里要讲的HystrixCommands在spring-cloud-netflix-core这个组件里

HystrixCommands

spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/hystrix/HystrixCommands.java

/**
 * Utility class to wrap a {@see Publisher} in a {@see HystrixObservableCommand}. Good for
 * use in a Spring WebFlux application. Allows more flexibility than the @HystrixCommand
 * annotation.
 * @author Spencer Gibb
 */
public class HystrixCommands {

	public static <T> PublisherBuilder<T> from(Publisher<T> publisher) {
		return new PublisherBuilder<>(publisher);
	}

	public static class PublisherBuilder<T> {
		private final Publisher<T> publisher;
		private String commandName;
		private String groupName;
		private Publisher<T> fallback;
		private Setter setter;
		private HystrixCommandProperties.Setter commandProperties;
		private boolean eager = false;
		private Function<HystrixObservableCommand<T>, Observable<T>> toObservable;

		public PublisherBuilder(Publisher<T> publisher) {
			this.publisher = publisher;
		}

		public PublisherBuilder<T> commandName(String commandName) {
			this.commandName = commandName;
			return this;
		}

		public PublisherBuilder<T> groupName(String groupName) {
			this.groupName = groupName;
			return this;
		}

		public PublisherBuilder<T> fallback(Publisher<T> fallback) {
			this.fallback = fallback;
			return this;
		}

		public PublisherBuilder<T> setter(Setter setter) {
			this.setter = setter;
			return this;
		}

		public PublisherBuilder<T> commandProperties(
				HystrixCommandProperties.Setter commandProperties) {
			this.commandProperties = commandProperties;
			return this;
		}

		public PublisherBuilder<T> commandProperties(
				Function<HystrixCommandProperties.Setter, HystrixCommandProperties.Setter> commandProperties) {
			if (commandProperties == null) {
				throw new IllegalArgumentException(
						"commandProperties must not both be null");
			}
			return this.commandProperties(
					commandProperties.apply(HystrixCommandProperties.Setter()));
		}

		public PublisherBuilder<T> eager() {
			this.eager = true;
			return this;
		}

		public PublisherBuilder<T> toObservable(Function<HystrixObservableCommand<T>, Observable<T>> toObservable) {
			this.toObservable = toObservable;
			return this;
		}

		public Publisher<T> build() {
			if (!StringUtils.hasText(commandName) && setter == null) {
				throw new IllegalStateException("commandName and setter can not both be empty");
			}
			Setter setterToUse = getSetter();

			PublisherHystrixCommand<T> command = new PublisherHystrixCommand<>(setterToUse, this.publisher, this.fallback);

			Observable<T> observable = getObservableFunction().apply(command);

			return RxReactiveStreams.toPublisher(observable);
		}

		public Function<HystrixObservableCommand<T>, Observable<T>> getObservableFunction() {
			Function<HystrixObservableCommand<T>, Observable<T>> observableFunc;

			if (this.toObservable != null) {
				observableFunc = this.toObservable;
			} else if (this.eager) {
				observableFunc = cmd -> cmd.observe();
			} else { // apply a default onBackpressureBuffer if not eager
				observableFunc = cmd -> cmd.toObservable().onBackpressureBuffer();
			}
			return observableFunc;
		}

		public Setter getSetter() {
			Setter setterToUse;
			if (this.setter != null) {
				setterToUse = this.setter;
			} else {
				String groupNameToUse;
				if (StringUtils.hasText(this.groupName)) {
					groupNameToUse = this.groupName;
				} else {
					groupNameToUse = commandName + "group";
				}

				HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey(groupNameToUse);
				HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(this.commandName);
				HystrixCommandProperties.Setter commandProperties = this.commandProperties != null
						? this.commandProperties
						: HystrixCommandProperties.Setter();
				setterToUse = Setter.withGroupKey(groupKey).andCommandKey(commandKey)
						.andCommandPropertiesDefaults(commandProperties);
			}
			return setterToUse;
		}

		public Flux<T> toFlux() {
			return Flux.from(build());
		}

		public Mono<T> toMono() {
			return Mono.from(build());
		}

	}

	private static class PublisherHystrixCommand<T> extends HystrixObservableCommand<T> {

		private Publisher<T> publisher;
		private Publisher<T> fallback;

		protected PublisherHystrixCommand(Setter setter, Publisher<T> publisher,
				Publisher<T> fallback) {
			super(setter);
			this.publisher = publisher;
			this.fallback = fallback;
		}

		@Override
		protected Observable<T> construct() {
			return RxReactiveStreams.toObservable(publisher);
		}

		@Override
		protected Observable<T> resumeWithFallback() {
			if (this.fallback != null) {
				return RxReactiveStreams.toObservable(this.fallback);
			}
			return super.resumeWithFallback();
		}
	}
}

从类注释可以看到这个类就是为了方便webflux应用使用hystrix而设计的。

实例

    @Test
    public void testHystrixFallback() throws InterruptedException {
        Mono<String> delayMono = Mono.just("hello")
                .delayElement(Duration.ofMillis(500));
        Mono<String> result = HystrixCommands.from(delayMono)
                .commandName("demoCmd")
                .groupName("demoGroup")
                .eager()
                .commandProperties(HystrixCommandProperties.Setter()
                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
                        .withExecutionTimeoutInMilliseconds(1000)
                )
                .fallback(Mono.just("from fallback"))
                .toMono();

        System.out.println(result.block());

    }
  • HystrixCommands.from方法可以对Publisher进行hystrix包装
  • commandName用于指定hystrix的command名称
  • groupName用于指定hystrix的group名称
  • eager是默认方式,表示使用的是observe()方法,相当于hot Observable,只能消费从订阅时刻之后的数据,lazy使用的是toObservable()方法,相当于cold Observable,可以消费订阅之前的数据。
  • commandProperties用于指定command的属性,比如executionIsolationStrategy、executionTimeoutInMilliseconds
  • fallback用于指定fallback的操作

另外配置文件也可以指定默认的参数,比如

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds : 6000
      circuitBreaker:
        sleepWindowInMilliseconds: 10000
      metrics:
        rollingStats:
          timeInMilliseconds : 18000

小结

HystrixCommands就是spring cloud对netflix hystrix的包装,以方便webflux里头使用hystrix,就省得再去使用AOP技术了。

doc

© 著作权归作者所有

共有 人打赏支持
go4it
粉丝 70
博文 792
码字总数 630118
作品 0
深圳
私信 提问
聊聊spring cloud的HystrixCircuitBreakerConfiguration

序 本文主要研究一下spring cloud的HystrixCircuitBreakerConfiguration HystrixCircuitBreakerConfiguration spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/......

go4it
06/24
0
0
聊聊eureka client的shutdown

序 本文主要研究一下eureka client的shutdown EurekaRegistration spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/EurekaClientAuto......

go4it
05/04
0
0
聊聊ribbon的超时时间设置

序 本文主要研究一下ribbon的超时时间设置 配置 实例 RibbonClientConfiguration spring-cloud-netflix-ribbon-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/ribbon/Ribb......

go4it
07/20
0
0
聊聊EurekaRibbonClientConfiguration

序 本文主要研究一下EurekaRibbonClientConfiguration EurekaRibbonClientConfiguration spring-cloud-netflix-eureka-client-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netfl......

go4it
07/21
0
0
聊聊springcloud的serviceRegistryEndpoint

序 本文主要研究一下springcloud的serviceRegistryEndpoint 实例 GET /actuator/service-registry 如果eureka挂了,则返回 后台日志 POST /actuator/service-registry 在查看则返回 再去eur...

go4it
04/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

大数据教程(7.6)shell脚本定时采集日志数据到hdfs

上一篇博客博主分享了hadoop内置rpc的使用案例,本节博主将为小伙伴们分享一个在实际生产中使用的日志搜集案例。前面的文章我们有讲到过用户点击流日志分析的流程,本节就是要完成这个分析流...

em_aaron
16分钟前
0
0
wave和pcm互转

wav->pcm pcm->wav c#代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using Sys......

whoisliang
18分钟前
0
0
Win10:默认的图片打开应用,打开图片时速度明显很慢的解决办法

首先,我们随便地打开一张图片。然后,点击右上角的三个小点,最后点击弹出菜单最下面的“设置”。如下图: 在“设置”中找到下面的“人物”,把它关掉就好了。 原来,默认情况下,Win 10的图...

LivingInFHL
今天
3
0
js代码激发onchange事件,兼容谷歌火狐IE

var el = document.getElementsByName('role')[0]; el.value = '3'; var evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); el.dispatchEvent(evt);......

我退而结网
今天
4
0
mysql客户端报错:libmysqlclient_16 not defined in file libmysqlclient.so.16

报错情况: 安装完mydumper之后(上一篇文章),登陆Mysql客户端报错:version libmysqlclient_16 not defined in file libmysqlclient.so.16 with link time reference 同样:mysql的其他客...

machogyb
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部