Implemented clients eager load

Fixes gh-729

# Conflicts:
#	spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/config/LoadBalancerAutoConfiguration.java
#	spring-cloud-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json
This commit is contained in:
Andrii Bohutskyi
2021-03-17 14:39:02 +02:00
committed by Olga Maciaszek-Sharma
parent 0b5789f6ce
commit d84c772b7e
6 changed files with 195 additions and 0 deletions

View File

@@ -59,6 +59,8 @@
|spring.cloud.loadbalancer.sticky-session.instance-id-cookie-name | `+++sc-lb-instance-id+++` | The name of the cookie holding the preferred instance id.
|spring.cloud.loadbalancer.x-forwarded.enabled | `+++false+++` | To Enable X-Forwarded Headers.
|spring.cloud.loadbalancer.zone | | Spring Cloud LoadBalancer zone.
|spring.cloud.loadbalancer.eager-load.enabled| | Enables eager load of client context.
|spring.cloud.loadbalancer.eager-load.clients | |
|spring.cloud.refresh.additional-property-sources-to-retain | | Additional property sources to retain during a refresh. Typically only system property sources are retained. This property allows property sources, such as property sources created by EnvironmentPostProcessors to be retained as well.
|spring.cloud.refresh.enabled | `+++true+++` | Enables autoconfiguration for the refresh scope and associated features.
|spring.cloud.refresh.extra-refreshable | `+++true+++` | Additional class names for beans to post process into refresh scope.

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2021 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
*
* https://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.springframework.cloud.client.loadbalancer;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Andrii Bohutskyi
*/
@ConfigurationProperties("spring.cloud.loadbalancer.eager-load")
public class LoadbalancerEagerLoadProperties {
private List<String> clients = new ArrayList<>();
public List<String> getClients() {
return clients;
}
public void setClients(List<String> clients) {
this.clients = clients;
}
}

View File

@@ -25,11 +25,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClientsProperties;
import org.springframework.cloud.client.loadbalancer.LoadbalancerEagerLoadProperties;
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerClientAutoConfiguration;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientSpecification;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.loadbalancer.support.LoadBalancerEagerContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@@ -66,4 +68,10 @@ public class LoadBalancerAutoConfiguration {
return clientFactory;
}
@Bean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.eager-load.enabled", havingValue = "true")
public LoadBalancerEagerContextInitializer loadBalancerEagerContextInitializer(
LoadBalancerClientFactory clientFactory, LoadbalancerEagerLoadProperties properties) {
return new LoadBalancerEagerContextInitializer(clientFactory, properties.getClients());
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2012-2021 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
*
* https://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.springframework.cloud.loadbalancer.support;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import java.util.List;
/**
* @author Andrii Bohutskyi
*/
public class LoadBalancerEagerContextInitializer implements ApplicationListener<ApplicationReadyEvent> {
private final LoadBalancerClientFactory factory;
private final List<String> serviceNames;
public LoadBalancerEagerContextInitializer(LoadBalancerClientFactory factory, List<String> serviceNames) {
this.factory = factory;
this.serviceNames = serviceNames;
}
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
warnUpFunction();
}
private void warnUpFunction() {
this.serviceNames.forEach(factory::getInstance);
}
}

View File

@@ -33,6 +33,16 @@
"name": "spring.cloud.loadbalancer.enabled",
"description": "Enables Spring Cloud LoadBalancer.",
"type": "java.lang.Boolean"
},
{
"name": "spring.cloud.loadbalancer.eager-load.enabled",
"description": "Enables eager load of clients context.",
"type": "java.lang.Boolean"
},
{
"name": "spring.cloud.loadbalancer.eager-load.clients",
"description": "Names of the clients.",
"type": "java.util.List"
}
]
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2012-2021 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
*
* https://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.springframework.cloud.loadbalancer.support;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Andrii Bohutskyi
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {
LoadBalancerAutoConfiguration.class,
LoadBalancerEagerContextInitializerTest.SomeTestConfig.class
})
class LoadBalancerEagerContextInitializerTest {
private static final String LOAD_BALANCER = "testLoadBalancer";
@Autowired
private LoadBalancerClientFactory factory;
@Test
void testLoadBalancerInstanceShouldBeInitialized() {
assertThat(LoadBalancerCounter.getCount()).isEqualTo(1);
factory.getInstance(LOAD_BALANCER);
assertThat(LoadBalancerCounter.getCount()).isEqualTo(1);
}
@Configuration(proxyBeanMethods = false)
@LoadBalancerClient(name = LOAD_BALANCER, configuration = TestLoadBalancerConfiguration.class)
static class SomeTestConfig {
@Bean
public LoadBalancerEagerContextInitializer loadBalancerEagerContextInitializer(
LoadBalancerClientFactory factory) {
return new LoadBalancerEagerContextInitializer(factory, Collections.singletonList(LOAD_BALANCER));
}
}
static class TestLoadBalancerConfiguration {
@Bean
public LoadBalancerCounter loadBalancerCounter() {
return new LoadBalancerCounter();
}
}
static class LoadBalancerCounter {
private static final AtomicInteger COUNT = new AtomicInteger();
public LoadBalancerCounter() {
COUNT.incrementAndGet();
}
public static int getCount() {
return COUNT.get();
}
}
}