Spring Cloud构建微服务架构(三)断路器
2024-10-14 02:26:18
在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。
Netflix Hystrix
在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。
下面我们来看看如何使用Hystrix。
准备工作
在开始加入断路器之前,我们先拿之前构建两个微服务为基础进行下面的操作,主要使用下面几个工程
- eureka-server工程:服务注册中心,端口1111
- compute-service工程:服务单元,端口2222
- eureka-ribbon:通过ribbon实现的服务单元,依赖compute-service的服务,端口3333
- eureka-feign:通过feign实现的服务单元,依赖compute-service的服务,端口3333
Ribbon中引入Hystrix
- 依次启动eureka-server、compute-service、eureka-ribbon工程
- 访问http://localhost:1111/可以看到注册中心的状态
- 访问http://localhost:3333/add,调用eureka-ribbon的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30
- 关闭compute-service服务,访问http://localhost:3333/add,我们获得了下面的报错信息
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jun 25 21:16:59 CST 2016
There was an unexpected error (type=Internal Server Error, status=500).
I/O error on GET request for "http://COMPUTE-SERVICE/add?a=10&b=20": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connectpom.xml
中引入依赖hystrix依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>- 在eureka-ribbon的主类
RibbonApplication
中使用@EnableCircuitBreaker
注解开启断路器功能:
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class RibbonApplication { @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
} }- 改造原来的服务消费方式,新增
ComputeService
类,在使用ribbon消费服务的函数上增加@HystrixCommand
注解来指定回调方法。
@Service
public class ComputeService { @Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "addServiceFallback")
public String addService() {
return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
} public String addServiceFallback() {
return "error";
} }- 提供rest接口的Controller改为调用ComputeService的addService
@RestController
public class ConsumerController { @Autowired
private ComputeService computeService; @RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return computeService.addService();
} }- 验证断路器的回调
- 依次启动eureka-server、compute-service、eureka-ribbon工程
- 访问http://localhost:1111/可以看到注册中心的状态
- 访问http://localhost:3333/add,页面显示:30
- 关闭compute-service服务后再访问http://localhost:3333/add,页面显示:error
更多关于Hystrix的使用可参考How To Use
Feign使用Hystrix
注意这里说的是“使用”,没有错,我们不需要在Feigh工程中引入Hystix,Feign中已经依赖了Hystrix,我们可以在未做任何改造前,尝试下面你的操作:
- 依次启动eureka-server、compute-service、eureka-feign工程
- 访问http://localhost:1111/可以看到注册中心的状态
- 访问http://localhost:3333/add,调用eureka-feign的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30
- 关闭compute-service服务,访问http://localhost:3333/add,我们获得了下面的报错信息
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jun 25 22:10:05 CST 2016
There was an unexpected error (type=Internal Server Error, status=500).
add timed-out and no fallback available.如果您够仔细,会发现与在ribbon中的报错是不同的,看到
add timed-out and no fallback available
这句,或许您已经猜到什么,看看我们的控制台,可以看到报错信息来自hystrix-core-1.5.2.jar
,所以在这个工程中,我们要学习的就是如何使用Feign中集成的Hystrix。- 使用
@FeignClient
注解中的fallback属性指定回调类
@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }- 创建回调类
ComputeClientHystrix
,实现@FeignClient
的接口,此时实现的方法就是对应@FeignClient
接口中映射的fallback函数。
@Component
public class ComputeClientHystrix implements ComputeClient { @Override
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -9999;
} }- 再用之前的方法验证一下,是否在compute-service服务不可用的情况下,页面返回了-9999。
- 关于Feign的更多使用方法可参考:Feign
最新文章
- 关于MySQL数据库优化的部分整理
- 杭电ACM 1998奇数阶魔方
- BestCoder Round #39
- 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍
- hdu 1559 最大子矩阵 (简单dp)
- WP-Syntax 插件使用方法
- lua wireshark 数据报解析
- xxx.java: Recompile with -Xlint:deprecation for details
- WPF3D学习,立方体的绘制
- leetcode[149]Max Points on a Line
- HTML知识速递
- http协议——cookie详解
- 浏览器端时间循环与nodejs端时间循环的不同之处(宏任务与微任务)
- Ubuntu18.04安装英伟达显卡驱动
- 关于Could not load driverClass ${jdbc.driverClassName}问题解决方案
- Moment.js 基本用法
- 6-14 Abbott的复仇 uva816
- 电影《Green book》观后感_已补全:携带着种族歧视的“光环”,艰难地获得朋友的相互依赖,依然得享受生活的酸甜苦咸。
- mybatis spring boot深入
- mfc CFileFind查找类
热门文章
- [Node.js] Level 3 new. Steam
- 设置tableViewCell背景颜色
- Win10 专业版激活!
- 解决Swap file &;quot;.ceshi.c.swp&;quot; already exists!问题
- MAC系统XAMPP 中 MySQL命令行client配置使用
- HTML的GET方法传递参数样式。
- javaweb项目打成war包
- rxvt-unicode配置
- 光驱在资源管理器显示黄色感叹号的解决方法BIOS内有 系统下没有
- http 事务