fix check style

pull/1800/head
shedfreewu 4 months ago
parent 3a6cae6dbe
commit 9d1e5f8fb0

@ -49,11 +49,11 @@ public class FeignEagerLoadContextInitializer implements ApplicationListener<App
private static final Logger LOG = LoggerFactory.getLogger(FeignEagerLoadContextInitializer.class);
private ApplicationContext applicationContext;
private final ApplicationContext applicationContext;
private LoadBalancerClientFactory loadBalancerClientFactory;
private final LoadBalancerClientFactory loadBalancerClientFactory;
private LoadBalancerEagerLoadProperties loadBalancerEagerLoadProperties;
private final LoadBalancerEagerLoadProperties loadBalancerEagerLoadProperties;
public FeignEagerLoadContextInitializer(ApplicationContext applicationContext,
LoadBalancerClientFactory loadBalancerClientFactory,
@ -63,16 +63,46 @@ public class FeignEagerLoadContextInitializer implements ApplicationListener<App
this.loadBalancerEagerLoadProperties = loadBalancerEagerLoadProperties;
}
public static Target.HardCodedTarget<?> getHardCodedTarget(Object proxy) {
try {
int count = 0;
Object invocationHandler = proxy;
// Avoid infinite loop
while (count++ < 100) {
invocationHandler = Proxy.getInvocationHandler(invocationHandler);
if (invocationHandler instanceof AopProxy) {
invocationHandler = JdkDynamicAopProxyUtils.getTarget(invocationHandler);
continue;
}
break;
}
for (Field field : invocationHandler.getClass().getDeclaredFields()) {
field.setAccessible(true);
Object fieldValue = field.get(invocationHandler);
if (fieldValue instanceof Target.HardCodedTarget) {
return (Target.HardCodedTarget<?>) fieldValue;
}
}
}
catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug("proxy:{}, getTarget failed.", proxy, e);
}
}
return null;
}
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
LOG.info("feign eager-load start");
// Get services that are already warmed by LoadBalancerEagerContextInitializer
Set<String> skipServices = getLoadBalancerEagerLoadServices();
// Set to track already warmed services
Set<String> warmedServices = new HashSet<>();
// Warm up FeignClient services
for (Object bean : applicationContext.getBeansWithAnnotation(FeignClient.class).values()) {
try {
@ -95,7 +125,7 @@ public class FeignEagerLoadContextInitializer implements ApplicationListener<App
LOG.debug("[{}] skip eager-load, already configured in LoadBalancerEagerLoadProperties.clients", serviceName);
continue;
}
// Skip if already warmed in this round
if (warmedServices.contains(serviceName)) {
LOG.debug("[{}] already warmed, skip.", serviceName);
@ -124,41 +154,11 @@ public class FeignEagerLoadContextInitializer implements ApplicationListener<App
*/
private Set<String> getLoadBalancerEagerLoadServices() {
Set<String> services = new HashSet<>();
if (loadBalancerEagerLoadProperties != null
if (loadBalancerEagerLoadProperties != null
&& loadBalancerEagerLoadProperties.isEnabled()
&& loadBalancerEagerLoadProperties.getClients() != null) {
services.addAll(loadBalancerEagerLoadProperties.getClients());
}
return services;
}
public static Target.HardCodedTarget<?> getHardCodedTarget(Object proxy) {
try {
int count = 0;
Object invocationHandler = proxy;
// Avoid infinite loop
while (count++ < 100) {
invocationHandler = Proxy.getInvocationHandler(invocationHandler);
if (invocationHandler instanceof AopProxy) {
invocationHandler = JdkDynamicAopProxyUtils.getTarget(invocationHandler);
continue;
}
break;
}
for (Field field : invocationHandler.getClass().getDeclaredFields()) {
field.setAccessible(true);
Object fieldValue = field.get(invocationHandler);
if (fieldValue instanceof Target.HardCodedTarget) {
return (Target.HardCodedTarget<?>) fieldValue;
}
}
}
catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug("proxy:{}, getTarget failed.", proxy, e);
}
}
return null;
}
}

@ -17,32 +17,33 @@ import org.springframework.context.ApplicationListener;
public class LoadBalancerEagerContextInitializer implements ApplicationListener<ApplicationReadyEvent> {
private static final Logger LOG = LoggerFactory.getLogger(LoadBalancerEagerContextInitializer.class);
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) {
LOG.info("spring cloud eager-load start");
try {
if (!CollectionUtils.isEmpty(serviceNames)) {
for (String serviceName : serviceNames) {
LoadBalancerWarmUpUtils.warmUp(factory, serviceName);
}
}
LOG.info("spring cloud eager-load end");
} catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug("spring cloud eager-load failed.", e);
}
}
}
private static final Logger LOG = LoggerFactory.getLogger(LoadBalancerEagerContextInitializer.class);
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) {
LOG.info("spring cloud eager-load start");
try {
if (!CollectionUtils.isEmpty(serviceNames)) {
for (String serviceName : serviceNames) {
LoadBalancerWarmUpUtils.warmUp(factory, serviceName);
}
}
LOG.info("spring cloud eager-load end");
}
catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug("spring cloud eager-load failed.", e);
}
}
}
}

@ -7,24 +7,24 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.cloud.loadbalancer.eager-load")
public class LoadBalancerEagerLoadProperties {
private List<String> clients;
private List<String> clients;
private boolean enabled = true;
private boolean enabled = true;
public boolean isEnabled() {
return enabled;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public List<String> getClients() {
return clients;
}
public List<String> getClients() {
return clients;
}
public void setClients(List<String> clients) {
this.clients = clients;
}
public void setClients(List<String> clients) {
this.clients = clients;
}
}

@ -22,7 +22,7 @@ import java.lang.reflect.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JdkDynamicAopProxyUtils {
public final class JdkDynamicAopProxyUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(JdkDynamicAopProxyUtils.class);

Loading…
Cancel
Save