Skip to content

Commit

Permalink
Merge pull request #38 from Kurok1/dev
Browse files Browse the repository at this point in the history
support feign load component from main beanFactory
  • Loading branch information
mercyblitz authored Jan 16, 2025
2 parents 931dfa2 + 2062532 commit f4f8c16
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.microsphere.spring.cloud.openfeign;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.cloud.openfeign.FeignClientSpecification;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 0.0.1
*/
public class FeignComponentProvider implements BeanFactoryAware {

private final NamedContextFactory<FeignClientSpecification> contextFactory;

private BeanFactory mainBeanFactory;

public FeignComponentProvider(NamedContextFactory<FeignClientSpecification> contextFactory) {
this.contextFactory = contextFactory;
}

public <T> T getInstance(String contextName, Class<T> type) {
T component = this.contextFactory.getInstance(contextName, type);
if (component == null && mainBeanFactory instanceof AutowireCapableBeanFactory) {
//create new
return ((AutowireCapableBeanFactory)this.mainBeanFactory).createBean(type);
}
return component;
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.mainBeanFactory = beanFactory;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.microsphere.spring.cloud.openfeign.autoconfigure;

import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import io.microsphere.spring.cloud.openfeign.autorefresh.FeignClientConfigurationChangedListener;
import io.microsphere.spring.cloud.openfeign.autorefresh.FeignComponentRegistry;
import io.microsphere.spring.cloud.openfeign.components.NoOpRequestInterceptor;
Expand All @@ -9,8 +10,10 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.cloud.openfeign.FeignBuilderCustomizer;
import org.springframework.cloud.openfeign.FeignClientProperties;
import org.springframework.cloud.openfeign.FeignClientSpecification;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
Expand All @@ -25,6 +28,7 @@
* @since 0.0.1
*/
@ConditionalOnBean(EnableFeignAutoRefresh.Marker.class)
//@AutoConfigureAfter(ConfigurationPropertiesRebinderAutoConfiguration.class)
public class FeignClientAutoRefreshAutoConfiguration {

@Bean
Expand Down Expand Up @@ -52,6 +56,11 @@ public FeignClientSpecificationPostProcessor feignClientSpecificationPostProcess
return new FeignClientSpecificationPostProcessor();
}

@Bean
public FeignComponentProvider feignComponentProvider(NamedContextFactory<FeignClientSpecification> contextFactory) {
return new FeignComponentProvider(contextFactory);
}

private void registerFeignClientConfigurationChangedListener(ApplicationReadyEvent event) {
ConfigurableApplicationContext context = event.getApplicationContext();
FeignComponentRegistry feignComponentRegistry = context.getBean(FeignComponentRegistry.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package io.microsphere.spring.cloud.openfeign.autorefresh;

import feign.Capability;
import feign.Contract;
import feign.QueryMapEncoder;
import feign.RequestInterceptor;
import feign.Retryer;
import feign.*;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import io.microsphere.spring.cloud.openfeign.components.DecoratedContract;
import io.microsphere.spring.cloud.openfeign.components.DecoratedDecoder;
import io.microsphere.spring.cloud.openfeign.components.DecoratedEncoder;
import io.microsphere.spring.cloud.openfeign.components.DecoratedErrorDecoder;
import io.microsphere.spring.cloud.openfeign.components.DecoratedFeignComponent;
import io.microsphere.spring.cloud.openfeign.components.DecoratedQueryMapEncoder;
import io.microsphere.spring.cloud.openfeign.components.DecoratedRetryer;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import io.microsphere.spring.cloud.openfeign.components.*;
import org.springframework.beans.BeansException;
import org.springframework.cloud.openfeign.FeignClientFactory;
import org.springframework.cloud.openfeign.FeignClientProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -28,22 +18,22 @@
public class AutoRefreshCapability implements Capability, ApplicationContextAware {

private final FeignComponentRegistry componentRegistry;
private final FeignClientFactory feignClientFactory;
//private final FeignClientFactory feignClientFactory;
private final FeignComponentProvider componentProvider;
private final FeignClientProperties clientProperties;

private String contextId;

private static final String CONTEXT_ID_PROPERTY_NAME = "spring.cloud.openfeign.client.name";

public AutoRefreshCapability(FeignClientProperties clientProperties, FeignClientFactory feignClientFactory, FeignComponentRegistry componentRegistry) {
public AutoRefreshCapability(FeignClientProperties clientProperties, FeignComponentProvider componentProvider, FeignComponentRegistry componentRegistry) {
this.clientProperties = clientProperties;
this.feignClientFactory = feignClientFactory;
this.componentProvider = componentProvider;
this.componentRegistry = componentRegistry;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.contextId = applicationContext.getEnvironment().getProperty(CONTEXT_ID_PROPERTY_NAME);
this.contextId = applicationContext.getEnvironment().getProperty("spring.cloud.openfeign.client.name");
}


Expand All @@ -53,7 +43,7 @@ public Retryer enrich(Retryer retryer) {
return null;

DecoratedRetryer decoratedRetryer = DecoratedFeignComponent.instantiate(DecoratedRetryer.class, Retryer.class,
contextId, feignClientFactory, clientProperties, retryer);
contextId, componentProvider, clientProperties, retryer);

this.componentRegistry.register(contextId, decoratedRetryer);
return decoratedRetryer;
Expand All @@ -65,7 +55,7 @@ public Contract enrich(Contract contract) {
return null;

DecoratedContract decoratedContract = DecoratedFeignComponent.instantiate(DecoratedContract.class, Contract.class,
contextId, feignClientFactory, clientProperties, contract);
contextId, componentProvider, clientProperties, contract);
this.componentRegistry.register(contextId, decoratedContract);
return decoratedContract;
}
Expand All @@ -76,7 +66,7 @@ public Decoder enrich(Decoder decoder) {
return null;

DecoratedDecoder decoratedDecoder = DecoratedFeignComponent.instantiate(DecoratedDecoder.class, Decoder.class,
contextId, feignClientFactory, clientProperties, decoder);
contextId, componentProvider, clientProperties, decoder);
this.componentRegistry.register(contextId, decoratedDecoder);
return decoratedDecoder;
}
Expand All @@ -87,7 +77,7 @@ public Encoder enrich(Encoder encoder) {
return null;

DecoratedEncoder decoratedEncoder = DecoratedFeignComponent.instantiate(DecoratedEncoder.class, Encoder.class,
contextId, feignClientFactory, clientProperties, encoder);
contextId, componentProvider, clientProperties, encoder);
this.componentRegistry.register(contextId, decoratedEncoder);
return decoratedEncoder;
}
Expand All @@ -97,7 +87,7 @@ public ErrorDecoder enrich(ErrorDecoder decoder) {
return null;

DecoratedErrorDecoder decoratedErrorDecoder = DecoratedFeignComponent.instantiate(DecoratedErrorDecoder.class, ErrorDecoder.class,
contextId, feignClientFactory, clientProperties, decoder);
contextId, componentProvider, clientProperties, decoder);
this.componentRegistry.register(contextId, decoratedErrorDecoder);
return decoratedErrorDecoder;
}
Expand All @@ -113,7 +103,7 @@ public QueryMapEncoder enrich(QueryMapEncoder queryMapEncoder) {
return null;

DecoratedQueryMapEncoder decoratedQueryMapEncoder = DecoratedFeignComponent.instantiate(DecoratedQueryMapEncoder.class, QueryMapEncoder.class,
contextId, feignClientFactory, clientProperties, queryMapEncoder);
contextId, componentProvider, clientProperties, queryMapEncoder);

this.componentRegistry.register(contextId, decoratedQueryMapEncoder);
return decoratedQueryMapEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import feign.Contract;
import feign.MethodMetadata;
import org.springframework.cloud.openfeign.FeignClientFactory;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.springframework.cloud.openfeign.FeignClientProperties;

import java.util.List;
Expand All @@ -13,8 +13,8 @@
*/
public class DecoratedContract extends DecoratedFeignComponent<Contract> implements Contract {

public DecoratedContract(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, Contract delegate) {
super(contextId, feignClientFactory, clientProperties, delegate);
public DecoratedContract(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, Contract delegate) {
super(contextId, feignComponentProvider, clientProperties, delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import feign.Response;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.springframework.cloud.openfeign.FeignClientFactory;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.springframework.cloud.openfeign.FeignClientProperties;

import java.io.IOException;
Expand All @@ -16,8 +16,8 @@
*/
public class DecoratedDecoder extends DecoratedFeignComponent<Decoder> implements Decoder {

public DecoratedDecoder(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, Decoder delegate) {
super(contextId, feignClientFactory, clientProperties, delegate);
public DecoratedDecoder(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, Decoder delegate) {
super(contextId, feignComponentProvider, clientProperties, delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import org.springframework.cloud.openfeign.FeignClientFactory;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.springframework.cloud.openfeign.FeignClientProperties;

import java.lang.reflect.Type;
Expand All @@ -14,8 +14,8 @@
*/
public class DecoratedEncoder extends DecoratedFeignComponent<Encoder> implements Encoder {

public DecoratedEncoder(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, Encoder delegate) {
super(contextId, feignClientFactory, clientProperties, delegate);
public DecoratedEncoder(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, Encoder delegate) {
super(contextId, feignComponentProvider, clientProperties, delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import feign.Response;
import feign.codec.ErrorDecoder;
import org.springframework.cloud.openfeign.FeignClientFactory;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.springframework.cloud.openfeign.FeignClientProperties;

/**
Expand All @@ -11,8 +11,8 @@
*/
public class DecoratedErrorDecoder extends DecoratedFeignComponent<ErrorDecoder> implements ErrorDecoder {

public DecoratedErrorDecoder(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, ErrorDecoder delegate) {
super(contextId, feignClientFactory, clientProperties, delegate);
public DecoratedErrorDecoder(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, ErrorDecoder delegate) {
super(contextId, feignComponentProvider, clientProperties, delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.microsphere.spring.cloud.openfeign.components;

import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClientFactory;
import org.springframework.cloud.openfeign.FeignClientProperties;
import org.springframework.lang.NonNull;

Expand All @@ -19,7 +19,8 @@ public abstract class DecoratedFeignComponent<T> implements Refreshable {

private final Logger log = LoggerFactory.getLogger(getClass());

private final FeignClientFactory feignClientFactory;
//private final FeignClientFactory feignClientFactory;
private final FeignComponentProvider componentProvider;
private final String contextId;

private final FeignClientProperties clientProperties;
Expand All @@ -32,9 +33,9 @@ public abstract class DecoratedFeignComponent<T> implements Refreshable {
private final ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
private final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();

public DecoratedFeignComponent(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, T delegate) {
public DecoratedFeignComponent(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, T delegate) {
this.contextId = contextId;
this.feignClientFactory = feignClientFactory;
this.componentProvider = feignComponentProvider;
this.clientProperties = clientProperties;
this.delegate = delegate;
}
Expand All @@ -52,9 +53,9 @@ public T delegate() {
}

@NonNull
public FeignClientFactory getFeignClientFactory() {
return this.feignClientFactory;
}
public FeignComponentProvider getFeignComponentProvider() {
return this.componentProvider;
};

@NonNull
public String contextId() {
Expand Down Expand Up @@ -83,7 +84,7 @@ protected T loadInstance() {
String contextId = contextId();
writeLock.lock();
try {
T component = getFeignClientFactory().getInstance(contextId, componentType);
T component = getFeignComponentProvider().getInstance(contextId, componentType);
if (component == null)
component = BeanUtils.instantiateClass(componentType);
this.delegate = component;
Expand Down Expand Up @@ -112,10 +113,10 @@ public String toString() {
}

public static <W extends DecoratedFeignComponent<T>, T> W instantiate(Class<W> decoratedClass, Class<T> componentClass,
String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, T delegate) {
String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, T delegate) {
try {
Constructor<W> constructor = decoratedClass.getConstructor(String.class, FeignClientFactory.class, FeignClientProperties.class, componentClass);
return BeanUtils.instantiateClass(constructor, contextId, feignClientFactory, clientProperties, delegate);
Constructor<W> constructor = decoratedClass.getConstructor(String.class, FeignComponentProvider.class, FeignClientProperties.class, componentClass);
return BeanUtils.instantiateClass(constructor, contextId, feignComponentProvider, clientProperties, delegate);
} catch (NoSuchMethodException noSuchMethodException) {
throw new BeanInstantiationException(decoratedClass, noSuchMethodException.getLocalizedMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import feign.QueryMapEncoder;
import io.microsphere.logging.Logger;
import org.springframework.cloud.openfeign.FeignClientFactory;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.springframework.cloud.openfeign.FeignClientProperties;

import java.lang.invoke.MethodHandle;
Expand All @@ -23,8 +23,8 @@ public class DecoratedQueryMapEncoder extends DecoratedFeignComponent<QueryMapEn

private static final MethodHandle getQueryMapEncoderMethodHandle = findVirtual(FeignClientProperties.FeignClientConfiguration.class, getQueryMapEncoderMethodName);

public DecoratedQueryMapEncoder(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, QueryMapEncoder delegate) {
super(contextId, feignClientFactory, clientProperties, delegate);
public DecoratedQueryMapEncoder(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, QueryMapEncoder delegate) {
super(contextId, feignComponentProvider, clientProperties, delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import feign.RetryableException;
import feign.Retryer;
import org.springframework.cloud.openfeign.FeignClientFactory;
import io.microsphere.spring.cloud.openfeign.FeignComponentProvider;
import org.springframework.cloud.openfeign.FeignClientProperties;

/**
Expand All @@ -11,8 +11,8 @@
*/
public class DecoratedRetryer extends DecoratedFeignComponent<Retryer> implements Retryer {

public DecoratedRetryer(String contextId, FeignClientFactory feignClientFactory, FeignClientProperties clientProperties, Retryer delegate) {
super(contextId, feignClientFactory, clientProperties, delegate);
public DecoratedRetryer(String contextId, FeignComponentProvider feignComponentProvider, FeignClientProperties clientProperties, Retryer delegate) {
super(contextId, feignComponentProvider, clientProperties, delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.cloud.openfeign.EnableFeignClients;
Expand Down Expand Up @@ -72,7 +69,6 @@ public void replaceConfig() {
public void testInternal() {
try {
this.client.echo("hello", "1.0");

} catch (Exception ignored) {

}
Expand Down
Loading

0 comments on commit f4f8c16

Please sign in to comment.