Spring整合Hystrix
1、添加maven依赖
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
2、配置切面
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
3、编写controller类,并添加注解
@Controller
@RequestMapping
public class CommonController {
/**
* 主页
*
* @return
*/
@RequestMapping("/")
public String home() {
return "home.html";
} /**
* 配置2秒超时,超时后调用testFallback方法返回到error界面<br>
* 当并发量比较大时,并非所有阻断或失败的请求都会走fallback方法,当处理线程忙不过来时,会直接抛出HystrixRuntimeException异常
*
* @param mav
* @param time 睡眠时间
* @return
*/
@HystrixCommand(groupKey = "groupTest", commandKey = "commandTest", fallbackMethod = "testFallback", commandProperties = {
@HystrixProperty(name = "execution.timeout.enabled", value = "true"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") })
@RequestMapping("/test")
public ModelAndView test(ModelAndView mav, @RequestParam(defaultValue = "1") int time) {
try {
Thread.sleep(1000 * time);
} catch (Exception e) {
}
mav.setViewName("success.html");
return mav;
} /**
* test访问熔断后回调页面
*
* @param mav
* @param time
* @return
*/
protected ModelAndView testFallback(ModelAndView mav, @RequestParam(defaultValue = "1") int time) {
mav.setViewName("fallback.html");
return mav;
}
}
4、在webapp目录下添加fallback.html、success.html文件
5、访问http://127.0.0.1:8080/spring-hystrix-demo/test?time=0,浏览器正常进入success.html页面;
访问http://127.0.0.1:8080/spring-hystrix-demo/test?time=5,连接超时,后台执行testFallback方法,浏览器进入fallback.html页面。
Spring整合Hystrix的更多相关文章
- Spring Cloud Hystrix理解与实践(一):搭建简单监控集群
前言 在分布式架构中,所谓的断路器模式是指当某个服务发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,这样就不会使得线程因调用故障服务被长时间占用不释放,避免故障的继续蔓延.Spring ...
- SpringCloud系列-整合Hystrix的两种方式
Hystrix [hɪst'rɪks],中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力.本文所说的Hystrix是Netflix开源的一款容错框架,同样具有自我保护能力. 本文目录 一.H ...
- 7、Spring Cloud Hystrix
1.Spring Cloud Hystrix简介 (1).分布式问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 多个微服务之间调用的时候,假设微服务A调 ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- spring整合hibernate的详细步骤
Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...
- Spring整合Ehcache管理缓存
前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
随机推荐
- /usr/bin/perl:bad interpreter:No such file or directory 的解决办法
yum -y install gcc gcc-c++ perl make kernel-headers kernel-devel 可能会提示:Cannot find a valid baseurl f ...
- python中常用的模块一
一,常用的模块 模块就是我们将装有特定功能的代码进行归类,从代码编写的单位来看我们的程序,从小到大的顺序: 一条代码<语句块,<代码块(函数,类)<模块我们所写的所有py文件都是模块 ...
- SqlServer中exists和in的区别
1.in 2.exists
- 自动化测试基础-断言(Assert)使用方法
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- 力扣(LeetCode)292. Nim游戏 巴什博奕
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你 ...
- 学习笔记21—PS换图片背景
将照片红底的换成白底的. 操作步骤: 1 先上效果,照片来自网络反正不认识,法律问题找度娘 2 下面开始操作,打开图片进入通道面板,选择照片底色的那个通道,复制并调整色阶,确保黑白分明 3 回到图层面 ...
- thinkphp5中如何使用 usort
thinkphp5中如何使用 usort 一.总结 一句话总结:其实比较函数加上命名空间就好啦,不然找不到 比较函数加命名空间 数组做usort的第二个参数 usort($question_list, ...
- SpringBoot集成TkMybatis插件 (二)
一.Tkmybatis的好处 Tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效.这个插件里面封装好了我们需要用到的很多sql语句,不过这个插件是通过我们去调用它封装的各种 ...
- ASA与N6K对接
ASA5545配置interface GigabitEthernet0/0 channel-group 10 mode active no nameif no security-level no ip ...
- gem "searchkick"(4500✨) 智能搜索(使用Elastic search)(Gorails:建立一个侧边搜索栏)
Searchkick