Spring Cloud @HystrixCommand和@CacheResult注解使用,参数配置
使用Spring Cloud时绕不开Hystrix,他帮助微服务实现断路器功能。该框架的目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备服务降级,服务熔断,线程和信号隔离,请求缓存,请求合并以及服务监控等强大功能。
关于Hystrix的介绍请参见:http://www.sohu.com/a/131568369_494947,本文部分介绍引自此文,建议先阅读此文了解Hystrix的使用场景和线程池概念。
@HystrixCommand的基础介绍请参见:http://blog.csdn.net/forezp/article/details/69934399,介绍的很详细,我这里主要说一说这个注解的各个参数的使用。
查看com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand的源代码如下:
/**
* Copyright 2012 Netflix, Inc.
*
* 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 com.netflix.hystrix.contrib.javanica.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* This annotation used to specify some methods which should be processes as hystrix commands.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand { /**
* The command group key is used for grouping together commands such as for reporting,
* alerting, dashboards or team/library ownership.
* <p/>
* default => the runtime class name of annotated method
*
* @return group key
*/
String groupKey() default ""; /**
* Hystrix command key.
* <p/>
* default => the name of annotated method. for example:
* <code>
* ...
* @HystrixCommand
* public User getUserById(...)
* ...
* the command name will be: 'getUserById'
* </code>
*
* @return command key
*/
String commandKey() default ""; /**
* The thread-pool key is used to represent a
* HystrixThreadPool for monitoring, metrics publishing, caching and other such uses.
*
* @return thread pool key
*/
String threadPoolKey() default ""; /**
* Specifies a method to process fallback logic.
* A fallback method should be defined in the same class where is HystrixCommand.
* Also a fallback method should have same signature to a method which was invoked as hystrix command.
* for example:
* <code>
* @HystrixCommand(fallbackMethod = "getByIdFallback")
* public String getById(String id) {...}
*
* private String getByIdFallback(String id) {...}
* </code>
* Also a fallback method can be annotated with {@link HystrixCommand}
* <p/>
* default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()}
*
* @return method name
*/
String fallbackMethod() default ""; /**
* Specifies command properties.
*
* @return command properties
*/
HystrixProperty[] commandProperties() default {}; /**
* Specifies thread pool properties.
*
* @return thread pool properties
*/
HystrixProperty[] threadPoolProperties() default {}; /**
* Defines exceptions which should be ignored.
* Optionally these can be wrapped in HystrixRuntimeException if raiseHystrixExceptions contains RUNTIME_EXCEPTION.
*
* @return exceptions to ignore
*/
Class<? extends Throwable>[] ignoreExceptions() default {}; /**
* Specifies the mode that should be used to execute hystrix observable command.
* For more information see {@link ObservableExecutionMode}.
*
* @return observable execution mode
*/
ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER; /**
* When includes RUNTIME_EXCEPTION, any exceptions that are not ignored are wrapped in HystrixRuntimeException.
*
* @return exceptions to wrap
*/
HystrixException[] raiseHystrixExceptions() default {}; /**
* Specifies default fallback method for the command. If both {@link #fallbackMethod} and {@link #defaultFallback}
* methods are specified then specific one is used.
* note: default fallback method cannot have parameters, return type should be compatible with command return type.
*
* @return the name of default fallback method
*/
String defaultFallback() default "";
}
让我们来看一下这个注解的简单应用:
package com.example.demo.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class ConsumerService { @Autowired
private RestTemplate restTemplate; @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")
}
)
public String hiConsumer(String id) { //SERVICE_HI是服务端的spring.application.name,并且大写,hi为服务端提供的接口
return restTemplate.getForEntity("http://SERVICE_HI/hi", String.class).getBody();
} public String hiConsumerFallBack(String id, Throwable e) {
return "This is a error";
} }
让我们来逐个介绍下@HystrixCommand注解的各个参数:
1:commandKey:配置全局唯一标识服务的名称,比如,库存系统有一个获取库存服务,那么就可以为这个服务起一个名字来唯一识别该服务,如果不配置,则默认是@HystrixCommand注解修饰的函数的函数名。
2:groupKey:一个比较重要的注解,配置全局唯一标识服务分组的名称,比如,库存系统就是一个服务分组。通过设置分组,Hystrix会根据组来组织和统计命令的告、仪表盘等信息。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
|
/**
* Copyright 2015 Netflix, Inc.
*
* 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 com.netflix.hystrix.contrib.javanica.cache.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* Marks a methods that results should be cached for a Hystrix command.
* This annotation must be used in conjunction with {@link com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand} annotation.
*
* @author dmgcodevil
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CacheResult { /**
* Method name to be used to get a key for request caching.
* The command and cache key method should be placed in the same class and have same method signature except
* cache key method return type, that should be <code>String</code>.
* <p/>
* cacheKeyMethod has higher priority than an arguments of a method, that means what actual arguments
* of a method that annotated with {@link CacheResult} will not be used to generate cache key, instead specified
* cacheKeyMethod fully assigns to itself responsibility for cache key generation.
* By default this returns empty string which means "do not use cache method".
*
* @return method name or empty string
*/
String cacheKeyMethod() default "";
}
- 减少重复的请求数,降低依赖服务的并发度;
- 在同一个用户请求的上下文,想同依赖服务的返回数据始终保持一致。
- 请求缓存在run()和contruct()执行之前生效,所以可以有效减少不必要的线程开销;
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.example.demo.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class CacheResultDemo { @Autowired
private RestTemplate restTemplate; @CacheResult(cacheKeyMethod = "getUserId")
@HystrixCommand(fallbackMethod = "hiConsumerFallBack")
public User hiConsumer(String id) { //SERVICE_HI是服务端的spring.application.name,并且大写,hi为服务端提供的接口
return restTemplate.getForEntity("http://SERVICE_HI/hi", User.class).getBody();
} public String hiConsumerFallBack(String id, Throwable e) {
return "This is a error";
} public String getUserId(String id) {
return id;
} }
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
importcom.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.example.demo.entity.User;
importcom.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class CacheKeyDemo {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiConsumerFallBack")
public User hiConsumer(@CacheKey("id") String id) { //SERVICE_HI是服务端的spring.application.name,并且大写,hi为服务端提供的接口
return restTemplate.getForEntity("http://SERVICE_HI/hi", User.class).getBody();
} public String hiConsumerFallBack(String id, Throwable e) {
return "This is a error";
}
}
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.example.demo.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class CacheKeyDemo2 { @Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiConsumerFallBack")
public User hiConsumer(@CacheKey("id") User user) { //SERVICE_HI是服务端的spring.application.name,并且大写,hi为服务端提供的接口
return restTemplate.getForEntity("http://SERVICE_HI/hi", User.class, user.getId()).getBody();
} public String hiConsumerFallBack(String id, Throwable e) {
return "This is a error";
} }
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
importcom.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
importcom.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.example.demo.entity.User;
importcom.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class CacheRemoveDemo {
@Autowired
private RestTemplate restTemplate;
@CacheRemove(commandKey = "getUserId")
@HystrixCommand(fallbackMethod = "hiConsumerFallBack")
public void update(@CacheKey("id") User user) { //SERVICE_HI是服务端的spring.application.name,并且大写,hi为服务端提供的接口
restTemplate.postForObject("http://SERVICE_HI/hi", user, User.class);
return;
} public String hiConsumerFallBack(String id, Throwable e) {
return "This is a error";
} public String getUserId(String id) {
return id;
}
}
Spring Cloud @HystrixCommand和@CacheResult注解使用,参数配置的更多相关文章
- spring cloud网关通过Zuul RateLimit 限流配置
目录 引入依赖 配置信息 RateLimit源码简单分析 RateLimit详细的配置信息解读 在平常项目中为了防止一些没有token访问的API被大量无限的调用,需要对一些服务进行API限流.就好比 ...
- Spring Cloud Config整合Spring Cloud Kubernetes,在k8s上管理配置
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Kubernetes有专门的ConfigMap和Secret来管理配置,但它也有一些局限性,所以还是希望通过Spring C ...
- spring cloud: zuul(二): zuul的serviceId/service-id配置(微网关)
spring cloud: zuul(二): zuul的serviceId/service-id配置(微网关) zuul: routes: #路由配置表示 myroute1: #路由名一 path: ...
- Spring Cloud gateway 七 Sentinel 注解方式使用
Sentinel 注解支持 @SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项. @SentinelResource 注解包含以下属性: value:资 ...
- Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录
背景 :在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnectio ...
- spring cloud Feign 使用 @RequestLine 注解遇到的问题
package com.itmuch.cloud; import org.springframework.cloud.netflix.feign.FeignClient; import com.itm ...
- 【Spring Cloud】Spring Cloud之自定义@SpringCloudProfile注解实现@Profile注解的功能
一.为什么会想到定义@SpringCloudProfile这样的注解 首页提一下@Profile注解:它主要用与Spring Boot多环境配置中,指定某个类只在指定环境中生效,比如swagger的配 ...
- Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh
国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心. svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依 ...
- Spring Cloud Eureka(四):Eureka 配置参数说明
Eureka Client 配置项(eureka.client.*) org.springframework.cloud.netflix.eureka.EurekaClientConfigBean 参 ...
随机推荐
- 嵌入式C快速翻转一个任何类型的数的二进制位
unsigned char reverse_bits(unsigned char value) { unsigned char answer , i ; answer = 0 ; for(i = 1 ...
- 数据挖掘进阶之序列模式分析算法GSP的实现
序列模式分析算法GSP的实现 一.算法简介 序列模式定义:给定一个由不同序列组成的集合,其中,每个序列由不同的元素按顺序有序排列,每个元素由不同项目组成,同时给定一个用户指定的最小支持度阈值,序列模式 ...
- C语言之实现随机数产生算法
随机数,也就是在不同的时刻产生不同的数值.在UNIX操作系统和window的操作系统上,我们知道有一个函数rand,它就是用来产生随机数的函数API接口,那么它的原理如何实现? 如果约定a1=f(se ...
- LeetCode之旅(18)-Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode(40)-Merge Sorted Array
听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...
- ruby和linux shell共同编程的示例
有了shell为毛还要ruby呢?话不能这么说,有些小功能用ruby还是很方便的,比如说字符串的反转再加1功能用shell来写就比较麻烦.str="123456",我们定义一个反转 ...
- ABAP Open SQL 分页查询
分页查询是一个常见需求,特别是在web相关的开发当中. 让人意外的是,google搜索abap paging query,查到的结果似乎都指出需要使用native SQL来实现相关功能:使用百度搜索 ...
- search a 2D matrix(在二维数组中搜索一个元素)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 使用JConsole以及VisualVM进行jvm程序的监控,排错,调优
这里只是做一个备份,便于以后继续. 添加两个感觉好的链接吧: http://www.linuxidc.com/Linux/2015-02/113420.htm http://blog.csdn.net ...
- C++string函数之strcpy_s
strcpy_s和strcpy()函数的功能几乎是一样的.strcpy函数,就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它只能假定缓冲足够大来容纳要拷贝的字符串.在程序运行时,这将导致 ...