SpringCloud-21-Hystrix全局降级和解耦降级逻辑

发布于:2022-12-07 ⋅ 阅读:(756) ⋅ 点赞:(0)

8.6 全局降级方法

  • 通过上面的方式实现服务降级时,需要针对所有业务方法都配置降级方法,这极有可能会造成代码的急剧膨胀。为了解决该问题,我们还可以为所有业务方法指定一个全局的回退方法,具体步骤如下。

  • 沿用microservice-cloud-consumer-dept-openFeign下的DeptHystrixController控制类。在 DeptHystrixController的类名上标注 @DefaultProperties 注解,并通过其 defaultFallback 属性指定一个全局的降级方法,前提需要创建一个名为 globalFallbackMethod 的全局回方法。

  • 另外在所有的业务方法上都标注 @HystrixCommand 注解,这里我们将 testTimeOut() 方法上的 @HystrixCommand(fallbackMethod = “deptTimeoutHandler”)注释为 @HystrixCommand 即可,代码如下。代码如下。

package com.example.controller;

import com.example.service.DeptHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author CNCLUKZK
 * @create 2022/9/20-17:25
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "globalFallbackMethod")
public class DeptHystrixController {

    @Autowired
    private DeptHystrixService deptHystrixService;

    @GetMapping("/consumer/dept/getInfo/Hystrix/200/{id}")
    @HystrixCommand
    public String dept_200(@PathVariable("id") Integer id) {
        String info = deptHystrixService.dept_200(id);
        log.info(info);
        return "客户端请求"+info;
    }
    //在客户端进行降级
    //超时测试,该服务的响应时间为 3 秒
    @GetMapping("/consumer/dept/testTimeOut/Hystrix/{id}")
    //@HystrixCommand(fallbackMethod = "deptTimeoutHandler")
    @HystrixCommand
    public String testTimeOut(@PathVariable("id") Integer id){
        String info = deptHystrixService.testTimeOut(id);
        //当Application.yml中没有配置当前端口号,只能使用此方式获取端口
        log.info(info);
        return info;
    }

    // testTimeOut方法的 专用 fallback 方法
    public String deptTimeoutHandler(Integer id){
        log.info("testTimeOut 出错,服务被降级!");
        return "因当前请求testTimeOut超时,客户端服务降级,返回提示信息!当前线程:"+Thread.currentThread().getName()+"请求超时500。ID+"+id;
    }

    public String globalFallbackMethod(){
        log.info("Dept业务异常,服务被降级!");
        return "因当前请求异常,客户端服务降级,客户端全局回退方法执行,返回提示信息!当前线程:"+Thread.currentThread().getName()+"请求超时500";
    }
}
  1. 降级(FallBack)方法必须与其对应的业务方法在同一个类中,否则无法生效。
  2. 全局的 fallback 方法@DefaultProperties(defaultFallback = “globalFallbackMethod”) 类上注解,请求方法上使用 @HystrixCommand 注解
  3. 全局降级方法的优先级较低,只有业务方法没有指定其降级方法时,服务降级时才会触发全局回退方法。若业务方法指定它自己的回退方法,那么在服务降级时,就只会直接触发它自己的回退方法,而非全局回退方法。
  • 重启 microservice-cloud-consumer-dept-openFeign,使用浏览器访问,访问http://localhost/consumer/dept/testTimeOut/Hystrix/1在这里插入图片描述

8.7 解耦降级逻辑

  • 不管是业务方法指定的降级方法还是全局降级方法,它们都必须和业务方法在同一个类中才能生效,业务逻辑与降级逻辑耦合度极高。

  • 下面对业务逻辑与降级逻辑进行解耦,操作步骤如下。在 microservice-cloud-consumer-dept-openFeign 的 com.example.service 包下,新建 DeptHystrixService接口的实现类 DeptHystrixFallBackServiceImpl,统一为 DeptHystrixService 中的方法提供服务降级处理 ,代码如下。

该实现 类必须以组件的形式添加 Spring 容器中才能生效,最常用的方式就是在类上标注 @Service注解。

package com.example.service;

import org.springframework.stereotype.Service;

/**
 * @author CNCLUKZK
 * @create 2022/9/20-18:55
 */
@Service
public class DeptHystrixFallBackServiceImpl implements DeptHystrixService{

    @Override
    public String dept_200(Integer id) {
        return "客户端提示,解耦降级方法执行dept_200";
    }

    @Override
    public String testTimeOut(Integer id) {
        return "客户端提示,解耦降级方法执行testTimeOut";
    }
}
  • 在服务绑定接口 DeptHystrixService标注的 @FeignClient 注解中添加 fallback 属性,属性值为 DeptHystrixFallBackServiceImpl.class,代码如下
package com.example.service;

import com.example.config.FeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Service
@FeignClient(value = "MICROSERVICECLOUDPROVIDERDEPTHYSTRIX",configuration = FeignConfiguration.class,fallback = DeptHystrixFallBackServiceImpl.class)
public interface DeptHystrixService {

    @GetMapping("/dept/getInfo/Hystrix/200/{id}")
    String dept_200(@PathVariable("id") Integer id);

    @GetMapping("/dept/testTimeOut/Hystrix/{id}")
    String testTimeOut(@PathVariable("id") Integer id);
}
  • 重启 microservice-cloud-consumer-dept-openFeign,使用浏览器访问,访问http://localhost/consumer/dept/testTimeOut/Hystrix/1 在这里插入图片描述
  • 也可以使用@FeignClient 注解fallbackFactory来进行解耦降级,新建一个DeptHystrixFallBackServiceImpl2去实现FallbackFactory,返回值返回DeptHystrixService的实现类DeptHystrixFallBackServiceImpl,若是没有实现类,则返回这个接口DeptHystrixService但需要在这里实现接口的方法。
package com.example.service;

import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Service;

/**
 * @author CNCLUKZK
 * @create 2022/9/21-18:25
 */
@Service
public class DeptHystrixFallBackServiceImpl2 implements FallbackFactory {
    @Override
    public Object create(Throwable cause) {
        return new DeptHystrixFallBackServiceImpl();
    }
}
  • 然后在服务绑定接口 DeptHystrixService标注的 @FeignClient 注解中添加 fallbackFactory属性,属性值为 DeptHystrixFallBackServiceImpl2.class,代码如下
package com.example.service;

import com.example.config.FeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author CNCLUKZK
 * @create 2022/9/20-17:22
 */
@Service
@FeignClient(value = "MICROSERVICECLOUDPROVIDERDEPTHYSTRIX",configuration = FeignConfiguration.class,fallbackFactory = DeptHystrixFallBackServiceImpl2.class)
public interface DeptHystrixService {

    @GetMapping("/dept/getInfo/Hystrix/200/{id}")
    String dept_200(@PathVariable("id") Integer id);

    @GetMapping("/dept/testTimeOut/Hystrix/{id}")
    String testTimeOut(@PathVariable("id") Integer id);


    @GetMapping("/dept/Hystrix/circuitBreaker/{num}")
    String circuitBreakerFallbackMethod(@PathVariable("num") Integer num);
}

下一篇:SpringCloud-22-Hystrix全Hystrix服务熔断

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到