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

Async feign flow #511

Open
wants to merge 6 commits into
base: 2.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -145,4 +145,9 @@
*/
boolean primary() default true;

/**
* @return whether Feign will use an underlying async Http client.
*/
boolean asynchronous() default false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import feign.AsyncClient;
import feign.AsyncFeign;
import feign.AsyncFeign.AsyncBuilder;
import feign.Client;
import feign.Contract;
import feign.ExceptionPropagationPolicy;
Expand All @@ -43,6 +46,7 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.cloud.openfeign.async.AsyncTargeter;
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient;
Expand Down Expand Up @@ -99,6 +103,8 @@ public class FeignClientFactoryBean implements FactoryBean<Object>, Initializing

private boolean followRedirects = new Request.Options().isFollowRedirects();

private boolean asynchronous = false;

@Override
public void afterPropertiesSet() {
Assert.hasText(contextId, "Context id must be set");
Expand All @@ -124,6 +130,24 @@ protected Feign.Builder feign(FeignContext context) {
return builder;
}

protected AsyncBuilder asyncFeign(FeignContext context) {
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
Logger logger = loggerFactory.create(type);

// @formatter:off
AsyncBuilder builder = get(context, AsyncBuilder.class)
// required values
.logger(logger)
.encoder(get(context, Encoder.class))
.decoder(get(context, Decoder.class))
.contract(get(context, Contract.class));
// @formatter:on

configureFeign(context, builder);

return builder;
}

private void applyBuildCustomizers(FeignContext context, Feign.Builder builder) {
Map<String, FeignBuilderCustomizer> customizerMap = context
.getInstances(contextId, FeignBuilderCustomizer.class);
Expand Down Expand Up @@ -166,6 +190,36 @@ protected void configureFeign(FeignContext context, Feign.Builder builder) {
}
}

protected void configureFeign(FeignContext context, AsyncBuilder builder) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class practically duplicates entire methods, with only small differences. Please refactor it so that this does not happen. You could extract code to smaller methods, use a wrapping interface that could have Builder or AsyncBuilder delegate, or use a method with more general arguments and use instanced checks and casting to process the two types, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true that there are many duplications, I will try to make it better.

FeignClientProperties properties = beanFactory != null
? beanFactory.getBean(FeignClientProperties.class)
: applicationContext.getBean(FeignClientProperties.class);

FeignClientConfigurer feignClientConfigurer = getOptional(context,
FeignClientConfigurer.class);
setInheritParentContext(feignClientConfigurer.inheritParentConfiguration());

if (properties != null && inheritParentContext) {
if (properties.isDefaultToProperties()) {
configureUsingConfiguration(context, builder);
configureUsingProperties(
properties.getConfig().get(properties.getDefaultConfig()),
builder);
configureUsingProperties(properties.getConfig().get(contextId), builder);
}
else {
configureUsingProperties(
properties.getConfig().get(properties.getDefaultConfig()),
builder);
configureUsingProperties(properties.getConfig().get(contextId), builder);
configureUsingConfiguration(context, builder);
}
}
else {
configureUsingConfiguration(context, builder);
}
}

protected void configureUsingConfiguration(FeignContext context,
Feign.Builder builder) {
Logger.Level level = getInheritedAwareOptional(context, Logger.Level.class);
Expand Down Expand Up @@ -220,6 +274,51 @@ protected void configureUsingConfiguration(FeignContext context,
}
}

protected void configureUsingConfiguration(FeignContext context,
AsyncBuilder builder) {
Logger.Level level = getInheritedAwareOptional(context, Logger.Level.class);
if (level != null) {
builder.logLevel(level);
}
ErrorDecoder errorDecoder = getInheritedAwareOptional(context,
ErrorDecoder.class);
if (errorDecoder != null) {
builder.errorDecoder(errorDecoder);
}
else {
FeignErrorDecoderFactory errorDecoderFactory = getOptional(context,
FeignErrorDecoderFactory.class);
if (errorDecoderFactory != null) {
ErrorDecoder factoryErrorDecoder = errorDecoderFactory.create(type);
builder.errorDecoder(factoryErrorDecoder);
}
}
Request.Options options = getInheritedAwareOptional(context,
Request.Options.class);
if (options != null) {
builder.options(options);
readTimeoutMillis = options.readTimeoutMillis();
connectTimeoutMillis = options.connectTimeoutMillis();
followRedirects = options.isFollowRedirects();
}
Map<String, RequestInterceptor> requestInterceptors = getInheritedAwareInstances(
context, RequestInterceptor.class);
if (requestInterceptors != null) {
List<RequestInterceptor> interceptors = new ArrayList<>(
requestInterceptors.values());
AnnotationAwareOrderComparator.sort(interceptors);
builder.requestInterceptors(interceptors);
}
QueryMapEncoder queryMapEncoder = getInheritedAwareOptional(context,
QueryMapEncoder.class);
if (queryMapEncoder != null) {
builder.queryMapEncoder(queryMapEncoder);
}
if (decode404) {
builder.decode404();
}
}

protected void configureUsingProperties(
FeignClientProperties.FeignClientConfiguration config,
Feign.Builder builder) {
Expand Down Expand Up @@ -293,6 +392,69 @@ protected void configureUsingProperties(
}
}

protected void configureUsingProperties(
FeignClientProperties.FeignClientConfiguration config, AsyncBuilder builder) {
if (config == null) {
return;
}

if (config.getLoggerLevel() != null) {
builder.logLevel(config.getLoggerLevel());
}

connectTimeoutMillis = config.getConnectTimeout() != null
? config.getConnectTimeout() : connectTimeoutMillis;
readTimeoutMillis = config.getReadTimeout() != null ? config.getReadTimeout()
: readTimeoutMillis;
followRedirects = config.isFollowRedirects() != null ? config.isFollowRedirects()
: followRedirects;

builder.options(new Request.Options(connectTimeoutMillis, TimeUnit.MILLISECONDS,
readTimeoutMillis, TimeUnit.MILLISECONDS, followRedirects));

if (config.getErrorDecoder() != null) {
ErrorDecoder errorDecoder = getOrInstantiate(config.getErrorDecoder());
builder.errorDecoder(errorDecoder);
}

if (config.getRequestInterceptors() != null
&& !config.getRequestInterceptors().isEmpty()) {
// this will add request interceptor to builder, not replace existing
for (Class<RequestInterceptor> bean : config.getRequestInterceptors()) {
RequestInterceptor interceptor = getOrInstantiate(bean);
builder.requestInterceptor(interceptor);
}
}

if (config.getDecode404() != null) {
if (config.getDecode404()) {
builder.decode404();
}
}

if (Objects.nonNull(config.getEncoder())) {
builder.encoder(getOrInstantiate(config.getEncoder()));
}

if (Objects.nonNull(config.getDefaultRequestHeaders())) {
builder.requestInterceptor(requestTemplate -> requestTemplate
.headers(config.getDefaultRequestHeaders()));
}

if (Objects.nonNull(config.getDefaultQueryParameters())) {
builder.requestInterceptor(requestTemplate -> requestTemplate
.queries(config.getDefaultQueryParameters()));
}

if (Objects.nonNull(config.getDecoder())) {
builder.decoder(getOrInstantiate(config.getDecoder()));
}

if (Objects.nonNull(config.getContract())) {
builder.contract(getOrInstantiate(config.getContract()));
}
}

private <T> T getOrInstantiate(Class<T> tClass) {
try {
return beanFactory != null ? beanFactory.getBean(tClass)
Expand Down Expand Up @@ -350,6 +512,9 @@ protected <T> T loadBalance(Feign.Builder builder, FeignContext context,

@Override
public Object getObject() {
if (asynchronous) {
return getAsyncTarget();
}
return getTarget();
}

Expand Down Expand Up @@ -404,6 +569,33 @@ <T> T getTarget() {
new HardCodedTarget<>(type, name, url));
}

/**
* @param <T> the target type of the Feign client
* @return an {@link AsyncFeign} client created with the specified data and the
* context information
*/
<T> T getAsyncTarget() {
FeignContext context = beanFactory != null
? beanFactory.getBean(FeignContext.class)
: applicationContext.getBean(FeignContext.class);
AsyncBuilder builder = asyncFeign(context);

if (!StringUtils.hasText(url)) {
url = name;
}
if (StringUtils.hasText(url) && !url.startsWith("http")) {
url = "http://" + url;
}
String url = this.url + cleanPath();
AsyncClient client = getOptional(context, AsyncClient.class);
if (client != null) {
builder.client(client);
}
AsyncTargeter targeter = get(context, AsyncTargeter.class);
return (T) targeter.target(this, builder, context,
new HardCodedTarget<>(type, name, url));
}

private String cleanPath() {
String path = this.path.trim();
if (StringUtils.hasLength(path)) {
Expand Down Expand Up @@ -509,6 +701,14 @@ public void setFallbackFactory(Class<?> fallbackFactory) {
this.fallbackFactory = fallbackFactory;
}

public boolean isAsynchronous() {
return asynchronous;
}

public void setAsynchronous(boolean asynchronous) {
this.asynchronous = asynchronous;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -528,14 +728,15 @@ public boolean equals(Object o) {
&& Objects.equals(type, that.type) && Objects.equals(url, that.url)
&& Objects.equals(connectTimeoutMillis, that.connectTimeoutMillis)
&& Objects.equals(readTimeoutMillis, that.readTimeoutMillis)
&& Objects.equals(followRedirects, that.followRedirects);
&& Objects.equals(followRedirects, that.followRedirects)
&& Objects.equals(asynchronous, that.asynchronous);
}

@Override
public int hashCode() {
return Objects.hash(applicationContext, beanFactory, decode404,
inheritParentContext, fallback, fallbackFactory, name, path, type, url,
readTimeoutMillis, connectTimeoutMillis, followRedirects);
readTimeoutMillis, connectTimeoutMillis, followRedirects, asynchronous);
}

@Override
Expand All @@ -548,11 +749,11 @@ public String toString() {
.append("applicationContext=").append(applicationContext).append(", ")
.append("beanFactory=").append(beanFactory).append(", ")
.append("fallback=").append(fallback).append(", ")
.append("fallbackFactory=").append(fallbackFactory).append("}")
.append("connectTimeoutMillis=").append(connectTimeoutMillis).append("}")
.append("readTimeoutMillis=").append(readTimeoutMillis).append("}")
.append("followRedirects=").append(followRedirects).append("}")
.toString();
.append("fallbackFactory=").append(fallbackFactory).append(", ")
.append("connectTimeoutMillis=").append(connectTimeoutMillis).append(", ")
.append("readTimeoutMillis=").append(readTimeoutMillis).append(", ")
.append("followRedirects=").append(followRedirects).append(", ")
.append("asynchronous=").append(asynchronous).append("}").toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;

import com.netflix.hystrix.HystrixCommand;
import feign.AsyncFeign;
import feign.Contract;
import feign.Feign;
import feign.Logger;
Expand All @@ -35,6 +36,7 @@
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -158,12 +160,19 @@ public Retryer feignRetryer() {
}

@Bean
@Scope("prototype")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@ConditionalOnMissingBean
public Feign.Builder feignBuilder(Retryer retryer) {
return Feign.builder().retryer(retryer);
}

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@ConditionalOnMissingBean
thanhj marked this conversation as resolved.
Show resolved Hide resolved
public AsyncFeign.AsyncBuilder asyncFeignBuilder() {
return AsyncFeign.asyncBuilder();
}

@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,13 @@ private void registerFeignClient(BeanDefinitionRegistry registry,
? (ConfigurableBeanFactory) registry : null;
String contextId = getContextId(beanFactory, attributes);
String name = getName(attributes);
boolean asynchronous = (Boolean) attributes.get("asynchronous");
FeignClientFactoryBean factoryBean = new FeignClientFactoryBean();
factoryBean.setBeanFactory(beanFactory);
factoryBean.setName(name);
factoryBean.setContextId(contextId);
factoryBean.setType(clazz);
factoryBean.setAsynchronous(asynchronous);
BeanDefinitionBuilder definition = BeanDefinitionBuilder
.genericBeanDefinition(clazz, () -> {
factoryBean.setUrl(getUrl(beanFactory, attributes));
Expand Down Expand Up @@ -255,7 +257,6 @@ private void registerFeignClient(BeanDefinitionRegistry registry,

// has a default, won't be null
boolean primary = (Boolean) attributes.get("primary");

beanDefinition.setPrimary(primary);

String[] qualifiers = getQualifiers(attributes);
Expand Down
Loading