org.cattleframework.cloud.discovery.loadbalancer.BaseCloudLoadBalancer 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 org.apache.commons.collections4.CollectionUtils;
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.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.RequestData;
import org.springframework.cloud.client.loadbalancer.RequestDataContext;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import reactor.core.publisher.Mono;
abstract class BaseCloudLoadBalancer implements ReactorServiceInstanceLoadBalancer {
private static final Logger logger = LoggerFactory.getLogger(BaseCloudLoadBalancer.class);
/**
* 获得实例响应
*
* @param instances 实例
* @return 实例响应
*/
abstract Response getInstanceResponse(List instances);
protected final String serviceId;
private ObjectProvider serviceInstanceListSupplierProvider;
private final List loadBalancerEvents;
public BaseCloudLoadBalancer(ObjectProvider serviceInstanceListSupplierProvider,
String serviceId, List loadBalancerEvents) {
this.serviceId = serviceId;
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
this.loadBalancerEvents = loadBalancerEvents;
}
@SuppressWarnings("rawtypes")
@Override
public Mono> choose(Request request) {
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
.getIfAvailable(NoopServiceInstanceListSupplier::new);
return supplier.get(request).next().map(serviceInstances -> {
if (serviceInstances.isEmpty()) {
logger.warn("No servers available for service: " + serviceId);
return new EmptyResponse();
}
filterInstances(request, serviceInstances);
return processInstanceResponse(supplier, serviceInstances);
});
}
@SuppressWarnings("rawtypes")
private void filterInstances(Request request, List serviceInstances) {
RequestDataContext requestDataContext = request.getContext() != null
&& request.getContext() instanceof RequestDataContext ? (RequestDataContext) request.getContext()
: null;
RequestData requestData = requestDataContext != null ? requestDataContext.getClientRequest() : null;
if (CollectionUtils.isNotEmpty(loadBalancerEvents)) {
loadBalancerEvents.forEach(loadBalancerEvent -> {
logger.debug("执行负载均衡器事件类:{}", loadBalancerEvent.getClass().getName());
loadBalancerEvent.onFilter(requestData, serviceId, serviceInstances);
});
}
}
private Response processInstanceResponse(ServiceInstanceListSupplier supplier,
List serviceInstances) {
Response serviceInstanceResponse = getInstanceResponse(serviceInstances);
if (supplier instanceof SelectedInstanceCallback && serviceInstanceResponse.hasServer()) {
((SelectedInstanceCallback) supplier).selectedServiceInstance(serviceInstanceResponse.getServer());
}
return serviceInstanceResponse;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy