Skip to content
Closed
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
8 changes: 8 additions & 0 deletions docs/src/main/asciidoc/spring-cloud-commons.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@ For the reactive implementation, you can additionally set:

For the reactive implementation, you can also implement your own `LoadBalancerRetryPolicy` to have more detailed control over the load-balanced call retries.

If you would like to configure retries for specific clients, you need to set `spring.cloud.loadbalancer.clients.retry.configuration.enabled=true`.
Then you can set properties:

- `spring.cloud.loadbalancer.clients.*clientName*.retry.maxRetriesOnSameServiceInstance`
- `spring.cloud.loadbalancer.clients.*clientName*.retry.maxRetriesOnNextServiceInstance`
- `spring.cloud.loadbalancer.clients.*clientName*.retry.retryOnAllOperations`
- `spring.cloud.loadbalancer.clients.*clientName*.retry.retryableStatusCodes`

NOTE: For load-balanced retries, by default, we wrap the `ServiceInstanceListSupplier` bean with `RetryAwareServiceInstanceListSupplier` to select a different instance from the one previously chosen, if available. You can disable this behavior by setting the value of `spring.cloud.loadbalancer.retry.avoidPreviousInstance` to `false`.

====
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2012-2021 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 java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* A {@link ConfigurationProperties} bean for client specific Spring Cloud LoadBalancer.
*
* @author Andrii Bohutskyi
*/
@ConfigurationProperties("spring.cloud.loadbalancer")
public class ClientLoadBalancerProperties {

private Map<String, LoadBalancerProperties> clients = new HashMap<>();

public Map<String, LoadBalancerProperties> getClients() {
return clients;
}

public void setClients(Map<String, LoadBalancerProperties> clients) {
this.clients = clients;
}

public Optional<LoadBalancerProperties> getClientLoadBalancerProperties(String service) {
return Optional.ofNullable(clients.get(service));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
"name": "spring.cloud.loadbalancer.retry.enabled",
"description": "Enables LoadBalancer retries.",
"type": "java.lang.Boolean"
},
{
"defaultValue": false,
"name": "spring.cloud.loadbalancer.clients.retry.configuration.enabled",
"description": "Enables LoadBalancer retries configuration per client.",
"type": "java.lang.Boolean"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2021 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.loadbalancer.blocking.retry;

import org.springframework.cloud.client.loadbalancer.ClientLoadBalancerProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;

/**
* An implementation of {@link LoadBalancedRetryFactory} for client specific
* {@link BlockingLoadBalancerClient}.
*
* @author Andrii Bohutskyi
*/
public class ClientBlockingLoadBalancedRetryFactory implements LoadBalancedRetryFactory {

private final LoadBalancerProperties loadBalancerProperties;

private final ClientLoadBalancerProperties clientLoadBalancerProperties;

public ClientBlockingLoadBalancedRetryFactory(LoadBalancerProperties loadBalancerProperties,
ClientLoadBalancerProperties clientLoadBalancerProperties) {
this.loadBalancerProperties = loadBalancerProperties;
this.clientLoadBalancerProperties = clientLoadBalancerProperties;
}

@Override
public LoadBalancedRetryPolicy createRetryPolicy(String service, ServiceInstanceChooser serviceInstanceChooser) {
final LoadBalancerProperties properties = clientLoadBalancerProperties.getClientLoadBalancerProperties(service)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Ferioney , as discussed here, we will wait with resolving properties per client in general till the feature is implemented in Boot, so the changes in this class should be removed. Specifcally, the issue here is that either all properties are resolved "per client" for a given client or none, while in the final version, we would want it to be possible to make that distinction on property level. For now, a simpler implementation only for the specific properties can be added to fix this issue, similarly to what is being done here and here.

.orElse(loadBalancerProperties);

return new BlockingLoadBalancedRetryPolicy(properties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.ClientLoadBalancerProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.loadbalancer.blocking.retry.BlockingLoadBalancedRetryFactory;
import org.springframework.cloud.loadbalancer.blocking.retry.ClientBlockingLoadBalancedRetryFactory;
import org.springframework.cloud.loadbalancer.core.LoadBalancerServiceInstanceCookieTransformer;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -70,15 +72,25 @@ public LoadBalancerServiceInstanceCookieTransformer loadBalancerServiceInstanceC

@Configuration
@ConditionalOnClass(RetryTemplate.class)
@EnableConfigurationProperties(LoadBalancerProperties.class)
@EnableConfigurationProperties({ LoadBalancerProperties.class, ClientLoadBalancerProperties.class })
protected static class BlockingLoadBalancerRetryConfig {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.clients.retry.configuration.enabled",
havingValue = "false", matchIfMissing = true)
LoadBalancedRetryFactory loadBalancedRetryFactory(LoadBalancerProperties properties) {
return new BlockingLoadBalancedRetryFactory(properties);
}

@Bean
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.clients.retry.configuration.enabled",
havingValue = "true")
LoadBalancedRetryFactory clientLoadBalancedRetryFactory(LoadBalancerProperties properties,
ClientLoadBalancerProperties clientProperties) {
return new ClientBlockingLoadBalancedRetryFactory(properties, clientProperties);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.loadbalancer.blocking.retry.BlockingLoadBalancedRetryFactory;
import org.springframework.cloud.loadbalancer.blocking.retry.ClientBlockingLoadBalancedRetryFactory;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -31,6 +33,7 @@
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
* @author Tim Ysewyn
* @author Andrii Bohutskyi
*/
class BlockingLoadBalancerClientAutoConfigurationTests {

Expand All @@ -43,9 +46,19 @@ void beansCreatedNormally() {
applicationContextRunner.run(ctxt -> {
assertThat(ctxt).hasSingleBean(BlockingLoadBalancerClient.class);
assertThat(ctxt).hasSingleBean(LoadBalancedRetryFactory.class);
assertThat(ctxt.getBean(LoadBalancedRetryFactory.class)).isInstanceOf(BlockingLoadBalancedRetryFactory.class);
});
}

@Test
void enableClientRetryConfigurationShouldLoadContext() {
applicationContextRunner.withPropertyValues("spring.cloud.loadbalancer.clients.retry.configuration.enabled=true")
.run(context -> {
assertThat(context).hasSingleBean(LoadBalancedRetryFactory.class);
assertThat(context.getBean(LoadBalancedRetryFactory.class)).isInstanceOf(ClientBlockingLoadBalancedRetryFactory.class);
});
}

@Test
public void worksWithoutSpringWeb() {
applicationContextRunner.withClassLoader(new FilteredClassLoader(RestTemplate.class)).run(context -> {
Expand Down