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 ...
随机推荐
- SHU oj 422 风力观测 线段树
风力观测 发布时间: 2017年7月9日 18:17 最后更新: 2017年7月9日 21:04 时间限制: 1000ms 内存限制: 128M 描述 小Y正在观测y地区的风力情况,他在一 ...
- Java 通过get post 请求url
1️⃣.已获取小程序的access_token 为例,通过Get请求url import com.alibaba.fastjson.JSONObject; String wechatUrl = &qu ...
- restFul接口设计规范
1.域名 1 应该尽量将API部署在专用域名之下. https://api.example.com 2 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https://example. ...
- MySQL字段拼接Concat
有时候,从数据库中拿出的数据并不是我们想要的格式,比如,有以下的vendors表 如果,想以 name (location)的格式展现出来,那么就要用到MySQL的Concat了. Concat()拼 ...
- 将矩阵数据转换为栅格图 filled.contour()
require(grDevices) # for colours filled.contour(volcano, color = terrain.colors, asp = 1) # simple x ...
- 浅谈Nginx负载均衡与F5的区别
前言 笔者最近在负责某集团网站时,同时用到了Nginx与F5,如图所示,负载均衡器F5作为处理外界请求的第一道“墙”,将请求分发到web服务器后,web服务器上的Nginx再进行处理,静态内容直接访问 ...
- C#_Demo_摄像头实时_4线程人脸识别注册开发全过程
v效率有点低,大家看看哪里开可以节省时间?源代码:https://github.com/catzhou2002/ArcFaceDemo说实话,为了提高识别效率,我也是竭尽所能,干了不少自认为的优化,如 ...
- easyUI tree 自定义图标
文章转载自: https://blog.csdn.net/zhlantian/article/details/52913115 近期由于项目中需要使用easyui tree树形列表,并在系统中动态配置 ...
- Codeforces 1077 F2 - Pictures with Kittens (hard version)
F2 - Pictures with Kittens (hard version) 思路: 单调队列优化dp 代码: #pragma GCC optimize(2) #pragma GCC optim ...
- css各种练习
2017年10月22日 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...