Skip to content
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

feign client eager load #36

Open
MI-cool opened this issue Sep 2, 2022 · 1 comment
Open

feign client eager load #36

MI-cool opened this issue Sep 2, 2022 · 1 comment

Comments

@MI-cool
Copy link

MI-cool commented Sep 2, 2022

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

@component
public class FeignClientEagerLoadConfig implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor, ApplicationListener {

private ApplicationContext applicationContext;

private BeanDefinitionRegistry beanDefinitionRegistry;

@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
	this.applicationContext = applicationContext;
}

@Override
public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry registry) throws BeansException {
	this.beanDefinitionRegistry = registry;
}

@Override
public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException {

}

@Override
public void onApplicationEvent(@NonNull ApplicationStartedEvent event) {
	CachingSpringLoadBalancerFactory loadBalancerFactory = applicationContext.getBean(CachingSpringLoadBalancerFactory.class);
	String[] beanNames = applicationContext.getBeanNamesForAnnotation(FeignClient.class);
	for (String name : beanNames) {
		Object feignClientNameObj = beanDefinitionRegistry.getBeanDefinition(name).getPropertyValues().get("name");
		if (feignClientNameObj != null) {
			loadBalancerFactory.create(feignClientNameObj.toString());
		}
	}
}

}

@MI-cool
Copy link
Author

MI-cool commented Sep 2, 2022

我看Spring Cloud TSF Finchley有实现feign eager load,感觉稍微有些复杂。
或许可以用上文的方法?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant