flink的RestartStrategy重启策略让你的程序更加健壮

flink的Restartstrages重启策略让你的程序更加健壮

意义
env.setRestartStrategy函数的意思是重启策略,当然这个函数并不是真正的让你的程序重启,反而是为了让你的程序避免重启,当我们在程序里设置了只接接受int类型的数据,我们偏偏给他传入一个double类型的数据,孱弱的程序会瞬间以内无法解析而废掉,但是我们在程序当中传入了.setRestartStrategy函数并传入参数,你的程序就不会废掉了,而是在不耽误干活的前提下给你汇报一下错误而已,这样避免了数据因为报错中断而丢失数据!从而让你的程序更加的健壮!
步骤
1.在程序中设置一个检查站(重启策略的必要前提)传入参数为5秒!就是说5秒钟检修查一次
env.enableCheckpointing(5000);
2.设置重启的次数和延迟时间
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3,5000));

代码实现

public class Restartstrages {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        //设置一个检查站(重启策略的必要前提)
        env.enableCheckpointing(5000);
        //设置重启的
        env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3,5000));

        //写java程序 第二种方式使用整合匿名实现类
        SingleOutputStreamOperator<Tuple2<String, Integer>> sandone = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String s) throws Exception {
                //当你传入shizijun的时候会让1/0就会报错!我们故意让他报错看看会不会中断程序!
                if (s.equals("shizijun")) {
                    int i = 1 / 0;
                }
                return Tuple2.of(s, 1);
            }
        });

        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = sandone.keyBy(0);
        SingleOutputStreamOperator<Tuple2<String, Integer>> summed = keyedStream.sum(1);
        summed.print();
        env.execute("wordcount");8888端口传入:
        shizijun
        打印台:
        报错但是不会中断程序!
    }
}
Flink提供了多种重启策略,以应对任务失败或需要重启的场景。以下是几种常见的重启策略及其配置方法: 1. **固定延迟重启策略(Fixed DelayRestartStrategy)**: - **配置方式**: ```yaml restart-strategy: fixed-delay restart-strategy.fixed-delay.attempts: 3 restart-strategy.fixed-delay.delay: 10s ``` - **说明**:该策略会在固定的时间间隔内进行多次重启尝试。每次重启之间有一个固定的延迟时间。 2. **失败率重启策略(FailureRateRestartStrategy)**: - **配置方式**: ```yaml restart-strategy: failure-rate restart-strategy.failure-rate.max-failures-per-interval: 3 restart-strategy.failure-rate.failure-rate-interval: 5min restart-strategy.failure-rate.delay: 10s ``` - **说明**:该策略在一定时间间隔内允许的最大失败次数。如果在指定的时间间隔内失败次数超过限制,则不再重启。 3. **无重启策略(NoRestartStrategy)**: - **配置方式**: ```yaml restart-strategy: none ``` - **说明**:该策略不会进行任何重启操作,任务失败后会直接停止。 4. **指数延迟重启策略(ExponentialDelayRestartStrategy)**: - **配置方式**: ```yaml restart-strategy: exponential-delay restart-strategy.exponential-delay.delay: 10s restart-strategy.exponential-delay.max-delay: 60s restart-strategy.exponential-delay.random-factor: 0.5 ``` - **说明**:该策略会根据失败的次数逐渐增加重启的延迟时间,最大延迟时间有一个上限。 5. **自定义重启策略(CustomRestartStrategy)**: - **配置方式**: ```java RestartStrategies.customRestartStrategy(FixedDelayRestartStrategyConfiguration configuration) .withMaxAttempts(3) .withDelay(Duration.ofSeconds(10)) .build(); ``` - **说明**:用户可以自定义重启策略,通过编程方式进行配置。 这些重启策略可以根据具体的应用场景和需求进行选择和配置,以确保Flink任务的稳定性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值