当前位置:网站首页>10.hystrix circuit breaker
10.hystrix circuit breaker
2022-06-28 16:39:00 【Bitter candy】
Study B Standing still, Mr. Zhou Yang of Silicon Valley SpringCloud The lecture notes
1. summary
1.1 The problem of distributed system
1.2. What is it?
1.3. What can I do?
service degradation
Service failure
Service restriction
Close to real-time monitoring
github Address :https://github.com/Netflix/Hystrix/wiki/How-To-Use
Hystrix Official announcement , Stop and improve dimension , Passive repair bugs, No longer accept merge requests , No more releases
2.hystrix Case study
2.1 newly build cloud-provider-hystrix-payment8001
2.1.1 Change pom
<dependencies>
<!-- newly added 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>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.1.2 Change yml
server:
port: 8001
eureka:
client:
register-with-eureka: true # I don't register myself with the registry
fetch-registry: true # Say you're the registry , The responsibility is to maintain service instances , There's no need to retrieve Services
service-url:
# defaultZone: http://eureka7002.com:7002/eureka/ # Set up with eureka server Both the interactive address query service and the registration service need to rely on this address
defaultZone: http://eureka7001.com:7001/eureka/
# server:
# enable-self-preservation: false
spring:
application:
name: cloud-provider-hystrix-payment
# eviction-interval-timer-in-ms: 2000
2.1.3 Main startup class and business class
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
}
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_OK(id);
log.info("*******result:"+result);
return result;
}
@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;
}
}
@Service
public class PaymentService {
// success
public String paymentInfo_OK(Integer id){
return " Thread pool :"+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t"+" Ha ha ha " ;
}
// Failure
public String paymentInfo_TimeOut(Integer id){
int timeNumber = 3;
try {
TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {
e.printStackTrace();}
return " Thread pool :"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+" Purring "+" Time consuming ( second )"+timeNumber;
}
}
2.1.4 Normal test
start-up eureka7001, start-up cloud-provider-hystrix-payment8001
visit :http://localhost:8001/payment/hystrix/ok/31 and http://localhost:8001/payment/hystrix/timeout/31
2.1.5 High concurrency testing
Turn on Jmeter, Come on 30000 A concurrent crush 8001,30000 All requests to visit paymentInfo_TimeOut service
result :ok and timeout Both of these interfaces are going round and round
why? Pressure measurement only timeout Interface ,ok Why does the interface slow down ?
tomcat The default number of worker threads for is full , There are no extra threads to break down the stress and process .
2.2.cloud-consumer-feign-hystrix-order80
2.2.1 Change pom
<dependencies>
<!-- newly added 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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2.2 Change yml
server
port: 80
eureka:
client:
register-with-eureka: true # I don't register myself with the registry
fetch-registry: true # Say you're the registry , The responsibility is to maintain service instances , There's no need to retrieve Services
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
spring:
application:
name: cloud-provider-hystrix-order
2.2.3 Main startup class and business class
@SpringBootApplication
@EnableFeignClients
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
@RestController
@Slf4j
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("*******result:"+result);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_TimeOut(id);
log.info("*******result:"+result);
return result;
}
2.2.4 test
http://localhost/consumer/payment/hystrix/ok/31 and http://localhost/consumer/payment/hystrix/timeout/31
2. 3 service degradation
Service degradation can be done on the server side , It can also be done on the client side .
2.3.1 Server side 8001 service degradation
The main boot class is annotated :@EnableHystrix, The specific method is to add @HystrixCommand
Set the peak value of self call timeout , It can run normally during peak period , It's beyond the need to have a way to deal with it , For service degradation fallback
Once the call to the service method fails and an error message is thrown , Automatically called @HystrixCommand Marked fallbackMethod Call the specified method in the class
@Service
public class PaymentService {
// success
public String paymentInfo_OK(Integer id){
return " Thread pool :"+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t"+" Ha ha ha " ;
}
// Failure
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") //3 Within seconds is the normal business logic
})
public String paymentInfo_TimeOut(Integer id){
// int timeNumber = 5;
int age = 10/0;
// try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
//return " Thread pool :"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+" Purring "+" Time consuming ( second )"+timeNumber;
return " Thread pool :"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+" Purring "+" Time consuming ( second )";
}
// How to cover the bottom
public String paymentInfo_TimeOutHandler(Integer id){
return " Thread pool :"+Thread.currentThread().getName()+" The system is busy , Please try again later ,id: "+id+"\t"+" Cried wow ";
}
}
2.3.2 client 80 service degradation
80 Order micro service , You can also protect yourself better , I also draw gourds to protect the client from degradation
feign:
hystrix:
enabled: true # If you deal with your own fault tolerance, turn it on . The opening method is different from the production end
The main boot class is annotated :@EnableHystrix, The specific method is to add @HystrixCommand
The current problem : Mixed with business logic ??? confusion
according to cloud-consumer-feign-hystrix-order80 Already exist PaymentHystrixService Interface , Create a new class (PaymentFallbackService) Implement the interface , Uniformly handle exceptions for the methods in the interface
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)";
}
}
stay PaymentHystrixService Indicate in fallback The class of is PaymentFallbackService
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
2.3.3 test
summary :
When both the client and the server are configured with service degradation , It is normal for the server to set the timeout time on the client , Will follow the client's demotion logic . conversely , Then go through the server degradation logic .
If a runtime exception occurs on the server , Then the server degradation logic will be followed .
If the server goes down , The client demotion logic will be followed .
边栏推荐
- Super automation and the future of network security
- 【Hot100】2.两数相加
- Hello, is it safe to open an account to buy stocks online?
- 运维-- 统一网关非常必要
- QQ出现大规模盗号,为什么会这样?就没有解决方法了吗?
- WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)
- What you have to know under the digital collection boom
- 【Laravel】关于Laravel8的composer安装
- [force button] 35 Search insert location
- Mysql自連接查詢「建議收藏」
猜你喜欢
运维-- 统一网关非常必要
2022年暑期及9月份CSP-J1 CSP-S1初赛 培训计划及学习要点
leetcode:22. bracket-generating
使用 Open Connector 进行 HubSpot 和 SAP 系统的集成工作
24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我
平台即代码的未来是Kubernetes扩展
防火墙基础之流量管理与控制
#夏日挑战赛#OHOS构建自定义服务实战
【Hot100】1. Sum of two numbers
[MySQL] official website document learning query statement SQL precautions
随机推荐
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
使用Karmada实现Helm应用的跨集群部署
Traffic management and control of firewall Foundation
Steps to be taken for successful migration to the cloud
【Hot100】4. Find the median of two positive arrays
Tongziping, partner of Tongchuang Weiye: "what should yuan universe invest in?"
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels
【Hot100】2. Add two numbers
Interviewer: how does the thread pool reuse threads? Do you know? Tell me about it
WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)
PostgreSQL异常处理
The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
Internet of things cloud convergence Security Guide
O & M - unified gateway is very necessary
Gartner announces five privacy trends from today to 2024
Summer Challenge ohos build custom service practice
运维-- 统一网关非常必要
知道这几个命令让你掌握Shell自带工具
Have you ever encountered the error that the main key of this setting is consistent with the database?
#夏日挑战赛#OHOS构建自定义服务实战