博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-Cloud-Finchley | 熔断 Hystrix
阅读量:4104 次
发布时间:2019-05-25

本文共 5234 字,大约阅读时间需要 17 分钟。

简介

熔断机制产生的背景是“雪崩效应”,在分布式系统中,一个服务提供者的不可用会导致服务消费者的不可用,一个服务的异常,最终会影响其他服务,造成线程阻塞,资源耗尽,如果有大量的请求,会导致服务瘫痪,引蝴蝶效应造成系统宕机。

如果一个服务的错误过多,短时间内得不到修复,就可以开启熔断机制,防止多次没有意义的调用。服务调用方会定时重试,如果可用,就继续使用。

熔断有以下几种状态:

  • 闭合状态

    添加一个计数器,如果失败的次数在指定时间内超过一定阈值,则开启熔断,此时处于断开状态,此时开启一个计时器,如果过了指定时间,则切换到半断开,服务可被尝试调用。

  • 断开状态

    服务如果被调用会立即返回错误信息,减少资源损耗。

  • 半开状态

    允许一定数量的请求调用服务,如果请求调用成功,则关闭熔断,认为服务的错误已经修正。如果还是发生错误,则切换到断开状态。

实例

1、搭建 maven 环境

Hystrix 是知名视频网站 Netflix 开源的框架,完美解决了分布式系统中所面临的这个技术问题,解决了雪崩效应。

首先搭建 maven 环境,父 pom 如下

UTF-8
1.8
1.8
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
2、搭建服务注册中心 eureka-server

maven 依赖

com.hly
04-spring-cloud-hystrix
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web

application.yml 配置文件

server:  port: 8761eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:  application:    name: service-server

SpringBoot 启动类

@SpringBootApplication@EnableEurekaServerpublic class ServiceServerApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceServerApplication.class, args);	}}
3、搭建服务提供者 client

maven 依赖

com.hly
04-spring-cloud-hystrix
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web

application.yml

server:  port: 8762spring:  application:    name: service-clienteureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/

SpringBoot 启动类

@SpringBootApplication@RestController@EnableEurekaClientpublic class ServiceClientApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceClientApplication.class, args);	}	@Value("${server.port}")	String port;	@RequestMapping("/hello")	public String home(@RequestParam(value = "name", defaultValue = "hly") String name) {		return "hi " + name + " ,i am from port:" + port;	}}
4、搭建服务消费者 feign 客户端

maven 依赖

com.hly
04-spring-cloud-hystrix
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign

application.yml

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8765spring:  application:    name: service-feign# 开启熔断feign:  hystrix:    enabled : true

服务层接口

//服务名指定调用哪个服务@FeignClient(value = "service-client",fallback =HiHystrixServiceImpl.class)public interface HiHystrixService {    //这里的映射名和需要调用的服务的映射名一样    @RequestMapping(value = "/hello",method = RequestMethod.GET)    String sayHiFromClientOne(@RequestParam(value = "name") String name);}

指定实行快速失败的方法

@Componentpublic class HiHystrixServiceImpl implements HiHystrixService {    @Override    public String sayHiFromClientOne(String name) {        return "sorry"+name;    }}

Controller 层

@RestControllerpublic class HiController {   @Autowired   HiHystrixService hiHystrixService;    /**     * 消费服务     * @param name     * @return     */    @GetMapping(value = "/hi")    public String sayHi(@RequestParam String name) {        return hiHystrixService.sayHiFromClientOne( name );    }}

SpringBoot 启动类

@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@EnableFeignClientspublic class ServiceFeignHystrixApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceFeignHystrixApplication.class, args);	}}

演示

1、依次启动 注册中心 server ,启动两个 client(启动一个后,修改application.yml 配置文件的端口,再次启动一次),接着启动 Feign 客户端,访问:

http://localhost:8765/hi?name=hly
当关掉客户端后,将会执行快速失败策略。

代码下载

04-spring-cloud-hystrix:

关于

公众号:【星尘Pro】

github:

推荐阅读

转载地址:http://vufsi.baihongyu.com/

你可能感兴趣的文章
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
python九九乘法表(详解)
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(二)
查看>>
pytorch(三)
查看>>
pytorch(四)
查看>>