Hystrix实现微服务的容错处理
cookqq ›博客列表 ›spring cloud

Hystrix实现微服务的容错处理

2018-09-04 11:42:46.0|分类: spring cloud|浏览量: 2593

摘要: Hystrix熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。

官方代码:https://github.com/Netflix/hystrix

官方文档:https://spring.io/guides/gs/circuit-breaker/


雪崩效应

基础服务的故障导致级联故障,进而造成了整个分布式系统的不可用,这种现象被称为服务雪崩效应。服务雪崩效应描述的是一种因服务提供者的不可用导致服务消费者的不可用,并将不可用逐渐放大的过程。

image

A是基础服务,B调用A,C/D调用B。如果A不可用,后面B/C/D也不可用


熔断器就像家里的保险丝,当电流过载了就会跳闸,不过Hystrix的熔断机制相对复杂一些。

image

项目结构

blob.png

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

blob.png

关闭provider-user

访问http://localhost:8080/user/1

blob.png

提供服务provider-user关闭,访问provider-user失败,返回findUserByIdFallBack()内容


访问/health端点,发现hystrix的状态是UP

{
    "description":"Composite Discovery Client",
    "status":"UP",
    "discoveryComposite":Object{...},
    "diskSpace":Object{...},
    "refreshScope":Object{...},
    "hystrix":{
        "status":"UP"
    }

}


虽然我们发现程序执行了回退逻辑,返回了默认用户,但是hystrix的状态是UP。这是因为程序的失败次数没有达到阀值(默认是5内失败20次)。

我们快速执行几十次,再次访问/health端点,发现hystrix状态是CIRCUIT_OPEN

{
    "description":"Composite Discovery Client",
    "status":"UP",
    "discoveryComposite":Object{...},
    "diskSpace":Object{...},
    "refreshScope":Object{...},
    "hystrix":{
        "status":"CIRCUIT_OPEN",
        "openCircuitBreakers":[
            "OrderController::findUserById"
        ]

    }

}


image

image

image


什么情况下会触发fallback方法?

名字

描述

触发fallback

EMIT

值传递

NO

SUCCESS

执行完成,没有错误

NO

FAILURE

执行抛出异常

YES

TIMEOUT

执行开始,但没有在允许的时间内完成

YES

BAD_REQUEST

执行抛出HystrixBadRequestException

NO

SHORT_CIRCUITED

断路器打开,不尝试执行

YES

THREAD_POOL_REJECTED

线程池拒绝,不尝试执行

YES

SEMAPHORE_REJECTED

信号量拒绝,不尝试执行

YES

 

fallback方法在什么情况下会抛出异常

名字

描述

抛异常

FALLBACK_EMIT

Fallback值传递

NO

FALLBACK_SUCCESS

Fallback执行完成,没有错误

NO

FALLBACK_FAILURE

Fallback执行抛出出错

YES

FALLBACK_REJECTED

Fallback信号量拒绝,不尝试执行

YES

FALLBACK_MISSING

没有Fallback实例

YES

 

超时时间(默认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隔离模式也起作用)


一键分享文章

分类列表

  • • struts源码分析
  • • flink
  • • struts
  • • redis
  • • kafka
  • • ubuntu
  • • zookeeper
  • • hadoop
  • • activiti
  • • linux
  • • 成长
  • • NIO
  • • 关键词提取
  • • mysql
  • • android studio
  • • zabbix
  • • 云计算
  • • mahout
  • • jmeter
  • • hive
  • • ActiveMQ
  • • lucene
  • • MongoDB
  • • netty
  • • flume
  • • 我遇到的问题
  • • GRUB
  • • nginx
  • • 大家好的文章
  • • android
  • • tomcat
  • • Python
  • • luke
  • • android源码编译
  • • 安全
  • • MPAndroidChart
  • • swing
  • • POI
  • • powerdesigner
  • • jquery
  • • html
  • • java
  • • eclipse
  • • shell
  • • jvm
  • • highcharts
  • • 设计模式
  • • 列式数据库
  • • spring cloud
  • • docker+node.js+zookeeper构建微服务
版权所有 cookqq 感谢访问 支持开源 京ICP备15030920号
CopyRight 2015-2018 cookqq.com All Right Reserved.