美文网首页
简单的熔断 java实现 CircuitBreaker

简单的熔断 java实现 CircuitBreaker

作者: 乘以零 | 来源:发表于2018-11-16 10:25 被阅读0次

接口 熔断 严格来说应该间隔一定时间后在开放请求 参考第三个熔断

/**
 * 
 * CircuitBreaker
 * 
 * @author xy.z@qq.com
 *
 * @since 2018年11月16日 上午10:11:07
 */
public interface CircuitBreaker<T> {
    T execute();
}

1. 基于qps的熔断 即单位时间请求数不能超过阈值 否则熔断 返回默认的failback方法

package com.multiplyzero.mz.core.rpc;

import java.util.concurrent.TimeUnit;

import com.multiplyzero.mz.core.function.Supplier;

/**
 * 
 * QpsCircuitBreaker
 * 
 * @author xy.z@qq.com
 *
 * @since 2018年11月16日 上午10:11:30
 */
public class QpsCircuitBreaker<T> implements CircuitBreaker<T> {

    public Supplier<T> function;
    public Supplier<T> failback;

    private QpsRateLimiter rateLimiter;

    public QpsCircuitBreaker(Supplier<T> function, Supplier<T> failback, int rate, long interval, TimeUnit unit) {
        this.rateLimiter = new QpsRateLimiter(rate, interval, unit);
        this.failback = failback;
        this.function = function;
    }

    public QpsCircuitBreaker(Supplier<T> function, int rate, long interval, TimeUnit unit) {
        this(function, null, rate, interval, unit);
    }

    @Override
    public T execute() {
        if (this.rateLimiter.allowable()) {
            return function.get();
        }
        if (failback != null) {
            return failback.get();
        }
        return null;
    }
}

2. 基于并发的熔断 即并发数不能超过阈值 否则返回默认值

package com.multiplyzero.mz.core.rpc;

import com.multiplyzero.mz.core.function.Supplier;

/**
 * 
 * ExecuteCircuitBreaker
 * 
 * @author xy.z@qq.com
 *
 * @since 2018年11月16日 上午10:11:18
 */
public class ExecuteCircuitBreaker<T> implements CircuitBreaker<T> {

    public Supplier<T> function;
    public Supplier<T> failback;

    private ExecuteRateLimiter rateLimiter;

    public ExecuteCircuitBreaker(Supplier<T> function, Supplier<T> failback, int rate) {
        this.rateLimiter = new ExecuteRateLimiter(rate);
        this.failback = failback;
        this.function = function;
    }

    public ExecuteCircuitBreaker(Supplier<T> function, int rate) {
        this(function, null, rate);
    }

    @Override
    public T execute() {
        if (this.rateLimiter.allowable()) {
            try {
                T t = function.get();
                return t;
            } finally {
                this.rateLimiter.release();
            }
        }
        if (failback != null) {
            return failback.get();
        }
        return null;
    }
}

3. 基于异常的熔断 即单位时间内 报错数量超过阈值 熔断 返回默认值 并且一直不可访问 然后间隔一定时间后开放接口

其实 ExecuteCircuitBreaker QpsCircuitBreaker 严格来说也应该按照这种模式 间隔一定时间后在开放请求
package com.multiplyzero.mz.core.rpc;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import com.multiplyzero.mz.core.function.Supplier;

/**
 * 
 * ExceptionCircuitBreaker
 * 
 * @author xy.z@qq.com
 *
 * @since 2018年11月16日 上午10:11:13
 */
public class ExceptionCircuitBreaker<T> implements CircuitBreaker<T> {

    public Supplier<T> function;
    public Supplier<T> failback;

    private QpsRateLimiter rateLimiter;

    private long delay;

    private boolean allowable = true;

    public ExceptionCircuitBreaker(Supplier<T> function, Supplier<T> failback, int rate, long interval,
            TimeUnit intervalUnit, long delay, TimeUnit delayUnit) {
        this.rateLimiter = new QpsRateLimiter(rate, interval, intervalUnit);
        this.failback = failback;
        this.function = function;
        this.delay = delayUnit.toMillis(delay);
    }

    public ExceptionCircuitBreaker(Supplier<T> function, int rate, long interval, TimeUnit intervalUnit, long delay,
            TimeUnit delayUnit) {
        this(function, null, rate, interval, intervalUnit, delay, delayUnit);
    }

    @Override
    public T execute() {
        try {
            if (allowable) {
                return function.get();
            }
        } catch (Exception e) {
            boolean b = this.rateLimiter.allowable();
            if (!b) {
                allowable = false;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        allowable = true;
                    }
                }, delay);
            }
        }
        if (failback != null) {
            return failback.get();
        }
        return null;

    }
}


相关文章

网友评论

      本文标题:简单的熔断 java实现 CircuitBreaker

      本文链接:https://www.haomeiwen.com/subject/cxbpfqtx.html