评论

收藏

[Java] Spring Cloud Gateway重试机制的实现

编程语言 编程语言 发布于:2021-09-18 13:38 | 阅读数:193 | 评论:0

这篇文章主要介绍了Spring Cloud Gateway重试机制的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
前言
重试,我相信大家并不陌生。在我们调用http接口的时候,总会因为某种原因调用失败,这个时候我们可以通过重试的方式,来重新请求接口。
生活中这样的事例很多,比如打电话,对方正在通话中啊,信号不好啊等等原因,你总会打不通,当你第一次没打通之后,你会打第二次,第三次...第四次就通了。
重试也要注意应用场景,读数据的接口比较适合重试的场景,写数据的接口就需要注意接口的幂等性了。还有就是重试次数如果太多的话会导致请求量加倍,给后端造成更大的压力,设置合理的重试机制才是最关键的。
今天我们来简单的了解下spring cloud gateway中的重试机制和使用。
使用讲解
retrygatewayfilter是spring cloud gateway对请求重试提供的一个gatewayfilter factory。
配置方式:
spring:
 cloud:
  gateway:
   routes:
   - id: fsh-house
  uri: lb://fsh-house
  predicates:
  - path=/house/**
  filters:
  - name: retry
   args:
    retries: 3
    series:
    - server_error
    statuses:
    - ok
    methods:
    - get
    - post
    exceptions:
    - java.io.ioexception
配置讲解
配置类源码org.springframework.cloud.gateway.filter.factory.retrygatewayfilterfactory.retryconfig:
public static class retryconfig {
  private int retries = 3;
  
  private list<series> series = tolist(series.server_error);
  
  private list<httpstatus> statuses = new arraylist<>();
  
  private list<httpmethod> methods = tolist(httpmethod.get);
 
  private list<class<? extends throwable>> exceptions = tolist(ioexception.class);
  
  // .....
}
retries:重试次数,默认值是3次
series:状态码配置(分段),符合的某段状态码才会进行重试逻辑,默认值是server_error,值是5,也就是5xx(5开头的状态码),共有5个值:
public enum series {
  informational(1),
  successful(2),
  redirection(3),
  client_error(4),
  server_error(5);
}
statuses:状态码配置,和series不同的是这边是具体状态码的配置,取值请参考:org.springframework.http.httpstatus
methods:指定哪些方法的请求需要进行重试逻辑,默认值是get方法,取值如下:
public enum httpmethod {
  get, head, post, put, patch, delete, options, trace;
}
exceptions:指定哪些异常需要进行重试逻辑,默认值是java.io.ioexception
代码测试
就写个接口,在接口中记录请求次数,然后抛出一个异常模拟500,通过网关访问这个接口,如果你配置了重试次数是3,那么接口中会输出4次结果才是对的,证明重试生效了。
atomicinteger ac = new atomicinteger();
 
@getmapping("/data")
public houseinfo getdata(@requestparam("name") string name) {
  if (stringutils.isblank(name)) {
  throw new runtimeexception("error");
  }
  system.err.println(ac.addandget(1));
  return new houseinfo(1l, "上海", "虹口", "xx小区");
}
更多spring cloud代码尽在:https://github.com/yinjihuan/spring-cloud
源码欣赏
@override
public gatewayfilter apply(retryconfig retryconfig) {
  // 验证重试配置格式是否正确
  retryconfig.validate();
 
  repeat<serverwebexchange> statuscoderepeat = null;
  if (!retryconfig.getstatuses().isempty() || !retryconfig.getseries().isempty()) {
  predicate<repeatcontext<serverwebexchange>> repeatpredicate = context -> {
    serverwebexchange exchange = context.applicationcontext();
    // 判断重试次数是否已经达到了配置的最大值
    if (exceedsmaxiterations(exchange, retryconfig)) {
    return false;
    }
    // 获取响应的状态码
    httpstatus statuscode = exchange.getresponse().getstatuscode();
    // 获取请求方法类型
    httpmethod httpmethod = exchange.getrequest().getmethod();
    // 判断响应状态码是否在配置中存在
    boolean retryablestatuscode = retryconfig.getstatuses().contains(statuscode);
 
    if (!retryablestatuscode && statuscode != null) { // null status code might mean a network exception?
    // try the series
    retryablestatuscode = retryconfig.getseries().stream()
      .anymatch(series -> statuscode.series().equals(series));
    }
    // 判断方法是否包含在配置中
    boolean retryablemethod = retryconfig.getmethods().contains(httpmethod);
    // 决定是否要进行重试
    return retryablemethod && retryablestatuscode;
  };
 
  statuscoderepeat = repeat.onlyif(repeatpredicate)
    .doonrepeat(context -> reset(context.applicationcontext()));
  }
 
  //todo: support timeout, backoff, jitter, etc... in builder
 
  retry<serverwebexchange> exceptionretry = null;
  if (!retryconfig.getexceptions().isempty()) {
  predicate<retrycontext<serverwebexchange>> retrycontextpredicate = context -> {
    if (exceedsmaxiterations(context.applicationcontext(), retryconfig)) {
    return false;
    }
    // 异常判断
    for (class<? extends throwable> clazz : retryconfig.getexceptions()) {     
    if (clazz.isinstance(context.exception())) {
      return true;
    }
    }
    return false;
  };
  // 使用reactor extra的retry组件
  exceptionretry = retry.onlyif(retrycontextpredicate)
    .doonretry(context -> reset(context.applicationcontext()))
    .retrymax(retryconfig.getretries());
  }
 
 
  return apply(statuscoderepeat, exceptionretry);
}
 
public boolean exceedsmaxiterations(serverwebexchange exchange, retryconfig retryconfig) {
  integer iteration = exchange.getattribute(retry_iteration_key);
 
  //todo: deal with null iteration
  return iteration != null && iteration >= retryconfig.getretries();
}
 
public void reset(serverwebexchange exchange) {
  //todo: what else to do to reset swe?
  exchange.getattributes().remove(serverwebexchangeutils.gateway_already_routed_attr);
}
 
public gatewayfilter apply(repeat<serverwebexchange> repeat, retry<serverwebexchange> retry) {
  return (exchange, chain) -> {
  if (log.istraceenabled()) {
    log.trace("entering retry-filter");
  }
 
  // chain.filter returns a mono<void>
  publisher<void> publisher = chain.filter(exchange)
    //.log("retry-filter", level.info)
    .doonsuccessorerror((avoid, throwable) -> {
      // 获取已经重试的次数,默认值为-1
      int iteration = exchange.getattributeordefault(retry_iteration_key, -1);
      // 增加重试次数
      exchange.getattributes().put(retry_iteration_key, iteration + 1);
    });
 
  if (retry != null) {
    // retrywhen returns a mono<void>
    // retry needs to go before repeat
    publisher = ((mono<void>)publisher).retrywhen(retry.withapplicationcontext(exchange));
  }
  if (repeat != null) {
    // repeatwhen returns a flux<void>
    // so this needs to be last and the variable a publisher<void>
    publisher = ((mono<void>)publisher).repeatwhen(repeat.withapplicationcontext(exchange));
  }
 
  return mono.fromdirect(publisher);
  };
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://segmentfault.com/a/1190000018469096

关注下面的标签,发现更多相似文章