org.cattleframework.cloud.discovery.loadbalancer.RandomCloudLoadBalancer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cattle-cloud-discovery-common Show documentation
Show all versions of cattle-cloud-discovery-common Show documentation
Cattle framework cloud discovery common component pom
/*
* 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.concurrent.ThreadLocalRandom;
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 RandomCloudLoadBalancer extends BaseCloudLoadBalancer {
private static final Logger logger = LoggerFactory.getLogger(RandomCloudLoadBalancer.class);
public RandomCloudLoadBalancer(ObjectProvider serviceInstanceListSupplierProvider,
String serviceId, List loadBalancerEvents) {
super(serviceInstanceListSupplierProvider, serviceId, loadBalancerEvents);
}
@Override
Response getInstanceResponse(List instances) {
if (instances.isEmpty()) {
if (logger.isWarnEnabled()) {
logger.warn("No servers available for service: " + serviceId);
}
return new EmptyResponse();
}
int index = ThreadLocalRandom.current().nextInt(instances.size());
ServiceInstance instance = instances.get(index);
return new DefaultResponse(instance);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy