Skip to content

feat:call choose method when eagerly initializing the load-balancers. #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@

package org.springframework.cloud.loadbalancer.support;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.context.ApplicationListener;
import reactor.core.publisher.Mono;

import java.util.List;

/**
* @author Andrii Bohutskyi
* @author Andrii Bohutskyi, Haotian Zhang
*/
public class LoadBalancerEagerContextInitializer implements ApplicationListener<ApplicationReadyEvent> {

private static final Log log = LogFactory.getLog(LoadBalancerEagerContextInitializer.class);

private final LoadBalancerClientFactory factory;

private final List<String> serviceNames;
Expand All @@ -37,7 +45,17 @@ public LoadBalancerEagerContextInitializer(LoadBalancerClientFactory factory, Li

@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
serviceNames.forEach(factory::getInstance);
serviceNames.forEach(serviceName -> {
ReactiveLoadBalancer<ServiceInstance> loadBalancer = factory.getInstance(serviceName);
if (loadBalancer != null) {
Response<ServiceInstance> loadBalancerResponse = Mono.from(loadBalancer.choose()).block();
if (log.isDebugEnabled() && loadBalancerResponse != null) {
log.debug("LoadBalancer for service: " + serviceName + " initialized with chosen instance: "
+ loadBalancerResponse.getServer().getHost() + ":" + loadBalancerResponse.getServer().getPort());
}
log.info("LoadBalancer for service: " + serviceName + " initialized");
}
});
}

}