Resilience4j是用来替代Hystrix的一个开源组件。

引入依赖:

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>1.3.1</version>
    <exclusions>
        <exclusion>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-circuitbreaker</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-timelimiter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-circuitbreaker</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-timelimiter</artifactId>
    <version>1.3.1</version>
</dependency>

用Resilience4j实现重试:

1、用@Retry标注调用下游服务的接口方法,用name属性指定这个重试的名称。我们可以针对不同的调用下游服务制定不同的重试策略

@FeignClient(name = "spring-cloud-eureka-client-producer")
@Component
public interface MyInterface {
@Retry(name = "helloRetry")
@PostMapping(value = "/hello")
String hello(@RequestBody Person person);
}

2、在配置文件中添加

resilience4j.retry.retry-aspect-order=1
resilience4j.retry.backends.helloRetry.max-retry-attempts=3
resilience4j.retry.backends.helloRetry.wait-duration=2000
resilience4j.retry.backends.helloRetry.event-consumer-buffer-size=1
resilience4j.retry.backends.helloRetry.enable-exponential-backoff=false
resilience4j.retry.backends.helloRetry.exponential-backoff-multiplier=2
resilience4j.retry.backends.helloRetry.enable-randomized-wait=false
resilience4j.retry.backends.helloRetry.randomized-wait-factor=2
resilience4j.retry.backends.helloRetry.retry-exception-predicate=com.kou.springbooteurekaconsumerdemo.HelloRetryExceptionPredicate

retry-aspect-order指定重试优先级,max-retry-attempts指定最多执行多少次,wait-duration指定上次执行失败后多少毫秒后才重试,

3、新建步骤2中用到的HelloRetryExceptionPredicate类

public class HelloRetryExceptionPredicate implements Predicate<Throwable> {
@Override
public boolean test(Throwable throwable) {
if (throwable instanceof FeignException) {
FeignException e = (FeignException) throwable;
Throwable cause = e.getCause();
return cause instanceof IOException;
}
return false;
}
}

这个类指定了只有调下游服务抛IOException时才重试。当然,我们也可以指定404、50X时也重试。

如果不想为每一个调用都指定重试策略,而是想所有的调用都指定同一个重试策略,那么只需指定所有调用的@Retry注解的name属性值都相同就可以了。

如何实现fallback呢?

用Resilience4j实现断路器:

1、用@CircuitBreaker标注调用下游服务的接口方法,用name属性指定这个重试的名称。我们可以针对不同的调用下游服务制定不同的重试策略

spark第十一篇:spark-submit命令支持选项的更多相关文章

  1. spark调优篇-spark on yarn web UI

    spark on yarn 的执行过程在 yarn RM 上无法直接查看,即 http://192.168.10.10:8088,这对于调试程序很不方便,所以需要手动配置 配置方法 1. 配置 spa ...

  2. Spark(十一)Spark分区

    一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...

  3. spark调优篇-Spark ON Yarn 内存管理(汇总)

    本文旨在解析 spark on Yarn 的内存管理,使得 spark 调优思路更加清晰 内存相关参数 spark 是基于内存的计算,spark 调优大部分是针对内存的,了解 spark 内存参数有也 ...

  4. Spark中文指南(入门篇)-Spark编程模型(一)

    前言 本章将对Spark做一个简单的介绍,更多教程请参考:Spark教程 本章知识点概括 Apache Spark简介 Spark的四种运行模式 Spark基于Standlone的运行流程 Spark ...

  5. 【原创】大数据基础之Spark(1)Spark Submit即Spark任务提交过程

    Spark2.1.1 一 Spark Submit本地解析 1.1 现象 提交命令: spark-submit --master local[10] --driver-memory 30g --cla ...

  6. 执行Spark运行在yarn上的命令报错 spark-shell --master yarn-client

    1.执行Spark运行在yarn上的命令报错 spark-shell --master yarn-client,错误如下所示: // :: ERROR SparkContext: Error init ...

  7. spark第六篇:Spark Streaming Programming Guide

    预览 Spark Streaming是Spark核心API的扩展,支持高扩展,高吞吐量,实时数据流的容错流处理.数据可以从Kafka,Flume或TCP socket等许多来源获取,并且可以使用复杂的 ...

  8. Spark调研笔记第6篇 - Spark编程实战FAQ

    本文主要记录我使用Spark以来遇到的一些典型问题及其解决的方法,希望对遇到相同问题的同学们有所帮助. 1. Spark环境或配置相关 Q: Sparkclient配置文件spark-defaults ...

  9. 转载:Spark中文指南(入门篇)-Spark编程模型(一)

    原文:https://www.cnblogs.com/miqi1992/p/5621268.html 前言 本章将对Spark做一个简单的介绍,更多教程请参考:Spark教程 本章知识点概括 Apac ...

随机推荐

  1. 'for' loop initial declarations are only allo

    linux系统下的c编程与windows有所不同,如果你在用gcc编译代码的时候提示‘for’ loop initial declarations are only allowed in C99 mo ...

  2. QGIS与Python

    Qgis python开发教程(一):https://blog.csdn.net/u011435933/article/details/80419496

  3. logback 配置详解——logger、root

    目录 1.根节点包含的属性 2.根节点的子节点 2.1.设置上下文名称: 2.2.设置loger.root 正文 回到顶部 1.根节点<configuration>包含的属性 scan: ...

  4. Lotus迁移到Exchange 2010 POC 之Domino Server的配置!

    1.      在桌面点击安装完成的Domino 服务器配置:

  5. Autoconf 中文手册

    Autoconf Autoconf Creating Automatic Configuration Scripts Edition 2.13, for Autoconf version 2.13 D ...

  6. sql 的REPLACE

    REPLACE 用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法 REPLACE ( 'string_expression1' , 'string_expression2 ...

  7. [修正] Firemonkey Windows 控件有虚线残影问题

    说明:在 Wndows 显示时,有时控件左方会显示一条虚线 适用:Firemonkey Windows (Berlin 或更高版) 修正方法: 请将源码 FMX.Canvas.D2D.pas 复制到自 ...

  8. mybatis 单表的增删改查

    添加数据返回id mapper.xml mapper -> insert -> selectKey mybatis 内置别名

  9. rabbitmqBat常用指令

    激活 RabbitMQ's Management Pluginrabbitmq-plugins.bat enable rabbitmq_management 查看已有用户及用户的角色rabbitmqc ...

  10. 浅谈K8S cni和网络方案

    此文已由作者黄扬授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在早先的k8s版本中,kubelet代码里提供了networkPlugin,networkPlugin是一组接 ...