SpringCloud系列(32)--使用Hystrix进行全局服务降级

前言:在上一节中我们使用Hystrix进行了服务降级,但是要在每个方法上面配置@HystrixCommand才能实现服务降级,如果需要进行服务降级的方法多了,@HystrixCommand也就得配置很多遍,所以本节内容则是使用Hystrix进行了全局服务降级处理。

1、修改cloud-consumer-feign-hystrix-order80子模块的OrderHystrixController类
java 复制代码
package com.ken.springcloud.controller;

import com.ken.springcloud.service.PaymentHystrixService;
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 org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@DefaultProperties(defaultFallback = "paymentGlobalFallBackMethod")
public class OrdertHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

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

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    //一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法,这里设置服务降级的条件为连接超时超过3秒,即3秒内走paymentInfoTimeOut方法内的业务逻辑,超过3秒走paymentInfoTimeOutHandler方法
    //@HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
    //不指明fallbackMethod方法,其走的就是默认的全局方法defaultFallback,指定就走指定的fallbackMethod方法
    @HystrixCommand
    public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
        //这里故意异常用于测试异常后是否会走服务降级的回退方法
        int i = 10/0;
        String result = paymentHystrixService.paymentInfoTimeOut(id);
        return result;
    }

    public String paymentInfoTimeOutHandler(Integer id) {
        return "服务提供者繁忙,请稍后再试";
    }

    //全局fallback方法,配置了@HystrixCommand但没有指明fallbackMethod方法的都走这个全局fallback方法
    public String paymentGlobalFallBackMethod() {
        return "Global异常处理";
    }
}
2、重启cloud-consumer-feign-hystrix-order80服务

效果图:

3、 在浏览器的地址栏里分别输入http://localhost:8080/consumer/payment/hystrix/timeout/1通过调用这个接口查看服务消费者的全局服务降级功能是否正常运行

由图可知服务消费者的全局服务降级成功,在服务异常后走了默认全局回退方法paymentGlobalFallBackMethod

相关推荐
listhi5209 分钟前
Docker中授权普通用户使用docker命令以及解决无权限访问/var/run/docker.sock错误
spring cloud·云原生·eureka
我命由我123455 小时前
Spring Cloud - Spring Cloud 负载均衡(Ribbon 负载均衡概述、Ribbon 使用)
java·后端·spring·spring cloud·ribbon·java-ee·负载均衡
纳就这样吧1 天前
Spring Cloud中@EnableDiscoveryClient注解详解
spring boot·后端·spring cloud
thginWalker1 天前
SpringCloud微服务项目实战——系统实现篇
spring cloud
云创智城-yuncitys2 天前
SpringCloud 架构在智慧交通路侧停车系统中的实践:从技术落地到城市级服务升级
spring·spring cloud·架构·智慧城市·停车系统·充电系统源码
番茄Salad2 天前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud
kkkkk0211062 天前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
洛克大航海2 天前
5-SpringCloud-服务链路追踪 Micrometer Tracing
后端·spring·spring cloud·zipkin·micrometer
我命由我123452 天前
Spring Cloud - Spring Cloud 微服务概述 (微服务的产生与特点、微服务的优缺点、微服务设计原则、微服务架构的核心组件)
java·运维·spring·spring cloud·微服务·架构·java-ee
我命由我123452 天前
Spring Cloud - Spring Cloud 注册中心与服务提供者(Spring Cloud Eureka 概述、微服务快速入门、微服务应用实例)
java·spring boot·spring·spring cloud·微服务·eureka·java-ee