Spring重试之Spring-Retry,Guava-Retry和Hystrix

1 重试之Spring-Retry

1.1 简介

Spring RetrySpring框架提供的一个模块,它通过提供注解或编程方式的方式,帮助我们实现方法级别的重试机制。在Spring Boot中,可以很方便地集成并使用Spring Retry
在这里插入图片描述

1.2 直接使用

Spring RetrySpring 应用程序提供了声明性重试支持。它用于Spring批处理、Spring集成、Apache Hadoop(等等)。它主要是针对可能抛出异常的一些调用操作,进行有策略的重试

1.2.1 pom.xml

准备工作
我们只需要加上依赖:

 <dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.2.RELEASE</version>
 </dependency>

1.2.2 重试任务

准备一个任务方法,这里是采用一个随机整数,根据不同的条件返回不同的值,或者抛出异常

package com.zgd.demo.thread.retry;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.remoting.RemoteAccessException;

/**
 * @Description:
 */
@Slf4j
public class RetryDemoTask {


  /**
   * 重试方法
   * @return
   */
  public static boolean retryTask(String param)  {
    log.info("收到请求参数:{}",param);

    int i = RandomUtils.nextInt(0,11);
    log.info("随机生成的数:{}",i);
    if (i == 0) {
      log.info("为0,抛出参数异常.");
      throw new IllegalArgumentException("参数异常");
    }else if (i  == 1){
      log.info("为1,返回true.");
      return true;
    }else if (i == 2){
      log.info("为2,返回false.");
      return false;
    }else{
      //为其他
        log.info("大于2,抛出自定义异常.");
        throw new RemoteAccessException("大于2,抛出远程访问异常");
      }
    }
}

1.2.3 使用SpringRetryTemplate

这里可以写我们的代码了

package com.zgd.demo.thread.retry.spring;

import com.zgd.demo.thread.retry.RetryDemoTask;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: spring-retry 重试框架
 */
@Slf4j
public class SpringRetryTemplateTest {

  /**
   * 重试间隔时间ms,默认1000ms
   * */
  private long fixedPeriodTime = 1000L;
  /**
   * 最大重试次数,默认为3
   */
  private int maxRetryTimes = 3;
  /**
   * 表示哪些异常需要重试,key表示异常的字节码,value为true表示需要重试
   */
  private Map<Class<? extends Throwable>, Boolean> exceptionMap = new HashMap<>();


  @Test
  public void test() {
    exceptionMap.put(RemoteAccessException.class,true);

    // 构建重试模板实例
    RetryTemplate retryTemplate = new RetryTemplate();

    // 设置重试回退操作策略,主要设置重试间隔时间
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(fixedPeriodTime);

    // 设置重试策略,主要设置重试次数
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(maxRetryTimes, exceptionMap);

    retryTemplate.setRetryPolicy(retryPolicy);
    retryTemplate.setBackOffPolicy(backOffPolicy);

    Boolean execute = retryTemplate.execute(
            //RetryCallback
            retryContext -> {
              boolean b = RetryDemoTask.retryTask("abc");
              log.info("调用的结果:{}", b);
              return b;
            },
            retryContext -> {
              //RecoveryCallback
              log.info("已达到最大重试次数或抛出了不重试的异常~~~");
              return false;
            }
      );

    log.info("执行结果:{}",execute);

  }

}

简单剖析下案例代码,RetryTemplate 承担了重试执行者的角色,它可以设置SimpleRetryPolicy(重试策略,设置重试上限,重试的根源实体),FixedBackOffPolicy(固定的回退策略,设置执行重试回退的时间间隔)。

RetryTemplate通过execute提交执行操作,需要准备RetryCallbackRecoveryCallback 两个类实例

  • RetryCallback:对应的就是重试回调逻辑实例,包装正常的功能操作
  • RecoveryCallback:实现的是整个执行操作结束的恢复操作实例

注意:只有在调用的时候抛出了异常,并且异常是在exceptionMap中配置的异常,才会执行重试操作,否则就调用到excute方法的第二个执行方法RecoveryCallback

当然,重试策略还有很多种,回退策略也是:

  • 重试策略
    • NeverRetryPolicy: 只允许调用RetryCallback一次,不允许重试
    • AlwaysRetryPolicy: 允许无限重试,直到成功,此方式逻辑不当会导致死循环
    • SimpleRetryPolicy: 固定次数重试策略,默认重试最大次数为3次,RetryTemplate默认使用的策略
    • TimeoutRetryPolicy: 超时时间重试策略,默认超时时间为1秒,在指定的超时时间内允许重试
    • ExceptionClassifierRetryPolicy: 设置不同异常的重试策略,类似组合重试策略,区别在于这里只区分不同异常的重试
    • CircuitBreakerRetryPolicy: 有熔断功能的重试策略,需设置3个参数openTimeoutresetTimeoutdelegate
    • CompositeRetryPolicy: 组合重试策略,有两种组合方式,乐观组合重试策略是指只要有一个策略允许即可以重试,悲观组合重试策略是指只要有一个策略不允许即可以重试,但不管哪种组合方式,组合中的每一个策略都会执行
  • 重试回退策略
    重试回退策略,指的是每次重试是立即重试还是等待一段时间后重试。
    默认情况下是立即重试,如果需要配置等待一段时间后重试则需要指定回退策略BackoffRetryPolicy
    • NoBackOffPolicy: 无退避算法策略,每次重试时立即重试
    • FixedBackOffPolicy: 固定时间的退避策略,需设置参数sleeperbackOffPeriodsleeper指定等待策略,默认是Thread.sleep,即线程休眠,backOffPeriod指定休眠时间,默认1秒
    • UniformRandomBackOffPolicy: 随机时间退避策略,需设置sleeperminBackOffPeriodmaxBackOffPeriod,该策略在minBackOffPeriod,maxBackOffPeriod之间取一个随机休眠时间,minBackOffPeriod默认500毫秒,maxBackOffPeriod默认1500毫秒
    • ExponentialBackOffPolicy: 指数退避策略,需设置参数sleeperinitialIntervalmaxIntervalmultiplierinitialInterval指定初始休眠时间,默认100毫秒,maxInterval指定最大休眠时间,默认30秒,multiplier指定乘数,即下一次休眠时间为当前休眠时间*multiplier
    • ExponentialRandomBackOffPolicy: 随机指数退避策略,引入随机乘数可以实现随机乘数回退

上面的代码的话,简单的设置了重试间隔为1秒,重试的异常是RemoteAccessException,下面就是测试代码的情况: 重试第二次成功的情况:
在这里插入图片描述
重试一次以后,遇到了没有指出需要重试的异常,直接结束重试,调用retryContext
在这里插入图片描述
重试了三次后,达到了最大重试次数,调用retryContext
在这里插入图片描述

1.3 注解使用方式

既然是Spring家族的东西,那么自然就支持和Spring-Boot整合

1.3.1 注解介绍

我们只要在需要重试的方法上加@Retryable,在重试失败的回调方法上加@Recover,下面是这些注解的属性

@EnableRetry,表示是否开启重试

序号 属性 类型 默认值 说明
1 proxyTargetClass boolean false 指示是否要创建基于子类的(CGLIB)代理,而不是创建标准的基于Java接口的代理

@Retryable,标注此注解的方法在发生异常时会进行重试

序号 属性 类型 默认值 说明
1 interceptor String "" 将interceptor的bean名称应用到retryable()
2 value Class[] {} 可重试的异常类型
3 label String "" 统计报告的唯一标签。如果没有提供,调用这可以选择忽略它,或者提供默认值
4 maxAttempts int 3 尝试的最大次数(包括第一次失败),默认为3次
5 backoff @Backoff @Backoff() 指定用于重试此操作的backoff属性。默认为空
6 exclude Class[] {} 排除可重试的异常类型

@Backoff

序号 属性 类型 默认值 说明
1 delay long 0 如果不设置则默认使用 1000 milliseconds 重试等待
2 maxDelay long 0 最大重试等待时间
3 multiplier long 0 用于计算下一个延迟的乘数(大于0生效)
4 random boolean false 随机重试等待时间

1.3.2 pom.xml

 <dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.2.RELEASE</version>
 </dependency>

 <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.1</version>
 </dependency>

1.3.3 代码

application启动类上加上@EnableRetry的注解

@EnableRetry
public class Application {
 ...
}

为了方便测试,这里写了一个SpringBootTest的测试基类,需要使用SpringBootTest的只要继承这个类就好了

package com.zgd.demo.thread.test;

/**
 * @Author: zgd
 * @Description:
 */

import com.zgd.demo.thread.Application;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author: zgd
 * @Date: 18/09/29 20:33
 * @Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class MyBaseTest {


  @Before
  public void init() {
    log.info("----------------测试开始---------------");
  }

  @After
  public void after() {
    log.info("----------------测试结束---------------");
  }

}

建一个service类

package com.zgd.demo.thread.retry.spring;

import com.zgd.demo.thread.retry.RetryDemoTask;
import com.zgd.demo.thread.test.MyBaseTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.ExhaustedRetryException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

/**
 * @Author: zgd
 * @Description:
 */
@Service
@Slf4j
public class SpringRetryDemo   {

 /**
   * 重试所调用方法
   * @param param
   * @return
   */
  @Retryable(value = {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 2000L,multiplier = 2))
  public boolean call(String param){
      return RetryDemoTask.retryTask(param);
  }

  /**
   * 达到最大重试次数,或抛出了一个没有指定进行重试的异常
   * recover 机制
   * @param e 异常
   */
  @Recover
  public boolean recover(Exception e,String param) {
    log.error("达到最大重试次数,或抛出了一个没有指定进行重试的异常:",e);
    return false;
  }

}

然后我们调用这个service里面的call方法

package com.zgd.demo.thread.retry.spring;

import com.zgd.demo.thread.test.MyBaseTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: zgd
 * @Description:
 */
@Component
@Slf4j
public class SpringRetryDemoTest extends MyBaseTest {

  @Autowired
  private SpringRetryDemo springRetryDemo;

  @Test
  public void retry(){
    boolean abc = springRetryDemo.call("abc");
    log.info("--结果是:{}--",abc);
  }

}

这里我依然是RemoteAccessException的异常才重试,@Backoff(delay = 2000L,multiplier = 2))表示第一次间隔2秒,以后都是次数的2倍,也就是第二次4秒,第三次6秒.

1.3.4 测试结果

遇到了没有指定重试的异常,这里指定重试的异常是 @Retryable(value = {RemoteAccessException.class}...,所以抛出参数异常IllegalArgumentException的时候,直接回调@Recover的方法
在这里插入图片描述

重试达到最大重试次数时,调用@Recover的方法
在这里插入图片描述

重试到最后一次没有报错,返回false
在这里插入图片描述

1.4 @Retryable和@Recover注解使用问题

@Retryable@RecoverSpring Framework中的注解,用于支持在方法执行期间发生异常时的重试和恢复操作。

  • @Retryable:注解用于标记方法,在方法执行期间发生异常时进行重试。重试行为可以使用Spring Retry框架提供的默认策略或自定义策略来定义。可以指定要重试的异常类型以及最大重试次数和重试间隔等参数。
  • @Recover:注解用于标记一个恢复方法,在最终重试失败后执行该方法。恢复方法应具有与原始方法相同的参数和返回类型,并且应在同一类中声明。如果未指定恢复方法,则重试失败后将抛出最后一次异常。

1.4.1 @Retryable和@Recover必须定义在一个类当中吗

@Retryable@Recover注解必须定义在同一个类中。这是因为@Retryable注解标记的方法需要调用重试逻辑,而@Recover注解标记的方法需要提供重试失败后的恢复逻辑。因此,这两个方法必须在同一个类中,以便它们能够相互调用。

例如,以下示例演示了@Retryable@Recover注解在同一个类中的使用

@Service
public class MyService {
 
    @Retryable(RuntimeException.class)
    public void myMethod() {
        // Some code that may throw a RuntimeException
    }
 
    @Recover
    public void recover() {
        // Recovery logic goes here
    }
}

在上面的示例中,MyService类中的myMethod()方法使用@Retryable注解进行标记,以便在发生RuntimeException异常时进行重试。如果重试最终失败,则recover()方法将被调用以提供恢复逻辑。由于这两个方法在同一个类中定义,因此它们可以相互调用。

1.4.2 如果一个类中有多个@Recover和@Retryable怎么区分

如果一个类中有多个方法标记了@Retryable@Recover注解,你可以通过value属性来区分它们。value属性允许你指定一个异常类型的数组,以区分在方法执行期间抛出的不同异常类型。

例如,以下示例演示了在同一个类中有多个使用@Retryable和@Recover注解的方法的情况:

@Service
public class MyService {
 
    @Retryable(value = {IOException.class})
    public void methodA() throws IOException {
        // Some code that may throw an IOException
    }
 
    @Recover
    public void recoverA(IOException e) {
        // Recovery logic for methodA() goes here
    }
 
    @Retryable(value = {NullPointerException.class})
    public void methodB() throws NullPointerException {
        // Some code that may throw a NullPointerException
    }
 
    @Recover
    public void recoverB(NullPointerException e) {
        // Recovery logic for methodB() goes here
    }
}

在上面的示例中,methodA()和methodB()方法都标记为@Retryable注解,以便在抛出IOException或NullPointerException异常时进行重试。然后,对于每个方法,都定义了一个恢复方法recoverA()和recoverB(),分别提供特定于该方法的恢复逻辑。由于每个方法都在@Retryable注解的value属性中指定了不同的异常类型,因此Spring框架可以区分它们,并在适当的时候调用相应的方法。

1.4.3 如果异常也一样呢

如果多个方法在抛出相同的异常时都需要进行重试和恢复操作,你可以在每个方法上使用相同的@Retryable和@Recover注解。在这种情况下,Spring框架会自动根据需要调用相应的恢复方法。

例如,以下示例演示了在同一个类中有多个方法抛出相同异常,并且都需要重试和恢复的情况:

@Service
public class MyService {
 
    @Retryable(RuntimeException.class)
    public void methodA() {
        // Some code that may throw a RuntimeException
    }
 
    @Retryable(RuntimeException.class)
    public void methodB() {
        // Some code that may throw a RuntimeException
    }
 
    @Recover
    public void recover() {
        // Recovery logic goes here
    }
}

在上面的示例中,methodA()和methodB()方法都标记为@Retryable注解,以便在抛出RuntimeException异常时进行重试。由于它们都使用相同的异常类型和重试策略,因此它们也可以使用相同的恢复方法recover()。在最终重试失败时,Spring框架将调用recover()方法以提供恢复逻辑。

1.4.4 那怎么在recover中知道是哪个方法发生了异常呢

@Recover注解标记的恢复方法中,可以通过方法的参数获取抛出异常的方法和异常信息。具体来说,@Recover方法可以接受与@Retryable注解标记的方法相同的参数,以便在恢复操作中访问异常信息和方法参数。

例如,以下示例演示了在@Retryable方法和@Recover方法中访问异常和方法参数的方式:

@Service
public class MyService {
 
    @Retryable(RuntimeException.class)
    public void methodA(String arg) {
        // Some code that may throw a RuntimeException
    }
 
    @Retryable(RuntimeException.class)
    public void methodB(int arg) {
        // Some code that may throw a RuntimeException
    }
 
    @Recover
    public void recover(Exception e, Object... args) {
        if (args[0] instanceof String) {
            // Recovery logic for methodA() goes here
        } else if (args[0] instanceof Integer) {
            // Recovery logic for methodB() goes here
        }
    }
}

在上面的示例中,methodA()和methodB()方法都标记为@Retryable注解,以便在抛出RuntimeException异常时进行重试。在@Recover注解标记的恢复方法中,可以通过方法的参数访问抛出异常的方法和方法参数。在上述示例中,我们检查第一个参数的类型以确定是哪个方法抛出了异常,从而提供相应的恢复逻辑。

需要注意的是,@Retryable方法和@Recover方法中的参数类型和数量必须相同,否则Spring框架将无法确定哪个方法抛出了异常。

2 重试之Guava-Retry

2.1 简介

Guava retryer工具与spring-retry类似,都是通过定义重试者角色来包装正常逻辑重试,但是Guava retryer有更优的策略定义,在支持重试次数和重试频度控制基础上,能够兼容支持多个异常或者自定义实体对象的重试源定义,让重试功能有更多的灵活性。
Guava Retryer也是线程安全的,入口调用逻辑采用的是Java.util.concurrent.Callablecall方法,示例代码如下:

2.2 pom.xml

pom.xml加入依赖

  <!-- https://mvnrepository.com/artifact/com.github.rholder/guava-retrying -->
    <dependency>
        <groupId>com.github.rholder</groupId>
        <artifactId>guava-retrying</artifactId>
        <version>2.0.0</version>
    </dependency>

2.3 代码操作

更改一下测试的任务方法

package com.zgd.demo.thread.retry;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.remoting.RemoteAccessException;

/**
 * @Author: zgd
 * @Description:
 */
@Slf4j
public class RetryDemoTask {


  /**
   * 重试方法
   * @return
   */
  public static boolean retryTask(String param)  {
    log.info("收到请求参数:{}",param);

    int i = RandomUtils.nextInt(0,11);
    log.info("随机生成的数:{}",i);
    if (i < 2) {
      log.info("为0,抛出参数异常.");
      throw new IllegalArgumentException("参数异常");
    }else if (i  < 5){
      log.info("为1,返回true.");
      return true;
    }else if (i < 7){
      log.info("为2,返回false.");
      return false;
    }else{
      //为其他
        log.info("大于2,抛出自定义异常.");
        throw new RemoteAccessException("大于2,抛出自定义异常");
      }
    }

}

这里设定跟Spring-Retry不一样,我们可以根据返回的结果来判断是否重试,比如返回false我们就重试

package com.zgd.demo.thread.retry.guava;

import com.github.rholder.retry.*;
import com.zgd.demo.thread.retry.RetryDemoTask;
import org.junit.Test;
import org.springframework.remoting.RemoteAccessException;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

/**
 * @Author: zgd
 * @Description:
 */
public class GuavaRetryTest {


  @Test
  public void fun01(){
    // RetryerBuilder 构建重试实例 retryer,可以设置重试源且可以支持多个重试源,可以配置重试次数或重试超时时间,以及可以配置等待时间间隔
    Retryer<Boolean> retryer = RetryerBuilder.<Boolean> newBuilder()
            .retryIfExceptionOfType(RemoteAccessException.class)//设置异常重试源
            .retryIfResult(res-> res==false)  //设置根据结果重试
            .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS)) //设置等待间隔时间
            .withStopStrategy(StopStrategies.stopAfterAttempt(3)) //设置最大重试次数
            .build();

    try {
      retryer.call(() -> RetryDemoTask.retryTask("abc"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

2.4 运行结果

遇到了我们指定的需要重试的异常,进行重试,间隔是3秒
在这里插入图片描述

重试次数超过了最大重试次数

在这里插入图片描述

返回为true,直接结束重试
在这里插入图片描述

遇到了没有指定重试的异常,结束重试
在这里插入图片描述

返回false,重试

在这里插入图片描述

2.5 Guava配置策略

我们可以更灵活的配置重试策略,比如:

  • retryIfExceptionretryIfException,抛出 runtime 异常、checked 异常时都会重试,但是抛出 error 不会重试。
  • retryIfRuntimeExceptionretryIfRuntimeException 只会在抛 runtime 异常的时候才重试,checked 异常和error 都不重试。
  • retryIfExceptionOfTyperetryIfExceptionOfType允许我们只在发生特定异常的时候才重试,比如NullPointerExceptionIllegalStateException 都属于 runtime 异常,也包括自定义的error。
    如:
    retryIfExceptionOfType(NullPointerException.class),只在抛出空指针异常重试
  • retryIfResult: retryIfResult 可以指定Callable 方法在返回值的时候进行重试,如
    • 返回false重试
      .retryIfResult(Predicates.equalTo(false))
    • _error结尾才重试
      .retryIfResult(Predicates.containsPattern("_error$"))
    • 返回为空时重试
      .retryIfResult(res-> res==null)
  • RetryListener: 当发生重试之后,假如我们需要做一些额外的处理动作,比如log一下异常,那么可以使用RetryListener。每次重试之后,guava-retrying 会自动回调我们注册的监听。可以注册多个RetryListener,会按照注册顺序依次调用。
.withRetryListener(new RetryListener {      
 @Override    
   public <T> void onRetry(Attempt<T> attempt) {  
               logger.error("第【{}】次调用失败" , attempt.getAttemptNumber());  
          } 
 }) 

2.6 主要接口

序号 接口 描述 备注
1 Attempt 一次执行任务
2 AttemptTimeLimiter 单次任务执行时间限制 如果单词任务执行超时,则终止执行当前任务
3 BlockStrategies 任务阻塞策略 通俗的讲就是当前任务执行完,下次任务还没开始这段时间做什么,默认策略为:BlockStrategies.THREAD_SLEEP_STRATEGY
4 RetryException 重试异常
5 RetryListener 自定义重试监听器 可以用于异步记录错误日志
6 StopStrategy 停止重试策略
7 WaitStrategy 等待时长策略 (控制时间间隔),返回结果为下次执行时长

StopStrategy,停止重试策略,提供三种

  • StopAfterDelayStrategy
    设定一个最长允许的执行时间,比如设定最长执行10S,无论任务执行次数,只要重试的时候超出了最长时间,则任务终止,并返回重试异常RetryException
  • NeverStopStrategy
    不停止,用于需要一直轮训知道返回期望结果的情况:
  • StopAfterAttemptStrategy
    设定最大重试次数,如果超出最大重试次数则停止重试,并返回重试异常:

WaitStrategy,等待时长策略

  • FixedWaitStrategy
    固定等待时长策略,
  • RandomWaitStrategy
    随机等待时长策略 (可以提供一个最小和最大时长,等待时长为其区间随机值)
  • IncrementingWaitstrategy
    递增等待时长策略 (提供一个初始值和步长,等待时间随重试次数增加而增加)
  • ExponentialWaitstrategy
    指数等待时长策略;
  • FibonacciWaitStrategy
    Fibonacci等待时长策略,
  • ExceptionWaitStrategy
    异常时长等待策略;
  • CompositeWaitStrategy
    复合时长等待策略;

3 使用断路器Hystrix实现熔断机制

除了重试机制外,熔断机制也是一种常见的容错处理手段。Hystrix是一款流行的断路器实现库,可以与Spring Boot集成,用于实现熔断机制。

3.1 添加依赖

在pom.xml中添加Hystrix的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

3.2 配置启用Hystrix

Spring Boot的主类上添加@EnableHystrix注解:

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

3.3 使用Hystrix实现熔断

3.3.1 代码示例

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class ThirdPartyService {

    @HystrixCommand(fallbackMethod = "fallback")
    public String callThirdPartyApi() {
        // 调用第三方API的逻辑
        // ...
    }

    public String fallback() {
        // 熔断时的降级逻辑
        // ...
    }
}

在上述示例中,通过@HystrixCommand注解标记了callThirdPartyApi方法,指定了熔断时执行的降级方法fallback

文章参考:
https://mp.weixin.qq.com/s/nUD5kT2BcEEbmfeyG7rnDQ
https://mp.weixin.qq.com/s/pEPHzbPdmCsNeFeF2LwlXg

posted @ 2022-12-23 15:36  上善若泪  阅读(167)  评论(0编辑  收藏  举报