(一)整合Hystrix熔断的两种方式:
(1)注解方式:
启动类注解@EnableCircuitBreaker //对hystrix熔断机制的支持
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
@HystrixProperty(name = "execution.timeout.enabled", value = "false")},fallbackMethod = "hello")
public String index(@PathVariable("name") String name) {
return helloRemoteService.hello(name);
}
public String hello(@RequestParam(value = "name") String name) {
return "hello" +name+", this messge send failed,please check your system!!!";
}
远程调用的方法!
@FeignClient(name = "productionzuul", path = "/home")
public interface HelloRemoteService {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(@RequestParam("name") String name);
}
(2)开启Feign中的熔断:
配置文件加上:
#feign中的熔断器(只作用于服务调用端也就是消费端) ,两种方式 其实还有注解版熔断器
feign.hystrix.enabled=true
启动类需要:
@EnableFeignClients //启用feign进行远程调用
Controller:
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
public String index(@PathVariable("name") String name) {
return helloRemoteService.hello(name);
}
Service
@FeignClient(name = "productionzuul", path = "/home",fallback = HelloRemoteHystrix.class)
public interface HelloRemoteService {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(@RequestParam("name") String name);
}
@Component
public class HelloRemoteHystrix implements HelloRemoteService{
@Override
public String hello(@RequestParam(value = "name") String name) {
return "hello" +name+", this messge send failed,please check your system!!!";
}
}
(二)Hystrix的请求缓存:
参考:博客1
请求缓存不是只写入一次结果就不再变化的,
而是每次请求到达Controller的时候,我们都需要为HystrixRequestContext进行初始化,之前的缓存也就是不存在了,我们是在同一个请求中保证结果相同,同一次请求中的第一次访问后对结果进行缓存,缓存的生命周期只有一次请求!
使用默认所有参数作为cacheKey:
ConsumerController:
@CacheResult
@HystrixCommand(commandKey = "commandKey")
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
public String index(@CacheKey("name") @PathVariable("name") String name) {
//初始化Hystrix请求上下文
HystrixRequestContext.initializeContext();
//利用注解存储
String hello1 = helloRemoteService.hello(name);
String hello2 = helloRemoteService.hello(name);
logger.info("first request result is:{} ,and secend request result is: {}", hello1, hello2);
//清除缓存
flushCacheByAnnotation(name);
//利用注解再存储
String hello3 = helloRemoteService.hello(name);
String hello4 = helloRemoteService.hello(name);
logger.info("first request result is:{} ,and secend request result is: {}", hello3, hello4);
return hello4;
}
/**
* @Author LJ
* @Description name变了就清除缓存
* @Date 19:51 2020/3/3
* @Param [id]
* @return void
**/
@CacheRemove(commandKey = "commandKey")
@HystrixCommand
public void flushCacheByAnnotation(@CacheKey("name") String name){
logger.info("请求缓存已清空!");
//这个@CacheRemove注解直接用在更新方法上效果更好
}
让我们来逐个介绍下@HystrixCommand注解的各个参数:
- commandKey:配置全局唯一标识服务的名称,比如,库存系统有一个获取库存服务,那么就可以为这个服务起一个名字来唯一识别该服务,如果不配置,则默认是@HystrixCommand注解修饰的函数的函数名。
- groupKey:一个比较重要的注解,配置全局唯一标识服务分组的名称,比如,库存系统就是一个服务分组。通过设置分组,Hystrix会根据组来组织和统计命令的告、仪表盘等信息。Hystrix命令默认的线程划分也是根据命令组来实现。默认情况下,Hystrix会让相同组名的命令使用同一个线程池,所以我们需要在创建Hystrix命令时为其指定命令组来实现默认的线程池划分。此外,Hystrix还提供了通过设置threadPoolKey来对线程池进行设置。建议最好设置该参数,使用threadPoolKey来控制线程池组。
- threadPoolKey:对线程池进行设定,细粒度的配置,相当于对单个服务的线程池信息进行设置,也可多个服务设置同一个threadPoolKey构成线程组。
- fallbackMethod:@HystrixCommand注解修饰的函数的回调函数,@HystrixCommand修饰的函数必须和这个回调函数定义在同一个类中,因为定义在了同一个类中,所以fackback
method可以是public/private均可。
- commandProperties:配置该命令的一些参数,如executionIsolationStrategy配置执行隔离策略,默认是使用线程隔离,此处我们配置为THREAD,即线程池隔离。参见:com.netflix.hystrix.HystrixCommandProperties中各个参数的定义。
-
threadPoolProperties:线程池相关参数设置,具体可以设置哪些参数请见:com.netflix.hystrix.HystrixThreadPoolProperties
-
ignoreExceptions:调用服务时,除了HystrixBadRequestException之外,其他@HystrixCommand修饰的函数抛出的异常均会被Hystrix认为命令执行失败而触发服务降级的处理逻辑(调用fallbackMethod指定的回调函数),所以当需要在命令执行中抛出不触发降级的异常时来使用它,通过这个参数指定,哪些异常抛出时不触发降级(不去调用fallbackMethod),而是将异常向上抛出。
-
observableExecutionMode:定义hystrix observable command的模式;
-
raiseHystrixExceptions:任何不可忽略的异常都包含在HystrixRuntimeException中;
-
defaultFallback:默认的回调函数,该函数的函数体不能有入参,返回值类型与@HystrixCommand修饰的函数体的返回值一致。如果指定了fallbackMethod,则fallbackMethod优先级更高。