Skip to content

Commit

Permalink
Add LoadBalancerRestClientBuilderBeanPostProcessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Oct 31, 2023
1 parent 41ae9a4 commit cfa428e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;

/**
Expand All @@ -49,7 +51,7 @@
* @author Olga Maciaszek-Sharma
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RestTemplate.class)
@ConditionalOnClass({ RestTemplate.class, RestClient.class })
@ConditionalOnBean(LoadBalancerClient.class)
@EnableConfigurationProperties(LoadBalancerClientsProperties.class)
public class LoadBalancerAutoConfiguration {
Expand Down Expand Up @@ -99,6 +101,13 @@ public RestTemplateCustomizer restTemplateCustomizer(final LoadBalancerIntercept
};
}

@Bean
@ConditionalOnMissingBean
LoadBalancerRestClientBuilderBeanPostProcessor lbRestClientPostProcessor(
final LoadBalancerInterceptor loadBalancerInterceptor, ApplicationContext context) {
return new LoadBalancerRestClientBuilderBeanPostProcessor(loadBalancerInterceptor, context);
}

}

private static class RetryMissingOrDisabledCondition extends AnyNestedCondition {
Expand Down Expand Up @@ -164,6 +173,13 @@ public RestTemplateCustomizer restTemplateCustomizer(
};
}

@Bean
@ConditionalOnMissingBean
LoadBalancerRestClientBuilderBeanPostProcessor lbRestClientPostProcessor(
final RetryLoadBalancerInterceptor loadBalancerInterceptor, ApplicationContext context) {
return new LoadBalancerRestClientBuilderBeanPostProcessor(loadBalancerInterceptor, context);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
public interface LoadBalancerRequestTransformer {

/**
* Order for the load balancer request tranformer.
* Order for the load balancer request transformer.
*/
int DEFAULT_ORDER = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.client.loadbalancer;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestClient;

/**
* @author Olga Maciaszek-Sharma
*/
// TODO: make deferring?
public class LoadBalancerRestClientBuilderBeanPostProcessor implements BeanPostProcessor {

private final ClientHttpRequestInterceptor loadBalancerInterceptor;

private final ApplicationContext context;

public LoadBalancerRestClientBuilderBeanPostProcessor(ClientHttpRequestInterceptor loadBalancerInterceptor,
ApplicationContext context) {
this.loadBalancerInterceptor = loadBalancerInterceptor;
this.context = context;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RestClient.Builder) {
if (context.findAnnotationOnBean(beanName, LoadBalanced.class) == null) {
return bean;
}
((RestClient.Builder) bean).requestInterceptor(loadBalancerInterceptor);
}
return bean;
}

}

0 comments on commit cfa428e

Please sign in to comment.