All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fashionbrot.ribbon.loadbalancer.BaseLoadBalancer Maven / Gradle / Ivy

Go to download

mars-ribbon-api 负载均衡工具 https://github.com/fashionbrot/mars-dynamic-config

There is a newer version: 0.1.3
Show newest version
package com.github.fashionbrot.ribbon.loadbalancer;

import com.github.fashionbrot.ribbon.ping.IPing;
import com.github.fashionbrot.ribbon.ping.PingUrl;
import com.github.fashionbrot.ribbon.rule.IRule;
import com.github.fashionbrot.ribbon.rule.RoundRobinRule;
import com.github.fashionbrot.ribbon.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author fashionbrot
 * @version 0.1.0
 * @date 2019/12/8 22:45
 */
@Slf4j
public class BaseLoadBalancer  implements ILoadBalancer {

    protected volatile List allServerList = Collections.synchronizedList(new ArrayList());

    protected ReadWriteLock allServerLock = new ReentrantReadWriteLock();

    protected IRule rule = new RoundRobinRule();

    protected IPing ping = new PingUrl();

    protected boolean enableLog = false;


    @Override
    public void addServers(List newServers) {
        if (CollectionUtil.isNotEmpty(newServers)){
            ArrayList newList = new ArrayList();
            newList.addAll(allServerList);
            newList.addAll(newServers);
            setServersList(newList);
        }
    }


    @Override
    public List getAllServers() {
        return allServerList;
    }

    @Override
    public Server chooseServer() {
        Lock lock = allServerLock.readLock();
        lock.lock();
        try {
            return rule.choose(this);
        }finally {
            lock.unlock();
        }

    }

    @Override
    public void setRule(IRule rule) {
        if (rule!=null){
            this.rule = rule;
        }
    }

    @Override
    public IRule getRule() {
        return this.rule;
    }

    @Override
    public void setPing(IPing ping) {
        if (ping!=null){
            this.ping = ping;
        }
    }

    @Override
    public IPing getPing() {
        return this.ping;
    }

    @Override
    public boolean enableLog() {
        return this.enableLog;
    }

    @Override
    public void setEnableLog(boolean enableLog) {
        this.enableLog = enableLog;
    }


    /**
     * Set the list of servers used as the server pool. This overrides existing
     * server list.
     */
    public void setServersList(List lsrv) {
        Lock writeLock = allServerLock.writeLock();

        writeLock.lock();
        try {
            ArrayList allServers = new ArrayList();
            for (Server server : lsrv) {
                if (server == null) {
                    continue;
                }
                allServers.add(server);
            }

            // This will reset readyToServe flag to true on all servers
            // regardless whether
            // previous priming connections are success or not
            allServerList = allServers;
        } finally {
            writeLock.unlock();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy