Hystrix参数配置
2、Hystrix参数配置示例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.mimaxueyuan.consumer.turbine.service.MyService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager; @RestController
public class HystrixController { @Autowired
private MyService myService; /**
* @HystrixCommand由名为“javanica”的Netflix contrib库提供 。
* Spring Cloud在连接到Hystrix断路器的代理中使用该注解自动包装Spring bean。
* 断路器计算何时打开和关闭电路,以及在发生故障时应该做什么。
*
* @author Kevin
* @Title: get
* @param id
* @return
* @return: String
*/
@GetMapping("/get1/{id}")
@HystrixCommand(fallbackMethod = "getError")
public String get1(@PathVariable String id) {
try {
System.out.println(Thread.currentThread().getName()+":get1 before sleep 5s....");
Thread.sleep(1000 * 5);
System.out.println(Thread.currentThread().getName()+":get1 after sleep 5s....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "kevin.get1." + id;
} /**
* 配置Hystrix隔离策略,默认为THREAD,隔离策略分为THREAD、SEMAPHORE两种
*
* execution.isolation.strategy 默认为THREAD
*
* @param id
* @return
* @author lynch
*/
@GetMapping("/get2/{id}")
@HystrixCommand(fallbackMethod = "getError", commandProperties = {
// @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value="THREAD")
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE") })
public String get2(@PathVariable String id) {
try {
System.out.println(Thread.currentThread().getName()+":get2 before sleep 5s....");
Thread.sleep(1000 * 5);
System.out.println(Thread.currentThread().getName()+":get2 after sleep 5s....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "kevin.get2." + id;
} /**
* 设置HystrixCommand的执行是否在超时发生时被中断。使用线程隔离时,是否对命令执行超时的线程调用中断(Thread.interrupt())操作
*
* execution.isolation.thread.interruptOnTimeout 参数,默认为true
*
* @param id
* @return
* @author lynch
*/
@GetMapping("/get3/{id}")
@HystrixCommand(fallbackMethod = "getError",commandProperties= {
//设置超时的时候不中断线程,默认为true
@HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_INTERRUPT_ON_TIMEOUT,value="false")
})
public String get3(@PathVariable String id) {
try {
System.out.println(Thread.currentThread().getName()+":get3 before sleep 5s....");
Thread.sleep(1000 * 5);
System.out.println(Thread.currentThread().getName()+":get3 after sleep 5s....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "kevin.get3." + id;
} /**
* 设置调用线程产生的HystrixCommand.getFallback()方法的允许最大请求数目。 如果达到最大并发数目,后续请求将会被拒绝,如果没有实现回退,则抛出异常。
* (这里需要注意一点,这个变量的命名不是很规范,它实际上对THREAD和SEMAPHORE两种隔离策略都生效)
*
* fallback.isolation.semaphore.maxConcurrentRequests默认为10
*
* @param id
* @return
* @author lynch
*/
@GetMapping("/get4/{id}")
@HystrixCommand(fallbackMethod = "getError",commandProperties= {
@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"),
@HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS,value="1")
})
public String get4(@PathVariable String id) {
try {
System.out.println(Thread.currentThread().getName()+":get4 before sleep 5s....");
Thread.sleep(1000 * 5);
System.out.println(Thread.currentThread().getName()+":get4 after sleep 5s....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "kevin.get4." + id;
} /**
* 设置HystrixCommand的执行是否有超时限制。
*
* execution.timeout.enabled 默认为true, 是否超时
*
* @param id
* @return
*/
@GetMapping("/get5/{id}")
@HystrixCommand(fallbackMethod = "getError",commandProperties= {
@HystrixProperty(name=HystrixPropertiesManager.EXECUTION_TIMEOUT_ENABLED,value="false")
})
public String get5(@PathVariable String id) {
try {
System.out.println(Thread.currentThread().getName()+":get5 before sleep 5s....");
Thread.sleep(1000 * 5);
System.out.println(Thread.currentThread().getName()+":get5 after sleep 5s....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "kevin.get5." + id;
} /**
* 设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑。
* 注意:超时会作用在HystrixCommand.queue(),即使调用者没有调用get()去获得Future对象。
*
* execution.isolation.thread.timeoutInMilliseconds 默认为1000
*
* @param id
* @return
* @author lynch
*/
@GetMapping("/get6/{id}")
@HystrixCommand(fallbackMethod = "getError",commandProperties= {
@HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,value="6000")
})
public String get6(@PathVariable String id) {
try {
System.out.println(Thread.currentThread().getName()+":get6 before sleep 5s....");
Thread.sleep(1000 * 5);
System.out.println(Thread.currentThread().getName()+":get6 after sleep 5s....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "kevin.get6." + id;
} public String getError(String id) {
System.out.println(Thread.currentThread().getName()+":getError before ....");
myService.execute();
System.out.println(Thread.currentThread().getName()+":getError after ....");
return "超时错误,使用断路器返回" + id;
}
}
Hystrix参数配置的更多相关文章
- Hystrix【参数配置及缓存】
1.常用参数说明 hystrix参数的详细配置可参照 https://github.com/Netflix/Hystrix/wiki/Configuration 下面是一些常用的配置: 配置项 默认值 ...
- hystrix参数使用方法
hystrix+feign+ribbon,但是可能很多人都知道hystrix还有线程隔离,信号量隔离,等等各种参数配置,在这几就记录下hystrix的参数, 一.hystrix参数使用方法 通过注解@ ...
- SpringCloud系列七:Hystrix 熔断机制(Hystrix基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)
1.概念:Hystrix 熔断机制 2.具体内容 所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢? 当现在服务的 ...
- Hystrix属性配置策略
Hystrix属性配置 Command可配参数 设置隔离策略 execution.isolation.strategy = THREAD 设置超时时间 execution.isolation.thre ...
- Hystrix断路器配置属性解析
HystrixCommand 配置方式 我们的配置都是基于 HystrixCommand 的,我们通过在方法上添加 @HystrixCommand 注解并配置注解的参数来实现配置,但有的时候一个类里面 ...
- 转:浅谈UNIX下Apache的MPM及httpd.conf配置文件中相关参数配置
为什么要并发处理 以Apache为代表的web服务器中,如果不支持并发,则在一个客户端连接的时候,如果该客户端的任务没有处理完,其他连接的客户端将会一直处于等待状态,这事不可想象的,好像没有为什么要不 ...
- Spark on Yarn:任务提交参数配置
当在YARN上运行Spark作业,每个Spark executor作为一个YARN容器运行.Spark可以使得多个Tasks在同一个容器里面运行. 以下参数配置为例子: spark-submit -- ...
- Production环境中iptables常用参数配置
production环境中iptables常用参数配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我相信在实际生产环境中有很多运维的兄弟跟我一样,很少用到iptables的这个 ...
- Unity3D安卓打包参数配置与兼容性的关系分析
前言 在使用Unity3D工程导出安卓安装包的时候,往往会遇到兼容性的问题,针对某些机型,要么无法打开游戏,要么会出现卡机的现象.面对这种情况,我们可以调节相关的参数来提高兼容性. 为了了解在打包时候 ...
随机推荐
- java 遇到的问题
1.list.sort(new Comparator<String>() { @override public int compare(String o1, String o2) { re ...
- UI与开发的必备神器!— iDoc一键适配不同平台尺寸(iDoc201902-2新功能)
一.自动换算不同平台尺寸在一个项目从设计到开发的过程中,为了适配不同设备,一份设计稿,UI需要花大量的时间去制作各种尺寸的切图,耗时耗力. 那有没有一种高效的办法,让UI只需要设计一份设计稿就可以了呢 ...
- The current state of generics in Delphi( 转载)
The current state of generics in Delphi To avoid duplication of generated code, the compiler build ...
- 《Linux就该这么学》第三天课程
秦时明月经典语录: 王道: 千里挥戈,万众俯首.四海江湖,百世王道.——项羽 今天主要介绍了常用系统工作的命令 如需进一步了解,请前往https://www.linuxcool.com(附带配音) r ...
- VM无法连接到虚拟机
The VMware Authorization Service is not running. 原因 虚拟机服务没有开启 解决方法 1. 我的电脑右击->管理 2. 打开服 ...
- ABP框架系列之一:(Entity-实体)
Entities are one of the core concepts of DDD (Domain Driven Design). Eric Evans describe it as " ...
- [转]Ubuntu16.04下ralink rt3290驱动安装
出处:https://askubuntu.com/questions/253632/how-do-i-get-a-ralink-rt3290-wireless-card-working 解决为问题:L ...
- docker相关操作
docker 安装参照官网一步一步来,特别简单,主要是下载比较慢: docker 需要 管理员权限: docker 相关命令: 容器生命周期管理 — docker [run|start|stop|re ...
- day_1 Python介绍及计算机组成和系统
python学习路线 基础语法 - 文件操作 - 函数 - 模块 - 面向对象(类) - 网络编程 - 数据库 - 前段 - 项目 学习方法 wwwh: what-why-where-how #wha ...
- GCD on Blackboard
题目大意:给你n个数,然后在这n个数中选一个数,选中的这个数可以变成任意的数,使这n个数的gcd(最大公约数)最大.打印这个最大的gcd. 思路:这题一看貌似很复杂,其实这题只要你知道前缀和 和 ...