1.概述
1.1.分布式系統面臨的問題
複雜分布式體系結構中的應用程式 有數10個依賴關系,每個依賴關系在某些時候将不可避免地失敗

1.2.是什麼
1.3.能幹嘛
服務降級
服務熔斷
接近實時的監控
1.4.官網資料
https://github.com/Netflix/hystrix/wiki
1.5.官宣,停更進維
https://github.com/Netflix/hystrix
被動修複bugs
不再接受合并請求
不再釋出新版本
2.HyStrix重要概念
2.1.服務降級
伺服器忙,請稍後再試,不讓用戶端等待并立刻傳回一個友好提示,fallback
2.2.哪些情況會發出降級
程式運作異常
逾時
服務熔斷觸發服務降級
線程池/信号量也會導緻服務降級
2.3.服務熔斷
類比保險絲達到最大服務通路後,直接拒絕通路,拉閘限電,然後調用服務降級的方法并傳回友好提示
就是保險絲
服務的降級->進而熔斷->恢複調用鍊路
2.4.服務限流
秒殺高并發等操作,嚴禁一窩蜂的過來擁擠,大家排隊,一秒鐘N個,有序進行
3.hystrix案例
3.1.建構
建立cloud-provider-hystrix-payment8001
POM
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud2020</artifactId>
<groupId>com.itxiongmao.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-hystrix-payment8001</artifactId>
<dependencies>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itxiongmao.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
YML
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
主啟動
package com.itxiongmao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao
* @CreateTime: 2020-11-22 12:37
* @Description: TODO
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}
業務類
service
package com.itxiongmao.service;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.service
* @CreateTime: 2020-11-22 12:39
* @Description: TODO
*/
public interface PaymentService {
String getPaymentInfo_OK(Integer id);
String paymentInfo_TimeOut(Integer id);
}
package com.itxiongmao.service.impl;
import com.itxiongmao.service.PaymentService;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.service.impl
* @CreateTime: 2020-11-22 12:39
* @Description: TODO
*/
@Service
public class PaymentServiceImpl implements PaymentService {
/*
* 正常通路
* @param id
* @return
*/
@Override
public String getPaymentInfo_OK(Integer id) {
return "線程池:" + Thread.currentThread().getName() + " paymentInfo_OK,id:" + id + "\t" + "O(∩_∩)O哈哈~";
}
/**
* 逾時通路
*
* @param id
* @return
*/
@Override
public String paymentInfo_TimeOut(Integer id) {
int timeNumber = 3;
try {
// 暫停3秒鐘
TimeUnit.SECONDS.sleep(timeNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "線程池:" + Thread.currentThread().getName() + " paymentInfo_TimeOut,id:" + id + "\t" +
"O(∩_∩)O哈哈~ 耗時(秒)" + timeNumber;
}
}
PaymentController
package com.itxiongmao.controller;
import com.itxiongmao.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String servicePort;
/**
* 正常通路
*
* @param id
* @return
*/
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id) {
String result = paymentService.getPaymentInfo_OK(id);
log.info("*****result:" + result);
return result;
}
/**
* 逾時通路
*
* @param id
* @return
*/
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfo_TimeOut(id);
log.info("*****result:" + result);
return result;
}
}
3.2.正常測試
啟動eureka7001
啟動eureka-provider-hystrix-payment8001
通路
success的方法
http://localhost:8001/payment/hystrix/ok/31
每次調用耗費5秒鐘
http://localhost:8001/payment/hystrix/timeout/31