SpringCloud第七節內容精簡(下),Hystrix斷路器和服務熔斷

服務的降級->再到熔斷 ->恢復調用鏈路

當檢測到響應正常後,恢復。

 

修改 8001項目的service層。窗口期10秒,10秒內錯誤達到一定次數接下來輸入對的也會失敗

package com.atguigu.springcloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: zhuangzibing
 * Date: 2020/7/1
 */
@Service
public class PaymentService {
    public String paymentInfo_Ok(Integer id){
        return "線程池:"+Thread.currentThread().getName()+"paymentInfo_ok,id:"+id;
    }


    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000")
    })
    public String paymentInfo_TimeOut(Integer id){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "線程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOut,id:"+id+"耗時5秒";
    }

    public String paymentInfo_TimeOutHandler(Integer id){
        return "線程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOutHandler,id:"+id+"攔截方法";
    }



    //=====服務熔斷
    @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 serialNumber = IdUtil.simpleUUID();

        return Thread.currentThread().getName()+"\t"+"調用成功,流水號: " + serialNumber;
    }
    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id)
    {
        return "id 不能負數,請稍後再試,/(ㄒoㄒ)/~~   id: " +id;
    }

}

控制器加多一個方法

//服務熔斷
    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String result = paymentService.paymentCircuitBreaker(id);
        log.info("******result"+result);
        return result;
    }

啓動類 

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class,args);
    }
}

效果圖。輸入-1後返回錯誤信息。然後重複刷新,接下里輸入正確的也會熔斷

 

可以參考所有配置

https://blog.csdn.net/bailaoshi666/article/details/107087268

 

服務限流在阿里巴巴的Sentinel說明

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章