Master Spring Boot 3: 151 Real‑World Cases on Retry, Concurrency, and HttpClient
This article introduces a continuously updated collection of 151 Spring Boot 3 practical examples, covering built‑in retry mechanisms, concurrency limits, and automatic HTTP client registration, complete with configuration snippets, code samples, and visual illustrations to help developers implement resilient backend services.
Spring Boot 3 practical case collection has been updated to 151 examples, with a promise of permanent free updates for subscribers.
1. Introduction
In Spring Boot development, retry mechanisms handle network fluctuations and service unavailability, while concurrency control prevents performance degradation caused by excessive requests. These features are typically implemented using third‑party libraries such as @Retryable from spring-retry or Guava RateLimiter, and can also be custom‑built with AOP.
Since Spring Framework 7.x, the framework natively integrates retry and concurrency control. Retry supports flexible back‑off strategies and exception matching, while concurrency control currently offers only single‑node rate limiting.
2. Practical Cases
Spring Framework 7 (still in SNAPSHOT) requires the following Maven configuration:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>Enable the features with:
@SpringBootApplication
@EnableResilientMethods
public class Application {}2.1 Retry Mechanism (@Retryable)
The @Retryable annotation can be placed on methods or classes to automatically retry failed executions.
@Retryable
public void sendMail() {
System.err.printf("%d - Sending mail...", DateTimeFormatter.ofPattern("mm:ss").format(LocalDateTime.now()));
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {}
System.err.println(1 / 0);
}By default, the method retries up to three times with a 1‑second delay between attempts. Customization examples:
@Retryable(EmailException.class)
public void sendMail() { /* ... */ }
@Retryable(maxAttempts = 5, delay = 100, jitter = 10, multiplier = 2, maxDelay = 1000)
public void sendMail() { /* ... */ }Reactive methods can also use @Retryable:
@Retryable(maxAttempts = 5, delay = 100, jitter = 10, multiplier = 2, maxDelay = 1000)
public Mono<Void> sendMail() {
// ...
return Mono.from(...);
}Result of the retry example is shown below:
2.2 Concurrency Control (@ConcurrencyLimit)
The @ConcurrencyLimit annotation limits the number of concurrent executions of a method.
@ConcurrencyLimit(1)
public void callPhone() {
System.err.printf("%s - %s - Calling...%n", Thread.currentThread().getName(),
DateTimeFormatter.ofPattern("mm:ss:SSS").format(LocalDateTime.now()));
try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) {}
}When one request is processing, others wait, preventing resource overload. This is especially useful for virtual threads.
2.3 Automatic HttpClient Registration
Declare an interface to define HTTP endpoints, similar to Feign:
public interface UserClient {
@GetExchange("/users")
List<User> getUsers();
@GetExchange("/users/{id}")
User getUserById(@PathVariable Long id);
}Configure the client:
@Configuration
public class HttpClientConfiguration {
@Bean
RestClient.Builder restClient() {
return RestClient.builder();
}
@Bean
UserClient userHttpClient(RestClient.Builder builder) {
RestClient restClient = builder.baseUrl("http://localhost:8081").build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(RestClientAdapter.create(restClient)).build();
return factory.createClient(UserClient.class);
}
}From Spring Framework 7 (Spring Boot 4) onward, the @ImportHttpServices annotation can automatically scan and register such interfaces:
@Configuration
@ImportHttpServices(group = "users", basePackages = "com.pack.client")
public class HttpClientConfiguration {
@Bean
RestClient.Builder restClient() {
return RestClient.builder();
}
}After registration, the client can be injected and used in controllers.
Conclusion
The article provides a complete set of examples for implementing retry, concurrency limits, and declarative HTTP clients in Spring Boot 3, helping developers build more resilient backend services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
