微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

这篇具有很好参考价值的文章主要介绍了微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前几章,我们讲解了Eureka注册中心、Ribbon和OpenFeign服务调用框架;今天开始讲一个分布式微服务项目中很重要的内容Hytrix服务降级框架。
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

尽管Hytrix官网停止更新了,但是Hytrix的设计理念和思想非常优秀,其他服务降级框架的设计都是借鉴于它,可以说它是所有分布式微服务项目中服务降级使用的基石。
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

1、Hystrix熔断器概述

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

2、HyStrix重要概念

  1. 服务降级fallback

比如当某个服务繁忙,不能让客户端的请求一直等待,应该立刻返回给客户端一个备选方案(兜底的服务)
服务器忙,请稍后再试,不让客户端等待并立刻返回一个友好提示,fallback

哪些情况会发出降级:

  • 程序运行异常
  • 超时
  • 服务熔断触发服务降级
  • 线程池/信号量也会导致服务降级
  1. 服务熔断break

当某个服务出现问题,卡死了,不能让用户一直等待,需要关闭所有对此服务的访问,然后调用服务降级

类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示

就是保险丝:服务的降级->进而熔断->恢复调用链路

  1. 服务限流flowlimit

限流,比如秒杀场景,不能访问用户瞬间都访问服务器,限制一次只可以有多少请求

秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行。

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

3、hystrix案例

3.1 新建模块 Cloud-provider-hystrix-payment8001
  1. 建模块 Cloud-provider-hystrix-payment8001
    微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

  1. 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>springcloud2023</artifactId>
        <groupId>com.tigerhhzz.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.tigerhhzz.springcloud-tigerhhzz</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>
  1. yml文件
server:
  port: 8001

spring:
  application:
    name: cloud-provider-hystrix-service

eureka:
  client:
    service-url:
      #      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
      defaultZone: http://eureka7001.com:7001/eureka/

  1. 主启动类
package com.tigerhhzz.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author tigerhhzz
 * @date 2023/4/12 9:41
 */
@SpringBootApplication
@EnableEurekaClient
@Slf4j
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class,args);
        log.info("PaymentHystrixMain8001启动成功~~~~~~~~~~~~~~~~~~~");
    }
}

  1. 业务类
    写service:
package com.tigerhhzz.springcloud.service;


/**
 * @author tigerhhzz
 * @date 2022/6/15 9:16
 */
public interface PaymentService {
    String getPaymentInfo_OK(Integer id);

    String getPaymentInfo_Error(Integer id);


}

service实现类:

package com.tigerhhzz.springcloud.service.impl;

import com.tigerhhzz.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:18
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Override
    public String getPaymentInfo_OK(Integer id) {
        return "当前线程池名称: "+Thread.currentThread().getName()+"----getPaymentInfo_OK----,id: "+id;
    }



    @Override
    public String getPaymentInfo_Error(Integer id) {
        //System.out.println(Thread.currentThread().getName()+"-----------error----------"+id);
        int timeout = 3;
        //int age = 10/0;
        try {
            TimeUnit.SECONDS.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "当前线程池名称: "+Thread.currentThread().getName()+"-----getPaymentInfo_Error----ERROR----"+id;
    }




}

controller:

package com.tigerhhzz.springcloud.controller;


import com.tigerhhzz.springcloud.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;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:19
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String getPaymentInfo_OK(@PathVariable("id") Integer id){
        String res = paymentService.getPaymentInfo_OK(id);
        return res;
    }
    @GetMapping("/payment/hystrix/error/{id}")
    public String getPaymentInfo_Error(@PathVariable("id") Integer id){
        String res = paymentService.getPaymentInfo_Error(id);
        return res;
    }



}

启动7001和8001模块,测试正常
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

以上述为根基平台,从正确–错误–降级熔断–恢复。

通过jmeter压力测试 20000个并发请求http://localhost:8001/payment/hystrix/error/1

  1. jmeter压力测试设置
    微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
再次压测2万并发,发现http://localhost:8001/payment/hystrix/ok/1访问也变慢了,开始转圈圈了。

此时使用压测工具,并发20000个请求,请求会延迟的那个方法,
        压测中,发现,另外一个方法并没有被压测,但是我们访问它时,却需要等待
        这就是因为被压测的方法它占用了服务器大部分资源,导致其他请求也变慢了

让问题和错误更严重,建一个80消费模块去访问8001,80模块感觉很崩溃~~~~~~~~

3.2 创建带降级的order模块 Cloud-comsumer-feign-hystrix-order80
  1. 建模块
    微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

  2. 修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>springcloud2023</artifactId>
        <groupId>com.tigerhhzz.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Cloud-comsumer-feign-hystrix-order80</artifactId>

    <dependencies>
        <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>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.tigerhhzz.springcloud-tigerhhzz</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>
  1. 改yml
server:
  port: 80

spring:
  application:
    name: cloud-comsumer-feign-hystrix-order80


eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  1. 主启动类
package com.tigerhhzz.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:56
 */
@SpringBootApplication
@EnableFeignClients
@Slf4j
public class OrderFeignHystrixMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignHystrixMain80.class,args);
        log.info("OrderFeignHystrixMain80启动成功~~~~~~~~~~~~~~~~~~~");
    }
}

  1. 远程调用8001模块的接口
package com.tigerhhzz.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:59
 *
 * 微服务接口 + @feign注解
 */
@Component
@FeignClient(value = "cloud-provider-hystrix-service")
public interface PaymentHystrixService {

    @GetMapping("/payment/hystrix/ok/{id}")
    public String getPaymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/error/{id}")
    public String getPaymentInfo_Error(@PathVariable("id") Integer id);
}

  1. controller
package com.tigerhhzz.springcloud.controller;


import com.tigerhhzz.springcloud.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;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:19
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String getPaymentInfo_OK(@PathVariable("id") Integer id){
        String res = paymentService.getPaymentInfo_OK(id);
        return res;
    }
    @GetMapping("/consumer/payment/hystrix/error/{id}")
    public String getPaymentInfo_Error(@PathVariable("id") Integer id){
        String res = paymentService.getPaymentInfo_Error(id);
        return res;
    }


}

  1. 测试

启动80模块,访问8001,正常。

再次压测2万并发,发现80访问也变慢了

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
此时使用压测工具,并发20000个请求,请求会延迟的那个方法,
压测中,发现,另外一个方法并没有被压测,但是我们访问它时,却需要等待
这就是因为被压测的方法它占用了服务器大部分资源,导致其他请求也变慢了
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

3.3 配置服务降级:
3.3.1 服务降级 Cloud-provider-hystrix-payment8001模块

8001先从自身找问题,设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,做服务降级fallback。

具体配置步骤如下:

  1. 为service的指定方法(会延迟的方法)添加@HystrixCommand注解

一旦调用服务方法失败并抛出错误信息后,会自动调用 @HystrixCommand标注好的fallbackMethod调用类中的指定方法

超过3秒,fallback服务降级。

package com.tigerhhzz.springcloud.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.tigerhhzz.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:18
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Override
    public String getPaymentInfo_OK(Integer id) {
        return "当前线程池名称: "+Thread.currentThread().getName()+"----getPaymentInfo_OK----,id: "+id;
    }



    @Override
    @HystrixCommand(fallbackMethod = "getPaymentInfo_ErrorHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String getPaymentInfo_Error(Integer id) {
        //System.out.println(Thread.currentThread().getName()+"-----------error----------"+id);
        int timeout = 5;
        //int age = 10/0;
        try {
            TimeUnit.SECONDS.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "当前线程池名称: "+Thread.currentThread().getName()+"-----getPaymentInfo_Error----ERROR----"+id;
    }


    /*fallback服务降级后的兜底方法*/
    public String getPaymentInfo_ErrorHandler(Integer id) {
        return Thread.currentThread().getName()+"-----8001 allback服务降级后的兜底方法----超时或异常------getPaymentInfo_ErrorHandler----ERROR----"+id;
    }




}

getPaymentInfo_ErrorHandler服务降级后的兜底方法

  1. 主启动类上,添加激活hystrix的注解
package com.tigerhhzz.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author tigerhhzz
 * @date 2023/4/12 9:41
 */
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@Slf4j
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class,args);
        log.info("PaymentHystrixMain8001启动成功~~~~~~~~~~~~~~~~~~~");
    }
}

先测试8001的自身加固,有没有效果?

访问:http://localhost:8001/payment/hystrix/error/1
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

结论:
上面故意制造两个异常:
1,int timeout = 5;
2,我们能接受3秒钟,它运行5秒钟,超时异常

当前服务不可用了,做服务降级,兜底的方案都是方法 getPaymentInfo_ErrorHandler。
这是8001对自己做的保护,遇到异常做服务降级,进入兜底方法。

3.3.2 服务降级Cloud-comsumer-feign-hystrix-order80模块

首先80模块也要有个服务自我保护的机制,也需要服务降级保护

然后,80通过feign调用8001时

  1. 修改80的yml配置

添加在feign中开启hystrix的配置

server:
  port: 80

spring:
  application:
    name: cloud-comsumer-feign-hystrix-order80


eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
feign:
  hystrix:
    enabled: true  #在feign中开启hystrix
  1. 改80启动类

在启动类中加上注解@EnableHystrix

package com.tigerhhzz.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:56
 */
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@Slf4j
public class OrderFeignHystrixMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignHystrixMain80.class,args);
        log.info("OrderFeignHystrixMain80启动成功~~~~~~~~~~~~~~~~~~~");
    }
}

  1. 业务类

主要意思是:80模块中的paymentInfo_ERROR接口也自身做个hystrix服务降级兜底方法。并设置超时时间峰值是1500毫秒。


    @GetMapping("/consumer/payment/hystrix/error/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String paymentInfo_ERROR(@PathVariable("id") Integer id){
        //int age= 10/0;
        return paymentHystrixService.getPaymentInfo_Error(id);
    }

    public String paymentInfo_ERROR_Handler(@PathVariable("id") Integer id){
        return "80____paymentInfo_ERROR_Handler___异常返回,请检查自己";
    }

测试,访问地址:http://localhost/consumer/payment/hystrix/error/1
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
上面有三个时间,需要注意一下。

第一个时间是:8001模块getPaymentInfo_Error接口的峰值时间5000毫秒(8001自我保护的服务降级峰值时间)

第二个时间是:8001模块getPaymentInfo_Error中的假设业务超时时间3000毫秒,允许3秒内不进行服务降级

第三个时间是:80模块的自我保护服务降级峰值时间1500毫秒。

3.3.3 Hystrix全局服务降级DefaultProperties

上述案例中,我们发现每个方法都需要服务降级的兜底方法,这样代码显得很膨胀,业务重复的问题。

统一与自定义的分开。

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
案例是在80模块的controller中设置如下:

package com.tigerhhzz.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.tigerhhzz.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @author tigerhhzz
 * @date 2022/6/15 10:03
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "Payment_Global_Fallback_Handler")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String getPaymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentHystrixService.getPaymentInfo_OK(id);
    }

    @GetMapping("/consumer/payment/hystrix/error/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String paymentInfo_ERROR(@PathVariable("id") Integer id){
        //int age= 10/0;
        return paymentHystrixService.getPaymentInfo_Error(id);
    }

    public String paymentInfo_ERROR_Handler(@PathVariable("id") Integer id){
        return "80____paymentInfo_ERROR_Handler___异常返回,请检查自己";
    }

    /*全局fallback方法  注意 这个全局兜底方法不能携带参数  这是我遇到的坑*/
    public String Payment_Global_Fallback_Handler(){
        return "80____Payment_Global_Fallback_Handler___异常返回";
    }

}

这里我新加了一个接口方法:

    @GetMapping("/consumer/payment/hystrix/error1/{id}")
    @HystrixCommand
    public String paymentInfo_ERROR1(@PathVariable("id") Integer id){
        int age= 10/0;
        return paymentHystrixService.getPaymentInfo_Error(id);
    }

测试访问地址:http://localhost/consumer/payment/hystrix/error1/1

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
进入全局服务降级的兜底方法。

3.3.3 Hystrix通配服务降级FeignFallback

微服务中,只要通过feign调用的接口上都有@FeignClient注解,换句话说:标有@FeignClient注解的接口中的所有方法做个统一的服务降级类,就可以实现接口服务的统一服务降级。

具体步骤:

  1. 新建一个类PaymentFallbackService实现 标注有@FeignClient注解的接口
    统一为接口里面的方法进行异常处理。
package com.tigerhhzz.springcloud.service;

import org.springframework.stereotype.Component;

/**
 * @author tigerhhzz
 * @date 2022/6/15 19:16
 */
@Component
public class PaymentFallbackService implements PaymentHystrixService{
    @Override
    public String getPaymentInfo_OK(Integer id) {
        return "80---PaymentFallbackService----payment/hystrix/ok";
    }

    @Override
    public String getPaymentInfo_Error(Integer id) {

        return "80----PaymentFallbackService---payment/hystrix/error";
    }

}

  1. 修改80中的feign接口PaymentHystrixService

添加@FeignClient(value = “cloud-provider-hystrix-service”,fallback = PaymentFallbackService.class)

package com.tigerhhzz.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author tigerhhzz
 * @date 2022/6/15 9:59
 *
 * 微服务接口 + @feign注解
 */
@Component
@FeignClient(value = "cloud-provider-hystrix-service",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {


    @GetMapping("/payment/hystrix/ok/{id}")
    public String getPaymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/error/{id}")
    public String getPaymentInfo_Error(@PathVariable("id") Integer id);
}
  1. 测试访问:
    首先访问http://localhost/consumer/payment/hystrix/ok/1

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
然后,关闭8001服务
再次访问上述地址

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
此时服务端已经宕机了,但是我们做了服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器。

3.4 服务熔断CircuitBreaker

熔断机制是应对雪崩效应的一种微服务链路保护机制。

保险丝

在springcloud框架中,熔断机制通过Hystrix实现,Hystrix会健康微服务间调用的状况
当失败的调用到一定阈值,缺省是5秒内20次调用的失败,就会启动熔断机制。熔断机制的注解是@HystrixCommand。

4.4.1 服务熔断理论

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
三个状态 closed、open、halfopen(开、关、半开)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

4.4.2 服务熔断案例

修改Cloud-provider-hystrix-payment8001模块

  1. PaymentServiceImpl:
    //---服务的熔断
    @Override
    @HystrixCommand(
            fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//失败率达到多少后跳闸

    }
    )
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        if (id<0) {
            throw new RuntimeException("******id不能为负数");
        }
        String simpleUUID = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t" + "成功调用,流水号是:" + simpleUUID;
    }

    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) {
        return "id不能为负数,请稍后再试............"+id;
    }
  1. PaymentController
    //服务熔断
    @GetMapping("info/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String res = paymentService.paymentCircuitBreaker(id);
        log.info("-----res:" +res);
        return res;
    }

  1. 测试
    自测Cloud-provider-hystrix-payment8001 启动

正确 http://localhost:8001/info/circuit/1

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

错误 http://localhost:8001/info/circuit/-1
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

一次正确一次错误,远远没有达到访问10次失败率6次以上的设置

此时,连续访问-1 20多次,然后再访问1时,发现刚开始调用链路还没有恢复,等几秒钟后才访问正常。
多次错误,然后慢慢正确,发现刚开始不满足条件,随着错误率下降后,才恢复正常。

服务的降级------进而熔断----恢复调用链路

4.4.3 服务熔断总结

微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
当断路器开启后:
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)

熔断的整体流程:

请求进来,首先查询缓存,如果缓存有,直接返回
如果缓存没有,--->2
查看断路器是否开启,如果开启的,Hystrix直接将请求转发到降级返回,然后返回
如果断路器是关闭的,
判断线程池等资源是否已经满了,如果已经满了
也会走降级方法
如果资源没有满,判断我们使用的什么类型的Hystrix,决定调用构造方法还是run方法
然后处理请求
然后Hystrix将本次请求的结果信息汇报给断路器,因为断路器此时可能是开启的
(因为断路器开启也是可以接收请求的)
断路器收到信息,判断是否符合开启或关闭断路器的条件,
如果本次请求处理失败,又会进入降级方法
如果处理成功,判断处理是否超时,如果超时了,也进入降级方法
最后,没有超时,则本次请求处理成功,将结果返回给controller

其他参数
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)
微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)文章来源地址https://www.toymoban.com/news/detail-419688.html

到了这里,关于微服务+springcloud+springcloud alibaba学习笔记【Hystrix(豪猪哥)的使用】(6/9)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。 1、sentinel的特征 丰富的应用场景 : Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突

    2024年02月16日
    浏览(29)
  • 【SpringCloud Alibaba】(四)使用 Feign 实现服务调用的负载均衡

    在上一文中,我们实现了服务的自动注册与发现功能。但是还存在一个很明显的问题:如果用户微服务和商品微服务在服务器上部署多份的话,之前的程序无法实现服务调用的负载均衡功能。 本文就带着大家一起实现服务调用的负载均衡功能 负载均衡:将原本由一台服务器

    2024年02月15日
    浏览(30)
  • 【springcloud 微服务】Spring Cloud Alibaba Sentinel使用详解

    目录 一、前言 二、分布式系统遇到的问题 2.1 服务可用性问题 2.1.1  单点故障

    2024年01月16日
    浏览(41)
  • 【springcloud 微服务】Spring Cloud Alibaba Nacos使用详解

    目录 一、前言 二、nacos介绍 2.1  什么是 Nacos 2.2 nacos 核心能力 2.2.1 服务发现和服务健康监测

    2024年01月22日
    浏览(42)
  • 阿里巴巴最新SpringCloud Alibaba全彩版笔记开源,架构师带你手撸微服务结构项目实战

    Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,依托Spring Cloud Alibaba,只需要添加一些注解和少量配置,就可以将Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。 下面这些都是

    2024年02月04日
    浏览(46)
  • 【SpringCloud Alibaba】(六)使用 Sentinel 实现服务限流与容错

    今天,我们就使用 Sentinel 实现接口的限流,并使用 Feign 整合 Sentinel 实现服务容错的功能,让我们体验下微服务使用了服务容错功能的效果。 因为内容仅仅围绕着 SpringCloud Alibaba技术栈展开,所以,这里我们使用的服务容错组件是阿里开源的 Sentinel。 当然,能够实现服务容错

    2024年02月14日
    浏览(34)
  • 深入学习SpringCloud Alibaba微服务架构,揭秘Nacos、Sentinel、Seata等核心技术,助力构建高效系统!

    链接: https://pan.baidu.com/s/1hRN0R8VFcwjyCTWCEsz-8Q?pwd=j6ej 提取码: j6ej 复制这段内容后打开百度网盘手机App,操作更方便哦 --来自百度网盘超级会员v4的分享 📚【第01阶段】课程简介:全面介绍课程内容,为你提供学习引导和目标规划,让你快速进入学习状态!💡 🔍【第02阶段】基

    2024年02月12日
    浏览(41)
  • SpringCloud(四)Hystrix服务降级、熔断、监控页面

    官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.5.RELEASE/single/spring-cloud-netflix.html#_circuit_breaker_hystrix_clients 我们知道,微服务之间是可以进行相互调用的,那么如果出现了下面的情况会导致什么问题? 由于位于最底端的服务提供者E发生故障,那么此时会直接导

    2024年02月17日
    浏览(41)
  • SpringCloud-Hystrix服务熔断与降级工作原理&源码

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这

    2024年02月14日
    浏览(43)
  • springcloud3 hystrix实现服务降级的案例配置2

    \\\"服务器忙,请稍后在试\\\"不让客户达等待,立即返回一个友好的提示。 1.程序运行异常; 2.超时; 3.服务熔断触发服务降级; 4.线程池/信号量打满也会导致服务降级 2.1.1 pom文件 2.1.2 设置降级规则 代码  2.1.3 开启hystrix熔断 添加:@EnableHystrix 注解 2.2.1 pom文件 2.2.2 设置降级规

    2024年02月12日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包