Hystrix的服務降級配置


Hystrix的服務降級,既可以配置在服務提供端,也可以配置在服務調用端,

但一般來說,配置在服務調用端!

服務提供者端:

1)業務類 : 添加fallback方法

@Service
public class PaymentService {


    public String paymentInfo_ok(Integer id){
        return "線程池:"+Thread.currentThread().getName()+
                " paymentInfo_ok,id:"+id+" O(∩_∩)O哈哈~";
    }


    @HystrixCommand(fallbackMethod = "paymentInfo_timeout_handler",
    commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000") //程序運行超過3s, 就會進行服務降級
    })
    public String paymentInfo_timeout(Integer id){
        int timenumber=5;
        try {
            TimeUnit.SECONDS.sleep(timenumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "線程池:"+Thread.currentThread().getName()+
                " paymentInfo_timeout,id:"+id+" 耗時3秒!!";
    }

    public String paymentInfo_timeout_handler(Integer id){
        return "超時+兜底+ o(╥﹏╥)o";
    }

}

 

2)主啟動類 :添加@EnableCircuitBreaker注解

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

 

服務消費者端:

1)業務類

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

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


    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
    //@HystrixCommand
    public String paymentInfo_timeout(@PathVariable("id") Integer id)
    {
        int age = 10/0;
        String result = paymentHystrixService.paymentInfo_timeout(id);
        return result;
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
    {
        return "我是消費者80,對方支付系統繁忙請10秒鍾后再試或者自己運行出錯請檢查自己,o(╥﹏╥)o";
    }

    // 下面是全局fallback方法
    public String payment_Global_FallbackMethod()
    {
        return "Global異常處理信息,請稍后再試,/(ㄒoㄒ)/~~";
    }
}

2)主啟動類

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class ConsumerFeignHystrixMian80 {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerFeignHystrixMian80.class,args);
    }
}

 

 

Hystrix之全局服務降級DefaultProperties

出現的問題:

每個業務方法對應一個兜底的方法,代碼碰撞

錯誤處理的方法和業務代碼混淆在一塊

解決

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

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


    @HystrixCommand
    public String paymentInfo_timeout(@PathVariable("id") Integer id)
    {
        int age = 10/0;
        String result = paymentHystrixService.paymentInfo_timeout(id);
        return result;
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
    {
        return "我是消費者80,對方支付系統繁忙請10秒鍾后再試或者自己運行出錯請檢查自己,o(╥﹏╥)o";
    }

    // 下面是全局fallback方法
    public String payment_Global_FallbackMethod()
    {
        return "Global異常處理信息,請稍后再試,/(ㄒoㄒ)/~~";
    }
}

 

Hystrix之通配服務降級FeignFallback

【由Feign來處理服務降級】

1)定義 fullback類

 

2)在Feign注解上進行配置

@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback =PaymentHystrixServiceFullbackImpl.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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM