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

org.cattleframework.cloud.discovery.loadbalancer.RoundRobinCloudLoadBalancer Maven / Gradle / Ivy

There is a newer version: 1.0.1-M5
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cattleframework.cloud.discovery.loadbalancer;

import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import org.cattleframework.cloud.discovery.event.LoadBalancerEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;

/**
 * 轮询的平台负载均衡器
 * 
 * @author orange
 *
 */
public class RoundRobinCloudLoadBalancer extends BaseCloudLoadBalancer {

    private static final Logger logger = LoggerFactory.getLogger(RoundRobinCloudLoadBalancer.class);

    private final AtomicInteger position;

    public RoundRobinCloudLoadBalancer(ObjectProvider serviceInstanceListSupplierProvider,
	    String serviceId, List loadBalancerEvents) {
	this(serviceInstanceListSupplierProvider, serviceId, new Random().nextInt(1000), loadBalancerEvents);
    }

    public RoundRobinCloudLoadBalancer(ObjectProvider serviceInstanceListSupplierProvider,
	    String serviceId, int seedPosition, List loadBalancerEvents) {
	super(serviceInstanceListSupplierProvider, serviceId, loadBalancerEvents);
	this.position = new AtomicInteger(seedPosition);
    }

    @Override
    Response getInstanceResponse(List instances) {
	if (instances.isEmpty()) {
	    if (logger.isWarnEnabled()) {
		logger.warn("No servers available for service: " + serviceId);
	    }
	    return new EmptyResponse();
	}
	// Do not move position when there is only 1 instance, especially some suppliers
	// have already filtered instances
	if (instances.size() == 1) {
	    return new DefaultResponse(instances.get(0));
	}
	// Ignore the sign bit, this allows pos to loop sequentially from 0 to
	// Integer.MAX_VALUE
	int pos = this.position.incrementAndGet() & Integer.MAX_VALUE;
	ServiceInstance instance = instances.get(pos % instances.size());
	return new DefaultResponse(instance);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy