org.cattleframework.cloud.discovery.loadbalancer.CloudLoadBalancerClientConfiguration 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.cattleframework.cloud.discovery.LoadBalancerProperties;
import org.cattleframework.cloud.discovery.event.LoadBalancerEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
/**
* 平台负载均衡器客户端配置
*
* @author orange
*
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
public class CloudLoadBalancerClientConfiguration {
private static final int REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER = 183827460;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "cattle.cloud.loadbalancer.strategy", havingValue = "roundRobin", matchIfMissing = true)
public ReactorLoadBalancer roundRobinLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory,
@Autowired(required = false) List loadBalancerEvents) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RoundRobinCloudLoadBalancer(
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name,
loadBalancerEvents);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "cattle.cloud.loadbalancer.strategy", havingValue = "random")
public ReactorLoadBalancer randomLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory,
@Autowired(required = false) List loadBalancerEvents) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RandomCloudLoadBalancer(
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name,
loadBalancerEvents);
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnReactiveDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER)
public static class ReactiveSupportConfiguration {
@Bean
@ConditionalOnBean(ReactiveDiscoveryClient.class)
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
public ServiceInstanceListSupplier serviceInstanceListSupplier(ConfigurableApplicationContext context,
LoadBalancerProperties loadBalancerProperties) {
ServiceInstanceListSupplierBuilder serviceInstanceListSupplierBuilder = ServiceInstanceListSupplier
.builder().withDiscoveryClient();
if (loadBalancerProperties.isCache()) {
serviceInstanceListSupplierBuilder = serviceInstanceListSupplierBuilder.withCaching();
}
return serviceInstanceListSupplierBuilder.build(context);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBlockingDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER + 1)
public static class BlockingSupportConfiguration {
@Bean
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
public ServiceInstanceListSupplier serviceInstanceListSupplier(ConfigurableApplicationContext context,
LoadBalancerProperties loadBalancerProperties) {
ServiceInstanceListSupplierBuilder serviceInstanceListSupplierBuilder = ServiceInstanceListSupplier
.builder().withBlockingDiscoveryClient();
if (loadBalancerProperties.isCache()) {
serviceInstanceListSupplierBuilder = serviceInstanceListSupplierBuilder.withCaching();
}
return serviceInstanceListSupplierBuilder.build(context);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy