spring cloud-使用Hystrix实现单个方法的fallback
来源:互联网 发布:算法设计与分析第二版 编辑:程序博客网 时间:2024/06/09 21:09
一、加入Hystrix依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>二、编写Controller
package com.chhliu.springboot.restful.controller;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.chhliu.springboot.restful.feignclient.UserFeignClient;import com.chhliu.springboot.restful.vo.User;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;@RestControllerpublic class RestTemplateControllerHystrixCommand {@Autowiredprivate UserFeignClient client; // 使用Feign来消费Restful服务@GetMapping("/get/{id}")@HystrixCommand(fallbackMethod="findByIdFallback")// 使用HystrixCommand注解,在fallbackMethod属性中指定fallback的方法public User findById(@PathVariable Long id) {return client.findById(id);} // 覆写fallbackMethod中指定的方法,注意,此方法的返回值,参数必须与原方法一致public User findByIdFallback(Long id){User u = new User();u.setName("zhangsan");u.setUsername("chhliu");u.setId(9L);return u;}}三、在启动类中添加Hystrix支持
@EnableCircuitBreaker四、添加配置文件
server.port:7904# spring boot服务注册到Eureka Server上的应用名称spring.application.name=springboot-rest-template-feign-hystrixeureka.instance.prefer-ip-address=true# Eureka Server注册服务的地址eureka.client.service-url.defaultZone=http://chhliu:chhliu123456@localhost:8764/eurekaspringboot-h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RetryRulehystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1 #为了测试Hystrix的fallback效果,此处将超时时间设置成1毫秒五、测试
在浏览器中输入:http://localhost:7904/get/2
测试结果如下:
{"id":9,"username":"chhliu","name":"zhangsan","age":null,"balance":null}从上面的测试结果可以看出,由于连接超时,直接进入了fallback方法。
0 0
- spring cloud-使用Hystrix实现单个方法的fallback
- Spring Cloud 的 Hystrix 通过Fallback Factory 检查回退的错误原因
- Spring Cloud 的 Hystrix 在 Feign上使用 Hystrix功能
- spring cloud使用hystrix实现断路保护机制
- [转]spring cloud之熔断器 Hystrix 的原理与使用
- spring cloud FeignClient fallback 断路器
- Spring Cloud 的 Hystrix 为Feign禁用Hystrix
- spring cloud-Feign的Hystrix支持
- 五、Spring Cloud 的 Hystrix 组件
- Spring Cloud中Hystrix的请求缓存
- Spring Cloud中Hystrix的请求合并
- Spring Cloud Hystrix的请求合并
- Spring Cloud Hystrix--熔断器
- spring cloud-hystrix
- spring cloud Hystrix
- spring-cloud-hystrix(一)
- Spring Cloud Hystrix Dashboard
- spring cloud hystrix
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现手机上的资源上传功能
- 线程及进程间的通信
- opencv:图像变换
- 最大公约数和最小公倍数问题
- 二叉平衡树
- spring cloud-使用Hystrix实现单个方法的fallback
- Spring_20-21,切面的优先级&重用切点表达式
- C语言qsort
- scala --数组相关操作
- 广播的一些基本用法
- opencv:canny边缘检测
- opencv:打开摄像头
- 笔试题12
- C语言中Static和Const关键字的的作用