2018-09-04 11:42:46.0|分类: spring cloud|浏览量: 2935
|
官方代码:https://github.com/Netflix/hystrix 官方文档:https://spring.io/guides/gs/circuit-breaker/ 雪崩效应 基础服务的故障导致级联故障,进而造成了整个分布式系统的不可用,这种现象被称为服务雪崩效应。服务雪崩效应描述的是一种因服务提供者的不可用导致服务消费者的不可用,并将不可用逐渐放大的过程。 A是基础服务,B调用A,C/D调用B。如果A不可用,后面B/C/D也不可用 熔断器就像家里的保险丝,当电流过载了就会跳闸,不过Hystrix的熔断机制相对复杂一些。
项目结构
pom.xml内容 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cookqq</groupId> <artifactId>consumer-order</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>consumer-order</name> <url>http://maven.apache.org</url> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> 增加mvn依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> OrderApp.java增加熔断注解 package com.cookqq.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* 订单服务
*
*/
@SpringBootApplication
@EnableHystrix//支持熔断
public class OrderApp
{
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main( String[] args )
{
SpringApplication.run(OrderApp.class, args);
}
}@EnableHystrix 开启熔断器动能 OrderController.java控制类 package com.cookqq.consumer.controller;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.cookqq.consumer.bean.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@HystrixCommand(fallbackMethod="findUserByIdFallBack")
@GetMapping("/user/{id}")
public User findUserById(@PathVariable Long id){
// return this.restTemplate.getForObject("http://127.0.0.1:8000/"+id, User.class);
// List<ServiceInstance> listInfo = discoveryClient.getInstances("provider-user");
// if(listInfo.size()>0){
// return this.restTemplate.getForObject(
// listInfo.get(new Random().nextInt(listInfo.size())).getUri()+"/"+id,
// User.class);
// }
return this.restTemplate.getForObject("http://provider-user/"+id, User.class);
}
public User findUserByIdFallBack(Long id, Throwable throwable){
System.out.println("进入回退方法:"+throwable.getMessage());
User user = new User();
user.setId(-1L);
user.setName("默认用户");
return user;
}
}@HystrixCommand(fallbackMethod="findUserByIdFallBack"),为findUserById编写一个回退方法findUserByIdFallBack。 启动euraka 启动provider-user 启动consumer-order 访问http://localhost:8080/user/1
关闭provider-user 访问http://localhost:8080/user/1
提供服务provider-user关闭,访问provider-user失败,返回findUserByIdFallBack()内容 访问/health端点,发现hystrix的状态是UP { 虽然我们发现程序执行了回退逻辑,返回了默认用户,但是hystrix的状态是UP。这是因为程序的失败次数没有达到阀值(默认是5内失败20次)。 我们快速执行几十次,再次访问/health端点,发现hystrix状态是CIRCUIT_OPEN {
什么情况下会触发fallback方法?
fallback方法在什么情况下会抛出异常
超时时间(默认1000ms,单位:ms) (1)hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 在调用方配置,被该调用方的所有方法的超时时间都是该值,优先级低于下边的指定配置 (2)hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds 在调用方配置,被该调用方的指定方法(HystrixCommandKey方法名)的超时时间是该值 线程池核心线程数 hystrix.threadpool.default.coreSize(默认为10) Queue (1)hystrix.threadpool.default.maxQueueSize(最大排队长度。默认-1,使用SynchronousQueue。其他值则使用 LinkedBlockingQueue。如果要从-1换成其他值则需重启,即该值不能动态调整,若要动态调整,需要使用到下边这个配置) (2)hystrix.threadpool.default.queueSizeRejectionThreshold(排队线程数量阈值,默认为5,达到时拒绝,如果配置了该选项,队列的大小是该队列) 注意:如果maxQueueSize=-1的话,则该选项不起作用 断路器 (1)hystrix.command.default.circuitBreaker.requestVolumeThreshold(当在配置时间窗口内达到此数量的失败后,进行短路。默认20个) For example, if the value is 20, then if only 19 requests are received in the rolling window (say a window of 10 seconds) the circuit will not trip open even if all 19 failed. 简言之,10s内请求失败数量达到20个,断路器开。 (2)hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds(短路多久以后开始尝试是否恢复,默认5s) (3)hystrix.command.default.circuitBreaker.errorThresholdPercentage(出错百分比阈值,当达到此阈值后,开始短路。默认50%) fallback hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests(调用线程允许请求HystrixCommand.GetFallback()的最大数量,默认10。超出时将会有异常抛出,注意:该项配置对于THREAD隔离模式也起作用) |
