@HystrixCommand和@CacheResult注解使用
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(value = "demo",fallback = HelloWorldServiceFailure.class)//目标项目名,熔断后处理类 public interface HelloWorldService { @RequestMapping(value = "/hello/hello", method = RequestMethod.GET) //接口全名 和 请求方式 // Feign将方法签名中方法参数对象序列化为请求参数放到HTTP请求中的过程,是由编码器(Encoder)完成的。 // 同理,将HTTP响应数据反序列化为Java对象是由解码器(Decoder)完成的。 // 默认情况下,Feign会将标有@RequestParam注解的参数转换成字符串添加到URL中,将没有注解的参数通过Jackson转换成json放到请求体中。 // 注意,如果在@RequetMapping中的method将请求方式指定为POST,那么所有未标注解的参数将会被忽略 @HystrixCommand(commandKey = "testCommand", groupKey = "testGroup", threadPoolKey = "testThreadKey", fallbackMethod = "hiConsumerFallBack", ignoreExceptions = {NullPointerException.class}, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "101"), @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"), @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")) String sayHello();
String hiConsumerFallBack(){
return "error";
}
}
根据例子介绍参数:
1:commandKey:配置全局唯一标识服务的名称,如果不配置,则默认是@HystrixCommand注解修饰的方法名。
2:groupKey:重要 配置全局唯一标识服务分组的名。通过设置分组,Hystrix会根据组来组织和统计命令的报告、仪表盘等信息。。默认情况下,Hystrix默认按照命令组划分线程组,在创建Hystrix命令时为其指定命令组划分线程池。此外,Hystrix还提供了通过设置threadPoolKey来对线程池进行设置。最好使用threadPoolKey来控制线程池组。
3:threadPoolKey:对线程池进行设定,可以对单个服务的线程池信息进行设置,也可多个服务设置同一个threadPoolKey构成线程组。
4:fallbackMethod:@HystrixCommand注解修饰的函数的回调函数,@HystrixCommand修饰的函数必须和这个回调函数定义在同一个类中,因为定义在了同一个类中,所以fackback method可以是public/private均可。
5:commandProperties:配置该命令的一些参数,如executionIsolationStrategy配置执行隔离策略,默认是使用线程隔离,此处我们配置为THREAD,即线程池隔离。
参见:com.netflix.hystrix.HystrixCommandProperties中各个参数的定义。
| 注解 | 描述 | 属性 |
| @CacheResult | 该注解用来标记请求命令返回的结果应该被缓存,它必须与@HystrixCommand注解结合使用 | cacheKeyMethod |
| @CacheRemove | 该注解用来让请求命令的缓存失效,失效的缓存根据定义Key决定 |
commandKey,
cacheKeyMethod
|
| @CacheKey | 该注解用来在请求命令的参数上标记,使其作为缓存的Key值,如果没有标注则会使用所有参数。如果同时还是使用了@CacheResult和@CacheRemove注解的cacheKeyMethod方法指定缓存Key的生成,那么该注解将不会起作用 | value |
开启请求缓存功能:
需要用到@CacheRsult注解 源码如下:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {
String groupKey() default "";
String commandKey() default "";
String threadPoolKey() default "";
String fallbackMethod() default "";
HystrixProperty[] commandProperties() default {};
HystrixProperty[] threadPoolProperties() default {};
Class<? extends Throwable>[] ignoreExceptions() default {};
ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;
HystrixException[] raiseHystrixExceptions() default {};
String defaultFallback() default "";
}
cacheKeyMethod这个参数 指定获取checkKey的方法名,根据取到的checkKey的值来区分是否是重复请求,如果多次请求一个checkKey,只会真实请求一次,其他次则返回这个checkKey缓存的内容,
开启checkKey的好处:1.减少重复请求次数,降低以来服务的并发度。2.在同一个用户请求的上下文,相同依赖服务返回内同相同。3.缓存在run()和contruct()执行之前,减少不必要的线程开销。
在参数中使用@CachKey 指定对象中的属性为缓存Key(可以理解为同一个请求中参数相同则不重复请求)
使用@CachResult开启缓存实例
@RequestMapping(value = "/hello/hello", method = RequestMethod.GET) //接口全名 和 请求方式 // Feign将方法签名中方法参数对象序列化为请求参数放到HTTP请求中的过程,是由编码器(Encoder)完成的。 // 同理,将HTTP响应数据反序列化为Java对象是由解码器(Decoder)完成的。 // 默认情况下,Feign会将标有@RequestParam注解的参数转换成字符串添加到URL中,将没有注解的参数通过Jackson转换成json放到请求体中。 // 注意,如果在@RequetMapping中的method将请求方式指定为POST,那么所有未标注解的参数将会被忽略 @HystrixCommand(commandKey = "testCommand", groupKey = "testGroup", threadPoolKey = "testThreadKey", fallbackMethod = "hiConsumerFallBack" ) @CacheResult(cacheKeyMethod = "getUserId") String sayHello(@CacehKey("msg") String msg);
清理缓存:
使用请求缓存时如果只有读操作问题不大,但是当有写操作时则不能使用缓存会让写操作失效。
@CacheRemove 清理缓存。
@RequestMapping(value = "/hello/hello", method = RequestMethod.GET) @HystrixCommand(commandKey = "testCommand", groupKey = "testGroup", threadPoolKey = "testThreadKey",fallbackMethod = "hiConsumerFallBack")
@CacheRemove(commandKey = "getUserId")
String sayHello(@CacehKey("msg") String msg);

浙公网安备 33010602011771号