国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)

這篇具有很好參考價值的文章主要介紹了微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前幾章,我們講解了Eureka注冊中心、Ribbon和OpenFeign服務調(diào)用框架;今天開始講一個分布式微服務項目中很重要的內(nèi)容Hytrix服務降級框架。
微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)

盡管Hytrix官網(wǎng)停止更新了,但是Hytrix的設計理念和思想非常優(yōu)秀,其他服務降級框架的設計都是借鑒于它,可以說它是所有分布式微服務項目中服務降級使用的基石。
微服務+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

哪些情況會發(fā)出降級:

  • 程序運行異常
  • 超時
  • 服務熔斷觸發(fā)服務降級
  • 線程池/信號量也會導致服務降級
  1. 服務熔斷break

當某個服務出現(xiàn)問題,卡死了,不能讓用戶一直等待,需要關(guān)閉所有對此服務的訪問,然后調(diào)用服務降級

類比保險絲達到最大服務訪問后,直接拒絕訪問,拉閘限電,然后調(diào)用服務降級的方法并返回友好提示

就是保險絲:服務的降級->進而熔斷->恢復調(diào)用鏈路

  1. 服務限流flowlimit

限流,比如秒殺場景,不能訪問用戶瞬間都訪問服務器,限制一次只可以有多少請求

秒殺高并發(fā)等操作,嚴禁一窩蜂的過來擁擠,大家排隊,一秒鐘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. 業(yè)務類
    寫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實現(xiàn)類:

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個并發(fā)請求http://localhost:8001/payment/hystrix/error/1

  1. jmeter壓力測試設置
    微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)

微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)
再次壓測2萬并發(fā),發(fā)現(xiàn)http://localhost:8001/payment/hystrix/ok/1訪問也變慢了,開始轉(zhuǎn)圈圈了。

此時使用壓測工具,并發(fā)20000個請求,請求會延遲的那個方法,
        壓測中,發(fā)現(xiàn),另外一個方法并沒有被壓測,但是我們訪問它時,卻需要等待
        這就是因為被壓測的方法它占用了服務器大部分資源,導致其他請求也變慢了

讓問題和錯誤更嚴重,建一個80消費模塊去訪問8001,80模塊感覺很崩潰~~~~~~~~

3.2 創(chuàng)建帶降級的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. 遠程調(diào)用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萬并發(fā),發(fā)現(xiàn)80訪問也變慢了

微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)
微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)
此時使用壓測工具,并發(fā)20000個請求,請求會延遲的那個方法,
壓測中,發(fā)現(xiàn),另外一個方法并沒有被壓測,但是我們訪問它時,卻需要等待
這就是因為被壓測的方法它占用了服務器大部分資源,導致其他請求也變慢了
微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)

3.3 配置服務降級:
3.3.1 服務降級 Cloud-provider-hystrix-payment8001模塊

8001先從自身找問題,設置自身調(diào)用超時時間的峰值,峰值內(nèi)可以正常運行,超過了需要有兜底的方法處理,做服務降級fallback。

具體配置步驟如下:

  1. 為service的指定方法(會延遲的方法)添加@HystrixCommand注解

一旦調(diào)用服務方法失敗并拋出錯誤信息后,會自動調(diào)用 @HystrixCommand標注好的fallbackMethod調(diào)用類中的指定方法

超過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)

結(jié)論:
上面故意制造兩個異常:
1,int timeout = 5;
2,我們能接受3秒鐘,它運行5秒鐘,超時異常

當前服務不可用了,做服務降級,兜底的方案都是方法 getPaymentInfo_ErrorHandler。
這是8001對自己做的保護,遇到異常做服務降級,進入兜底方法。

3.3.2 服務降級Cloud-comsumer-feign-hystrix-order80模塊

首先80模塊也要有個服務自我保護的機制,也需要服務降級保護

然后,80通過feign調(diào)用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. 業(yè)務類

主要意思是: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中的假設業(yè)務超時時間3000毫秒,允許3秒內(nèi)不進行服務降級

第三個時間是:80模塊的自我保護服務降級峰值時間1500毫秒。

3.3.3 Hystrix全局服務降級DefaultProperties

上述案例中,我們發(fā)現(xiàn)每個方法都需要服務降級的兜底方法,這樣代碼顯得很膨脹,業(yè)務重復的問題。

統(tǒng)一與自定義的分開。

微服務+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方法  注意 這個全局兜底方法不能攜帶參數(shù)  這是我遇到的坑*/
    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調(diào)用的接口上都有@FeignClient注解,換句話說:標有@FeignClient注解的接口中的所有方法做個統(tǒng)一的服務降級類,就可以實現(xiàn)接口服務的統(tǒng)一服務降級。

具體步驟:

  1. 新建一個類PaymentFallbackService實現(xiàn) 標注有@FeignClient注解的接口
    統(tǒng)一為接口里面的方法進行異常處理。
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)
然后,關(guān)閉8001服務
再次訪問上述地址

微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)
此時服務端已經(jīng)宕機了,但是我們做了服務降級處理,讓客戶端在服務端不可用時也會獲得提示信息而不會掛起耗死服務器。

3.4 服務熔斷CircuitBreaker

熔斷機制是應對雪崩效應的一種微服務鏈路保護機制。

保險絲

在springcloud框架中,熔斷機制通過Hystrix實現(xiàn),Hystrix會健康微服務間調(diào)用的狀況
當失敗的調(diào)用到一定閾值,缺省是5秒內(nèi)20次調(diào)用的失敗,就會啟動熔斷機制。熔斷機制的注解是@HystrixCommand。

4.4.1 服務熔斷理論

微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)
三個狀態(tài) closed、open、halfopen(開、關(guān)、半開)
微服務+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"), //請求次數(shù)
            @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不能為負數(shù)");
        }
        String simpleUUID = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t" + "成功調(diào)用,流水號是:" + simpleUUID;
    }

    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) {
        return "id不能為負數(shù),請稍后再試............"+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次以上的設置

此時,連續(xù)訪問-1 20多次,然后再訪問1時,發(fā)現(xiàn)剛開始調(diào)用鏈路還沒有恢復,等幾秒鐘后才訪問正常。
多次錯誤,然后慢慢正確,發(fā)現(xiàn)剛開始不滿足條件,隨著錯誤率下降后,才恢復正常。

服務的降級------進而熔斷----恢復調(diào)用鏈路

4.4.3 服務熔斷總結(jié)

微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)
當斷路器開啟后:
微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)

熔斷的整體流程:

請求進來,首先查詢緩存,如果緩存有,直接返回
如果緩存沒有,--->2
查看斷路器是否開啟,如果開啟的,Hystrix直接將請求轉(zhuǎn)發(fā)到降級返回,然后返回
如果斷路器是關(guān)閉的,
判斷線程池等資源是否已經(jīng)滿了,如果已經(jīng)滿了
也會走降級方法
如果資源沒有滿,判斷我們使用的什么類型的Hystrix,決定調(diào)用構(gòu)造方法還是run方法
然后處理請求
然后Hystrix將本次請求的結(jié)果信息匯報給斷路器,因為斷路器此時可能是開啟的
(因為斷路器開啟也是可以接收請求的)
斷路器收到信息,判斷是否符合開啟或關(guān)閉斷路器的條件,
如果本次請求處理失敗,又會進入降級方法
如果處理成功,判斷處理是否超時,如果超時了,也進入降級方法
最后,沒有超時,則本次請求處理成功,將結(jié)果返回給controller

其他參數(shù)
微服務+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)文章來源地址http://www.zghlxwxcb.cn/news/detail-419688.html

到了這里,關(guān)于微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • SpringCloud Alibaba微服務-- Sentinel的使用(保姆級)

    SpringCloud Alibaba微服務-- Sentinel的使用(保姆級)

    隨著微服務的流行,服務和服務之間的穩(wěn)定性變得越來越重要。Sentinel 以流量為切入點,從流量控制、熔斷降級、系統(tǒng)負載保護等多個維度保護服務的穩(wěn)定性。 1、sentinel的特征 豐富的應用場景 : Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突

    2024年02月16日
    瀏覽(22)
  • 【springcloud 微服務】Spring Cloud Alibaba Nacos使用詳解

    目錄 一、前言 二、nacos介紹 2.1??什么是 Nacos 2.2 nacos 核心能力 2.2.1 服務發(fā)現(xiàn)和服務健康監(jiān)測

    2024年01月22日
    瀏覽(26)
  • 【springcloud 微服務】Spring Cloud Alibaba Sentinel使用詳解

    目錄 一、前言 二、分布式系統(tǒng)遇到的問題 2.1 服務可用性問題 2.1.1? 單點故障

    2024年01月16日
    瀏覽(22)
  • 【SpringCloud Alibaba】(四)使用 Feign 實現(xiàn)服務調(diào)用的負載均衡

    【SpringCloud Alibaba】(四)使用 Feign 實現(xiàn)服務調(diào)用的負載均衡

    在上一文中,我們實現(xiàn)了服務的自動注冊與發(fā)現(xiàn)功能。但是還存在一個很明顯的問題:如果用戶微服務和商品微服務在服務器上部署多份的話,之前的程序無法實現(xiàn)服務調(diào)用的負載均衡功能。 本文就帶著大家一起實現(xiàn)服務調(diào)用的負載均衡功能 負載均衡:將原本由一臺服務器

    2024年02月15日
    瀏覽(28)
  • 阿里巴巴最新SpringCloud Alibaba全彩版筆記開源,架構(gòu)師帶你手擼微服務結(jié)構(gòu)項目實戰(zhàn)

    阿里巴巴最新SpringCloud Alibaba全彩版筆記開源,架構(gòu)師帶你手擼微服務結(jié)構(gòu)項目實戰(zhàn)

    Spring Cloud Alibaba 致力于提供微服務開發(fā)的一站式解決方案。此項目包含開發(fā)分布式應用微服務的必需組件,依托Spring Cloud Alibaba,只需要添加一些注解和少量配置,就可以將Spring Cloud 應用接入阿里微服務解決方案,通過阿里中間件來迅速搭建分布式應用系統(tǒng)。 下面這些都是

    2024年02月04日
    瀏覽(30)
  • 【SpringCloud Alibaba】(六)使用 Sentinel 實現(xiàn)服務限流與容錯

    【SpringCloud Alibaba】(六)使用 Sentinel 實現(xiàn)服務限流與容錯

    今天,我們就使用 Sentinel 實現(xiàn)接口的限流,并使用 Feign 整合 Sentinel 實現(xiàn)服務容錯的功能,讓我們體驗下微服務使用了服務容錯功能的效果。 因為內(nèi)容僅僅圍繞著 SpringCloud Alibaba技術(shù)棧展開,所以,這里我們使用的服務容錯組件是阿里開源的 Sentinel。 當然,能夠?qū)崿F(xiàn)服務容錯

    2024年02月14日
    瀏覽(23)
  • 深入學習SpringCloud Alibaba微服務架構(gòu),揭秘Nacos、Sentinel、Seata等核心技術(shù),助力構(gòu)建高效系統(tǒng)!

    鏈接: https://pan.baidu.com/s/1hRN0R8VFcwjyCTWCEsz-8Q?pwd=j6ej 提取碼: j6ej 復制這段內(nèi)容后打開百度網(wǎng)盤手機App,操作更方便哦 --來自百度網(wǎng)盤超級會員v4的分享 ??【第01階段】課程簡介:全面介紹課程內(nèi)容,為你提供學習引導和目標規(guī)劃,讓你快速進入學習狀態(tài)!?? ??【第02階段】基

    2024年02月12日
    瀏覽(38)
  • SpringCloud(四)Hystrix服務降級、熔斷、監(jiān)控頁面

    SpringCloud(四)Hystrix服務降級、熔斷、監(jiān)控頁面

    官方文檔:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.5.RELEASE/single/spring-cloud-netflix.html#_circuit_breaker_hystrix_clients 我們知道,微服務之間是可以進行相互調(diào)用的,那么如果出現(xiàn)了下面的情況會導致什么問題? 由于位于最底端的服務提供者E發(fā)生故障,那么此時會直接導

    2024年02月17日
    瀏覽(26)
  • SpringCloud-Hystrix服務熔斷與降級工作原理&源碼

    在微服務架構(gòu)中,根據(jù)業(yè)務來拆分成一個個的服務,服務與服務之間可以相互調(diào)用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調(diào)用。為了保證其高可用,單個服務通常會集群部署。由于網(wǎng)絡原因或者自身的原因,服務并不能保證100%可用,如果單個服務出現(xiàn)問題,調(diào)用這

    2024年02月14日
    瀏覽(24)
  • springcloud3 hystrix實現(xiàn)服務降級的案例配置2

    springcloud3 hystrix實現(xiàn)服務降級的案例配置2

    \\\"服務器忙,請稍后在試\\\"不讓客戶達等待,立即返回一個友好的提示。 1.程序運行異常; 2.超時; 3.服務熔斷觸發(fā)服務降級; 4.線程池/信號量打滿也會導致服務降級 2.1.1 pom文件 2.1.2 設置降級規(guī)則 代碼 ?2.1.3 開啟hystrix熔斷 添加:@EnableHystrix 注解 2.2.1 pom文件 2.2.2?設置降級規(guī)

    2024年02月12日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包