本文共 5234 字,大约阅读时间需要 17 分钟。
熔断机制产生的背景是“雪崩效应”,在分布式系统中,一个服务提供者的不可用会导致服务消费者的不可用,一个服务的异常,最终会影响其他服务,造成线程阻塞,资源耗尽,如果有大量的请求,会导致服务瘫痪,引蝴蝶效应造成系统宕机。
如果一个服务的错误过多,短时间内得不到修复,就可以开启熔断机制,防止多次没有意义的调用。服务调用方会定时重试,如果可用,就继续使用。
熔断有以下几种状态:
闭合状态
添加一个计数器,如果失败的次数在指定时间内超过一定阈值,则开启熔断,此时处于断开状态,此时开启一个计时器,如果过了指定时间,则切换到半断开,服务可被尝试调用。断开状态
服务如果被调用会立即返回错误信息,减少资源损耗。半开状态
允许一定数量的请求调用服务,如果请求调用成功,则关闭熔断,认为服务的错误已经修正。如果还是发生错误,则切换到断开状态。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
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); }}
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; }}
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/