-
Notifications
You must be signed in to change notification settings - Fork 700
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
Added classes for async load balancer interceptor #149
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...ava/org/springframework/cloud/client/loadbalancer/AsyncLoadBalancerAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2013-2016 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 | ||
* | ||
* http://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.factory.SmartInitializingSingleton; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.AsyncClientHttpRequestInterceptor; | ||
import org.springframework.web.client.AsyncRestTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Auto configuration for Ribbon (client side load balancing). | ||
* | ||
* @author Rob Worsnop | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass(AsyncRestTemplate.class) | ||
@ConditionalOnBean(LoadBalancerClient.class) | ||
@EnableConfigurationProperties(LoadBalancerRetryProperties.class) | ||
public class AsyncLoadBalancerAutoConfiguration { | ||
|
||
@LoadBalanced | ||
@Autowired(required = false) | ||
private List<AsyncRestTemplate> restTemplates = Collections.emptyList(); | ||
|
||
@Bean | ||
public SmartInitializingSingleton loadBalancedRestTemplateInitializer( | ||
final List<AsyncRestTemplateCustomizer> customizers) { | ||
return new SmartInitializingSingleton() { | ||
@Override | ||
public void afterSingletonsInstantiated() { | ||
for (AsyncRestTemplate restTemplate : AsyncLoadBalancerAutoConfiguration.this.restTemplates) { | ||
for (AsyncRestTemplateCustomizer customizer : customizers) { | ||
customizer.customize(restTemplate); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Configuration | ||
static class LoadBalancerInterceptorConfig { | ||
@Bean | ||
public AsyncLoadBalancerInterceptor ribbonInterceptor(LoadBalancerClient loadBalancerClient) { | ||
return new AsyncLoadBalancerInterceptor(loadBalancerClient); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public AsyncRestTemplateCustomizer restTemplateCustomizer( | ||
final AsyncLoadBalancerInterceptor loadBalancerInterceptor) { | ||
return new AsyncRestTemplateCustomizer() { | ||
@Override | ||
public void customize(AsyncRestTemplate restTemplate) { | ||
List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>( | ||
restTemplate.getInterceptors()); | ||
list.add(loadBalancerInterceptor); | ||
restTemplate.setInterceptors(list); | ||
} | ||
}; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...main/java/org/springframework/cloud/client/loadbalancer/AsyncLoadBalancerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2013-2016 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 | ||
* | ||
* http://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.cloud.client.ServiceInstance; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.AsyncClientHttpRequestExecution; | ||
import org.springframework.http.client.AsyncClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.util.concurrent.ListenableFuture; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
/** | ||
* @author Rob Worsnop | ||
*/ | ||
public class AsyncLoadBalancerInterceptor implements AsyncClientHttpRequestInterceptor { | ||
|
||
private LoadBalancerClient loadBalancer; | ||
|
||
public AsyncLoadBalancerInterceptor(LoadBalancerClient loadBalancer) { | ||
this.loadBalancer = loadBalancer; | ||
} | ||
|
||
@Override | ||
public ListenableFuture<ClientHttpResponse> intercept(final HttpRequest request, final byte[] body, | ||
final AsyncClientHttpRequestExecution execution) throws IOException { | ||
final URI originalUri = request.getURI(); | ||
String serviceName = originalUri.getHost(); | ||
return this.loadBalancer.execute(serviceName, | ||
new LoadBalancerRequest<ListenableFuture<ClientHttpResponse>>() { | ||
@Override | ||
public ListenableFuture<ClientHttpResponse> apply(final ServiceInstance instance) | ||
throws Exception { | ||
HttpRequest serviceRequest = new ServiceRequestWrapper(request, | ||
instance, loadBalancer); | ||
return execution.executeAsync(serviceRequest, body); | ||
} | ||
|
||
}); | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
.../main/java/org/springframework/cloud/client/loadbalancer/AsyncRestTemplateCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2013-2016 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 | ||
* | ||
* http://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.web.client.AsyncRestTemplate; | ||
|
||
/** | ||
* @author Rob Worsnop | ||
*/ | ||
public interface AsyncRestTemplateCustomizer { | ||
void customize(AsyncRestTemplate restTemplate); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | |
org.springframework.cloud.client.CommonsClientAutoConfiguration,\ | ||
org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration,\ | ||
org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfiguration,\ | ||
org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration,\ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alphabetical order, please. |
||
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration,\ | ||
org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,\ | ||
org.springframework.cloud.commons.util.UtilAutoConfiguration | ||
|
159 changes: 159 additions & 0 deletions
159
...rg/springframework/cloud/client/loadbalancer/AsyncLoadBalancerAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package org.springframework.cloud.client.loadbalancer; | ||
|
||
import lombok.SneakyThrows; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.cloud.client.DefaultServiceInstance; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.http.client.AsyncClientHttpRequestInterceptor; | ||
import org.springframework.web.client.AsyncRestTemplate; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.empty; | ||
|
||
|
||
/** | ||
* @author Rob Worsnop | ||
*/ | ||
public class AsyncLoadBalancerAutoConfigurationTests { | ||
|
||
@Test | ||
public void restTemplateGetsLoadBalancerInterceptor() { | ||
ConfigurableApplicationContext context = init(OneRestTemplate.class); | ||
final Map<String, AsyncRestTemplate> restTemplates = context | ||
.getBeansOfType(AsyncRestTemplate.class); | ||
|
||
MatcherAssert.assertThat(restTemplates, is(notNullValue())); | ||
MatcherAssert.assertThat(restTemplates.values(), hasSize(1)); | ||
AsyncRestTemplate restTemplate = restTemplates.values().iterator().next(); | ||
MatcherAssert.assertThat(restTemplate, is(notNullValue())); | ||
|
||
assertLoadBalanced(restTemplate); | ||
} | ||
|
||
private void assertLoadBalanced(AsyncRestTemplate restTemplate) { | ||
List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); | ||
MatcherAssert.assertThat(interceptors, hasSize(1)); | ||
AsyncClientHttpRequestInterceptor interceptor = interceptors.get(0); | ||
MatcherAssert.assertThat(interceptor, is(instanceOf(AsyncLoadBalancerInterceptor.class))); | ||
} | ||
|
||
@Test | ||
public void multipleRestTemplates() { | ||
ConfigurableApplicationContext context = init(TwoRestTemplates.class); | ||
final Map<String, AsyncRestTemplate> restTemplates = context | ||
.getBeansOfType(AsyncRestTemplate.class); | ||
|
||
MatcherAssert.assertThat(restTemplates, is(notNullValue())); | ||
Collection<AsyncRestTemplate> templates = restTemplates.values(); | ||
MatcherAssert.assertThat(templates, hasSize(2)); | ||
|
||
TwoRestTemplates.Two two = context.getBean(TwoRestTemplates.Two.class); | ||
|
||
MatcherAssert.assertThat(two.loadBalanced, is(notNullValue())); | ||
assertLoadBalanced(two.loadBalanced); | ||
|
||
MatcherAssert.assertThat(two.nonLoadBalanced, is(notNullValue())); | ||
MatcherAssert.assertThat(two.nonLoadBalanced.getInterceptors(), is(empty())); | ||
} | ||
|
||
protected ConfigurableApplicationContext init(Class<?> config) { | ||
return new SpringApplicationBuilder().web(false) | ||
.properties("spring.aop.proxyTargetClass=true") | ||
.sources(config, AsyncLoadBalancerAutoConfiguration.class).run(); | ||
} | ||
|
||
@Configuration | ||
protected static class OneRestTemplate { | ||
|
||
@LoadBalanced | ||
@Bean | ||
AsyncRestTemplate loadBalancedRestTemplate() { | ||
return new AsyncRestTemplate(); | ||
} | ||
|
||
@Bean | ||
LoadBalancerClient loadBalancerClient() { | ||
return new NoopLoadBalancerClient(); | ||
} | ||
|
||
@Bean | ||
LoadBalancedRetryPolicyFactory loadBalancedRetryPolicyFactory() { return new LoadBalancedRetryPolicyFactory.NeverRetryFactory();} | ||
|
||
} | ||
|
||
@Configuration | ||
protected static class TwoRestTemplates { | ||
|
||
@Primary | ||
@Bean | ||
AsyncRestTemplate restTemplate() { | ||
return new AsyncRestTemplate(); | ||
} | ||
|
||
@LoadBalanced | ||
@Bean | ||
AsyncRestTemplate loadBalancedRestTemplate() { | ||
return new AsyncRestTemplate(); | ||
} | ||
|
||
@Bean | ||
LoadBalancerClient loadBalancerClient() { | ||
return new NoopLoadBalancerClient(); | ||
} | ||
|
||
@Configuration | ||
protected static class Two { | ||
@Autowired | ||
AsyncRestTemplate nonLoadBalanced; | ||
|
||
@Autowired | ||
@LoadBalanced | ||
AsyncRestTemplate loadBalanced; | ||
} | ||
|
||
} | ||
|
||
private static class NoopLoadBalancerClient implements LoadBalancerClient { | ||
private final Random random = new Random(); | ||
|
||
@Override | ||
public ServiceInstance choose(String serviceId) { | ||
return new DefaultServiceInstance(serviceId, serviceId, | ||
this.random.nextInt(40000), false); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { | ||
return request.apply(choose(serviceId)); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException { | ||
return request.apply(choose(serviceId)); | ||
} | ||
|
||
@Override | ||
public URI reconstructURI(ServiceInstance instance, URI original) { | ||
return DefaultServiceInstance.getUri(instance); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We recently added retry logic to the
RestTemplate
interceptor, should we do the same for the async interceptor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that, but
RetryTemplate
does not seem to lend itself to asynchronous operations. Or am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good point, clearly I didn't put to much thought into that comment when I made it :) Let me ask around and see if anyone has done something like this yet