SpringCloud 商城系统搭建之Hystrix(基于Ribbon + RestTemplate)

不点 阅读:605 2021-03-31 13:18:44 评论:0

前提

本文是基于SpringCloud 商城系统搭建之eureka 

一、Hystrix简介

Hystrix是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失败,从而提升系统的可用性、容错性与局部应用的弹性,是一个实现了超时机制和断路器模式的工具类库。

二、Hystrix的设计原则

  • 防止任何单独的依赖耗尽资源(线程) 过载立即切断并快速失败,防止排队
  • 尽可能提供回退以保护用户免受故障
  • 使用隔离技术(例如隔板,泳道和断路器模式)来限制任何一个依赖的影响
  • 通过近实时的指标,监控和告警,确保故障被及时发现
  • 通过动态修改配置属性,确保故障及时恢复
  • 防止整个依赖客户端执行失败,而不仅仅是网络通信

三、Hystrix的工作原理

  • 使用命令模式将所有对外部服务(或依赖关系)的调用包装在HystrixCommand或HystrixObservableCommand对象中,并将该对象放在单独的线程中执行。
  • 每个依赖都维护着一个线程池(或信号量),线程池被耗尽则拒绝请求(而不是让请求排队)。
  • 记录请求成功,失败,超时和线程拒绝。
  • 服务错误百分比超过了阈值,熔断器开关自动打开,一段时间内停止对该服务的所有请求。
  • 请求失败,被拒绝,超时或熔断时执行降级逻辑。
  • 近实时地监控指标和配置的修改。

当使用Hystrix封装每个基础依赖项时,每个依赖项彼此隔离,受到延迟时发生饱和的资源的限制,并包含回退逻辑,该逻辑决定了在依赖项中发生任何类型的故障时做出什么响应。

四、基于Ribbon + RestTemplate使用熔断器

按照下面步骤改造之前的项目supermarker-consum

1、pom.xml 引入Hystrix 依赖jar 包

        <!-- hystrix --> 
		<dependency> 
			<groupId>org.springframework.cloud</groupId> 
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 
		</dependency>

2、启动类上添加@EnableHystrix注解

在启动类上添加@EnableHystrix注解开启Hystrix的熔断器功能,改造后启动类如下:

package com.zzg; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
import org.springframework.cloud.netflix.hystrix.EnableHystrix; 
 
@SpringBootApplication 
@EnableEurekaClient 
@EnableHystrix 
public class EurekaConsumeApplication { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		SpringApplication.run(EurekaConsumeApplication.class, args); 
	} 
 
} 

3、改造UserController

在需要有熔断机制的方法上添加 @HystrixCommand,属性fallbackMethod是熔断时返回的方法,修改完成后UserController.java代码如下:

package com.zzg.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.client.RestTemplate; 
 
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 
import com.zzg.entity.User; 
 
@RestController 
public class UserController { 
	@Autowired 
	private RestTemplate restTemplate; 
	 
	@HystrixCommand(fallbackMethod = "error") 
	@GetMapping("/user") 
    public User findById(@RequestParam(value = "id") Integer id) { 
		return restTemplate.getForObject("http://provider/user?id=" + id, User.class); 
	} 
	 
	 public User error(Integer id) { 
		 System.out.println("Hystrix 断路器 生效"); 
	     return new User(); 
	 } 
 
} 

4、开始熔断功能测试

按顺序启动supermarker-eureka(服务注册中心)、supermarker-provider(生产者)和supermarker-consume(消费者),此时打开浏览器访问http://localhost:8083/user?id=1,服务正常,截图如下:

然后停服务supermarker-provider,再次访问访问http://localhost:8083/user?id=1,此时会触发熔断,截图如下:

 

标签:Spring Cloud
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号