跳到主要内容

19 篇博文 含有标签「reactive」

查看所有标签

Deep Dive Schedulers.boundedElastic()

· 阅读需 12 分钟

Start with a simple piece of code snippet​

kafkaReceiver.receive()
.concatMap(message -> process(message) // Step 1
.subscribeOn(Schedulers.boundedElastic())) // Step 2
.doOnNext(msg -> msg.receiverOffset().acknowledge()) // Step 3
.subscribe(); // Step 4

This is the gold-standard pattern in 2025 for consuming Kafka with Spring WebFlux + Project Reactor + Reactor Kafka when process(message) contains blocking code (JDBC, Redis sync driver, REST call with RestTemplate, file I/O, etc.).

Step-by-Step Execution Flow​

StepWhat HappensWhich Thread Pool?Why It Matters
1kafkaReceiver.receive() returns a Flux<ReceiverRecord>Runs on Netty event-loop threads (non-blocking I/O)This thread must never block or the whole reactor Netty server will starve
2concatMap waits for the **previous process() to finish before taking the next message → preserves Kafka order per partitionStill on Netty thread until…
subscribeOn(Schedulers.boundedElastic()) moves the entire upstream work (i.e. process(message)) to a boundedElastic thread→ jumps to boundedElastic thread poolThis is the only safe place to do blocking work
3After process() completes successfully, we come back to the original Netty thread to call acknowledge()Back on Netty event-loopCommit must happen on the same thread that received the record (Reactor Kafka requirement)
4.subscribe() starts the whole chain

What Is Schedulers.boundedElastic() Exactly?​

PropertyValue (Reactor 3.6+)What It Means in Practice
Thread pool typeThread-per-task + bounded queueCreates new threads on demand
Max threadsMath.max(10, CPU × 10) → usually 100–200Will never create thousands of threads
QueueBounded (default 100,000 tasks)If queue full → onError with QueueFullException
Thread name prefixboundedElastic-xEasy to spot in thread dumps
Designed forBlocking I/O (JDBC, blocking Redis, file, external sync API)This is the only scheduler you are allowed to block on

Comparison with Other Schedulers​

SchedulerUse CaseCan I block here?
Schedulers.parallel()CPU-intensive (encryption, image processing)No – will starve CPU workers
Schedulers.boundedElastic()Blocking I/O ← this is the oneYes – made for this
Schedulers.single() / immediate()Rare, very specific casesNever block

Real-Life Example – What Happens with 10,000 Messages/sec​

// BAD – blocks Netty thread → entire WebFlux app dies in seconds
.concatMap(msg -> blockingJdbc.save(msg))

// GOOD – this code you posted
.concatMap(message -> process(message) // contains JdbcTemplate, RestTemplate, etc.
.subscribeOn(Schedulers.boundedElastic()))

Without subscribeOn(boundedElastic()) → Netty event-loop threads block → TCP backlog → all WebSocket clients disconnect → outage.

With it → 150 boundedElastic threads happily do JDBC work, Netty stays responsive, throughput 10k+/sec easily.

Best Practices (2025)​

// Most common production pattern
kafkaReceiver.receive()
.concatMap(record ->
process(record.value()) // may block
.subscribeOn(Schedulers.boundedElastic()) // ← mandatory
.doOnError(ex -> log.error("...", ex))
.onErrorReturn fallbackValue) // don't stop the consumer
.doOnNext(ReceiverRecord::receiverOffset.acknowledge) // commit
.retryWhen(Retry.backoff(10, Duration.ofSeconds(1))) // survive Kafka blips
.subscribe();

Summary​

Schedulers.boundedElastic() is the only safe place to run blocking code in a WebFlux/Reactive application.
Without it, you will kill your Netty event-loop and take down the entire service.
With it, you get safe, scalable, ordered Kafka consumption even when process() talks to JDBC, Redis sync, or any legacy blocking library.

That one line .subscribeOn(Schedulers.boundedElastic()) is literally the difference between a stable 50k RPS service and a total outage.

What exactly happens under the hood when you write this line:​

process(record.value())
.subscribeOn(Schedulers.boundedElastic())

Step-by-Step Execution (Thread View)​

TimeWhat happensWhich thread does the work?Thread pool name
1Kafka record arrives on Netty event-loop threadreactor-http-nio-3 (or similar)Netty event-loop
2concatMap sees the subscribeOn(boundedElastic()) operatorStill on Netty thread
3Reactor immediately hands the entire process(...) chain to the boundedElastic scheduler→ jumps to a thread from the boundedElastic thread poolboundedElastic-1, boundedElastic-2, …
4Your process(record.value()) code runs completely on that boundedElastic threadYes – this thread can safely block (JDBC, RestTemplate, File I/O, etc.)
5When process() finishes (success or error), Reactor schedules the downstream operators (.doOnNext(acknowledge)) back on the original Netty thread (because of how Reactor Kafka is designed)Back to reactor-http-nio-3

Visual Thread Timeline​

Netty thread (reactor-http-nio-3) boundedElastic thread pool
────────────────────────────── ───────────────────────────────
receive() → concatMap → subscribeOn() ──→ boundedElastic-7 runs process()
← process() completes
→ acknowledge() runs back on Netty thread

boundedElastic Thread Pool Details (2025 defaults)​

PropertyValue (Reactor 3.6+)Meaning
Core threads10 (minimum)Always alive
Maximum threadsCPU cores × 10 → usually 100–200Grows only when queue is full
Queue size100,000 tasks (configurable)If full → rejects with exception
Thread keep-alive60 seconds (idle threads die)Saves memory
Thread name prefixboundedElastic-1, boundedElastic-2, …Easy to spot in jstack / Grafana

Real Example from Production Thread Dump​

"boundedElastic-27" #124 daemon prio=5 os_prio=0 tid=0x... nid=0x4b2c runnable
java.lang.Thread.State: RUNNABLE
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(...)

"boundedElastic-28" #125 daemon prio=5 os_prio=0 tid=0x... nid=0x4b2d waiting on condition
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(Native Method)
- parking to wait for <0x...> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)

You can see 27 threads doing MySQL queries, 28 waiting for more work – exactly what we want.

Summary Again​

subscribeOn(Schedulers.boundedElastic()) =
“Take this whole piece of work (process(...)) and run it on a dedicated thread pool that is designed and allowed to block.
The Netty event-loop stays completely free and responsive.”

That single operator is the #1 reason reactive Spring Boot + Kafka applications survive real production traffic without melting down.

What Exactly Happens Inside Schedulers.boundedElastic() (Reactor 3.5+ / Spring Boot 3.x – 2025)​

QuestionExact Answer (Source Code Level)
Which ThreadPoolExecutor is used under the hood?BoundedElasticScheduler → wraps a custom java.util.concurrent.ThreadPoolExecutor with a very special configuration
Executor typeUnbounded core + bounded maximum + bounded queue (the only one in the JDK that behaves like this)
JDK classreactor.core.scheduler.BoundedElasticScheduler.BoundedScheduledExecutorService → extends ThreadPoolExecutor
Queue usedLinkedBlockingQueue<Runnable> with configurable capacity (default 100,000)
Thread factoryreactor.core.scheduler.SchedulerThreadFactory → names threads boundedElastic-1, boundedElastic-2, …

Exact Thread Pool Configuration (2025 defaults)​

ParameterValue (Reactor 3.6+)Formula / Meaning
corePoolSize0 (zero!)No threads kept alive when idle
maximumPoolSizeCPU × 10 (e.g. 8-core → 80, 16-core → 160, capped at ~200)Math.min(Runtime.getRuntime().availableProcessors() * 10, 100_000)
keepAliveTime60 secondsIdle threads above core (i.e. all) die after 60s
Queue capacity100,000 tasks by defaultConfigurable via -Dreactor.schedulers.boundedElastic.queueCapacity=500000
RejectedExecutionHandlerAbortPolicy → throws RejectedExecutionException when queue fullYou’ll see QueueFullException in logs if overloaded

Execution Strategy – Visualized​

corePoolSize = 0 → No threads at startup
│
└─ Task arrives → create new thread (up to maxPoolSize)
│
├─ Queue has space → enqueue task
│ (even if threads < maxPoolSize)
│
└─ Queue full + threads == maxPoolSize → REJECT
Task arrives
│
├─ threads < maxPoolSize → create new thread immediately
│
├─ threads == maxPoolSize → enqueue in 100k queue
│
└─ queue full + max threads → RejectedExecutionException → your Flux errors

This is NOT the classic “fixed + bounded queue” model.
It’s a hybrid designed specifically for blocking I/O:

  • Grow very aggressively (up to CPU×10) to handle bursty blocking calls
  • Shrink back to zero when idle → saves memory in microservices
  • Huge queue (100k) prevents rejection in normal spikes

How to Change the Limits (Production Tuning)​

# application.yml (Spring Boot)
spring:
threads:
virtual:
enabled: true # Java 21+ → switches to virtual threads automatically!

Or JVM flags:

# Increase queue to 500k
-Dreactor.schedulers.boundedElastic.queueCapacity=500000

# Force max 200 threads even on 32-core machines
-Dreactor.schedulers.boundedElastic.maxThreads=200

Java 21+ Bonus: Virtual Threads (2024–2025 reality)​

Since Java 21 + Reactor 3.6 + Spring Boot 3.2:

Schedulers.boundedElastic() // automatically uses virtual threads

→ Each task gets its own virtual thread instead of platform thread
→ maximumPoolSize becomes practically unlimited (hundreds of thousands)
→ Memory per thread drops from ~1–2 MB → ~100 KB
→ You can safely process 100k+ blocking JDBC calls concurrently

Summary One More Time​

Schedulers.boundedElastic()
= ThreadPoolExecutor(
corePoolSize = 0,
maximumPoolSize = CPU × 10,
keepAlive = 60s,
queue = LinkedBlockingQueue(100_000),
policy = AbortPolicy
)
+ (since Java 21) automatically uses virtual threads → effectively unlimited

That’s why it’s the only safe place to call JDBC, RestTemplate, blocking Redis, etc. in a WebFlux/Reactive application — and why it scales beautifully in real production systems handling 10k–100k RPS.

Real Production Code Patterns​

// 1. Classic blocking Kafka consumer (most common)
kafkaReceiver.receive()
.concatMap(record ->
processBlocking(record.value()) // JDBC, RestTemplate, etc.
.subscribeOn(Schedulers.boundedElastic()) // ← THIS SAVES YOUR APP
.doOnError(ex -> log.error("Failed {}", record, ex))
.onErrorResume(ex -> Mono.empty()) // don't kill the stream
)
.doOnNext(ReceiverRecord::receiverOffset.acknowledge)
.subscribe();

// 2. WebFlux controller calling legacy blocking service
@GetMapping("/legacy-report")
public Mono<Report> getReport() {
return Mono.fromCallable(() -> legacyBlockingService.generatePdf())
.subscribeOn(Schedulers.boundedElastic()) // safe!
.timeout(Duration.ofSeconds(30));
}

// 3. Parallel batch processing (e.g. 1000 files)
Flux)
filesFlux
.flatMap(file ->
processFile(file) // heavy CPU + blocking I/O
.subscribeOn(Schedulers.boundedElastic()),
50 // max 50 concurrent
)
.collectList()
.block();

5. Production Tuning Examples​

# Recommended JVM flags for high-throughput services (2025)
-Dreactor.schedulers.boundedElastic.maxThreads=500
-Dreactor.schedulers.boundedElastic.queueCapacity=200000
-Dreactor.schedulers.defaultBoundedElasticOnVirtualThreads=true # force virtual threads even on JDK 21+

# Or in application.yml (Spring Boot 3.2+)
spring:
threads:
virtual:
enabled: true # ← turns boundedElastic into virtual-thread heaven

6. What Happens If You Forget It? (Real Outage Stories)​

MistakeSymptomTime to Total Outage
Call JdbcTemplate directly in WebFluxNetty threads block → TCP backlog → all WebSockets disconnect10–60 seconds
Use Schedulers.parallel() for JDBCCPU workers starve → GC thrashing → OOM2–10 minutes
Use subscribeOn(boundedElastic())System stays alive at 50k+ RPSNever

Must Remember!!!​

Schedulers.boundedElastic() is the only thread pool specifically created so you can safely call blocking code (JDBC, RestTemplate, Redis sync, file I/O, etc.) inside a fully reactive Spring WebFlux application without blowing up your Netty event loop.

Can Schedulers.boundedElastic() be pre-configured?​

Yes but only via system properties (JVM flags) or Spring Boot properties — not via code like new BoundedElasticScheduler(...) in normal applications.

Here is the complete list of everything you can pre-configure in 2025 (Reactor 3.6+ / Spring Boot 3.2+):

What you want to changeHow to do it (JVM flag)Spring Boot 3.2+ equivalent (application.yml)Default value
Maximum threads (JDK ≤20)-Dreactor.schedulers.boundedElastic.maxThreads=500spring.threads.virtual.enabled=false + JVM flagCPU × 10
Queue capacity-Dreactor.schedulers.boundedElastic.queueCapacity=500000—100,000
Thread name prefix-Dreactor.schedulers.boundedElastic.threadPrefix=myapp-be—boundedElastic
TTL of cached schedulers-Dreactor.schedulers.boundedElastic.cachedSchedulerExpireAfter=300s—60s
Force virtual threads (JDK 21+)-Dreactor.schedulers.boundedElasticOnVirtualThreads=truespring.threads.virtual.enabled=true ← recommendedauto-detect
Pre-warm / init threadsNot directly possible—starts at 0

Can I Pre-create Threads?​

No
boundedElastic is intentionally designed with corePoolSize = 0.
There is no official way to pre-warm threads (create 10 threads at startup).

But here are three real-world workarounds senior teams actually use**:

Workaround 1 – Force virtual threads (JDK 21+) → problem disappears​

# application.yml – this is the 2025 gold standard
spring:
threads:
virtual:
enabled: true # ← makes boundedElastic use virtual threads

→ Each task gets its own virtual thread instantly
→ No thread creation latency
→ No need to pre-warm anything
→ This is what Netflix, Uber, Alibaba, and most new Spring Boot 3 apps do.

Workaround 2 – Custom pre-warmed scheduler (rare, but possible)​

@Configuration
public class ReactorConfig {

public static final Scheduler PREWARMED_BLOCKING = Schedulers.newBoundedElastic(
20, // core size (pre-created)
200, // max size
"myapp-blocking",
60 // keep-alive seconds
);

@PreDestroy
public void cleanup() {
PREWARMED_BLOCKING.dispose();
}
}

Then use it instead:

.subscribeOn(ReactorConfig.PREWARMED_BLOCKING)

Workaround 3 – Eager initialization at startup (hacky but works)​

@PostConstruct
public void warmupBoundedElastic() {
IntStream.range(0, 20).parallel().forEach(i ->
Schedulers.boundedElastic().schedule(() -> {
try { Thread.sleep(10); } catch (Exception ignored) {}
})
);
}

→ Forces the default boundedElastic to create 20 threads immediately.

2025 Production Recommendation​

# application.yml – copy-paste this and forget about thread pools forever
spring:
threads:
virtual:
enabled: true # ← turns boundedElastic into infinite virtual threads
task:
scheduling:
pool:
size: 50 # for @Scheduled tasks

With Java 21 + Spring Boot 3.2 + this config:

  • Schedulers.boundedElastic() becomes effectively infinite
  • Thread creation latency ≈ 0
  • Memory per blocking call ≈ 100 KB instead of 2 MB
  • You never have to pre-warm or tune anything again
GoalSolution in 2025
Pre-warm threadsNot possible with default scheduler
Make it fast + infinitespring.threads.virtual.enabled=true (JDK 21+) ← use this
Full controlCreate your own with Schedulers.newBoundedElastic(...)

99 % of teams in 2025 just enable virtual threads and never think about boundedElastic configuration again.

Mono.fromCallable(...) – The Ultimate 2025 Explanation​

(What every senior Java/Reactive engineer must know inside-out)

QuestionAnswer (Real Production Truth)
What is it?A factory method that creates a Mono<T> from a blocking, callable piece of code (Callable<T> or lambda that returns T).
When do you use it?Every time you have to call legacy blocking code (JDBC, RestTemplate, Redis sync, file I/O, external API, etc.) inside a reactive chain.
Is it blocking?Yes — the code inside runs synchronously and blocks the calling thread.
Is it safe to call directly in WebFlux?Only safe if you offload it (via .subscribeOn(...)) — otherwise you block Netty event-loop → outage.

Exact Contract & Behavior​

public static <T> Mono<T> fromCallable(Callable<? extends T> supplier)
ScenarioWhat actually happens
supplier returns a valueMono emits that value → onNext → onComplete
supplier throws exceptionMono emits onError with that exception
supplier returns nullMono emits onNext(null) → onComplete (yes, null is allowed)
You call it on Netty threadBlocks the carrier thread → bad unless you offload

The Two Correct Ways to Use It in 2025​

1. Classic way (Java 17–20) – must offload​

Mono.fromCallable(() -> jdbcTemplate.queryForObject(sql, Long.class))
.subscribeOn(Schedulers.boundedElastic()) // ← mandatory pre-Java 21
.timeout(Duration.ofSeconds(5));

2. 2025 way (Java 21 + virtual threads) – offload optional​

# application.yml
spring:
threads:
virtual:
enabled: true # ← turns boundedElastic into virtual threads
// Now perfectly safe without subscribeOn!
Mono.fromCallable(() -> jdbcTemplate.queryForObject(sql, Long.class))
.timeout(Duration.ofSeconds(5));

Real-World Examples (Copy-Paste Ready)​

// 1. Legacy JDBC call
Mono.fromCallable(() -> userRepository.findById(id)) // blocking
.subscribeOn(Schedulers.boundedElastic())

// 2. External blocking HTTP client
Mono.fromCallable(() -> restTemplate.postForEntity(url, request, String.class))
.map(HttpEntity::getBody)

// 3. File I/O
Mono.fromCallable(() -> Files.readString(Path.of("/tmp/config.json")))
.map(Json::parse)

// 4. CPU-heavy calculation (WRONG scheduler!)
Mono.fromCallable(() -> complexEncryption(data)) // CPU bound → use parallel!
.subscribeOn(Schedulers.parallel()) // ← correct

// 5. With error handling + fallback
Mono.fromCallable(() -> legacyService.getUser(id))
.onErrorResume(ex -> {
log.warn("Legacy failed, using cache", ex);
return Mono.just(cachedUser);
})

Common Mistakes Seniors Still Make​

MistakeConsequenceFix
Forgetting .subscribeOn(boundedElastic())Blocks Netty → all WebSockets disconnectAdd it (or enable virtual threads)
Using it for CPU workStarves parallel schedulerUse Mono.fromSupplier + Schedulers.parallel()
Chaining many fromCallable without limitThread explosion (one thread per call)Use .publishOn(...) or limit concurrency

Mono.fromCallable vs Alternatives (2025 Decision Table)​

Use CaseBest ChoiceWhy
Simple blocking call (DB, HTTP, file)Mono.fromCallable(...).subscribeOn(boundedElastic())Cleanest
You already have a Supplier<T>Mono.fromSupplier(...)Same as fromCallable but no checked exception
You want lazy execution only on subscribeMono.defer(() -> Mono.fromCallable(...))Prevents execution on cold publishers
Async but blocking driverPrefer true async driver (R2DBC, reactive Redis)Zero blocking

The One Rule Every Senior Lives By​

Mono.fromCallable { blocking code }
→ Always pair with .subscribeOn(Schedulers.boundedElastic())
→ Or run on Java 21+ with spring.threads.virtual.enabled=true and delete the subscribeOn forever.

Reactive Programming in Spring WebFlux Series - I

· 阅读需 4 分钟

Overview and Fundamentals

What is Reactive Programming?​

Reactive programming is a programming paradigm oriented around data flows and the propagation of changes. Unlike traditional imperative programming, reactive programming allows you to declare data dependencies, and changes are automatically propagated when the data source changes. The core idea of reactive programming involves asynchronous data stream processing and non-blocking operations, making applications more efficient in utilizing system resources.


Why Choose Spring WebFlux?​

Spring WebFlux is an asynchronous, non-blocking web framework introduced in Spring 5 that handles non-blocking asynchronous requests. Unlike traditional Spring MVC, Spring WebFlux is built on the Reactive Streams specification and supports a fully non-blocking reactive programming model. Some key reasons to choose Spring WebFlux include:

  • Non-blocking I/O: Allows handling of more concurrent requests, improving system throughput and response times.
  • Reactive Programming Model: Offers reactive programming support based on Project Reactor, making the code more concise and maintainable.
  • Flexible Programming Model: Supports both annotation-driven and functional programming models, catering to different developer needs.
  • High Scalability: Ideal for microservices and distributed systems, making it easy to scale and integrate.

Core Concepts and Components​

1. Reactive Streams​

Reactive Streams is the foundation of reactive programming, defining the standards for asynchronous stream processing. It consists of four core interfaces:

  • Publisher: Responsible for producing data.
  • Subscriber: Consumes data.
  • Subscription: Manages the relationship between publishers and subscribers.
  • Processor: Acts as both a publisher and a subscriber.

2. Project Reactor​

Project Reactor is the underlying implementation for Spring WebFlux, providing support for reactive streams. It has two main types:

  • Mono: Represents an asynchronous sequence of 0 or 1 element.
  • Flux: Represents an asynchronous sequence of 0 to N elements.

3. WebFlux​

Spring WebFlux is an asynchronous, non-blocking web framework based on Reactive Streams API and Project Reactor. The main components include:

  • Router Functions: Define mappings between requests and handlers in a functional style.
  • Handler Functions: Handle HTTP requests and return responses.

Practical Example: Building a Simple WebFlux Application​

1. Project Setup​

Start by creating a Spring Boot project and adding the necessary dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

2. Define Data Model​

Define a simple user data model:

public class User {
private String id;
private String name;

// Constructors, Getters and Setters
}

3. Create Handler and Router​

Create a handler function and a router function:

@Component
public class UserHandler {
public Mono<ServerResponse> getUser(ServerRequest request) {
User user = new User("1", "John Doe");
return ServerResponse.ok().body(Mono.just(user), User.class);
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route(RequestPredicates.GET("/user"), handler::getUser);
}
}

Conclusion​

In this article, we introduced the basic concepts and core components of Spring WebFlux and demonstrated how to build a simple reactive application using WebFlux.

Reactive Programming in Spring WebFlux Series - II

· 阅读需 4 分钟

Index​


In-depth Understanding of Reactor and Asynchronous Data Streams


Introduction to Reactor​

Reactor is at the core of Spring WebFlux, providing powerful reactive programming support based on the Reactive Streams standard. Reactor mainly offers two core classes: Mono and Flux, which are used to represent asynchronous sequences of one or multiple elements, respectively.


Mono and Flux​

Mono​

Mono represents an asynchronous sequence of 0 or 1 element. It is typically used when dealing with operations that might return a single result or no result at all.

Flux​

Flux represents an asynchronous sequence of 0 to N elements. It is suitable for cases where multiple results need to be handled, such as querying a database that returns multiple records.


Basic Operations​

Reactor provides a rich set of operators for manipulating data streams, such as transformations, filtering, and aggregations. Some of the commonly used operators include:

  • map: Transforms each element in the data stream.
  • filter: Filters elements in the data stream, retaining only those that meet the criteria.
  • flatMap: Maps each element to a new Publisher and merges these Publishers into a single Flux.
  • concatMap: Similar to flatMap but maintains the order of the elements.
  • zip: Combines multiple Publishers by pairing elements based on their indices.

Practical Example: Building a Reactive Application with Reactor​

1. Project Setup​

Ensure your project contains the following dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

2. Define Data Model​

Define a simple user data model:

public class User {
private String id;
private String name;

// Constructors, Getters and Setters
}

3. Simulated Data Source​

Create a simulated data source to generate user data:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

public class UserRepository {
private static final List<User> users = Arrays.asList(
new User("1", "John Doe"),
new User("2", "Jane Doe"),
new User("3", "Sam Smith")
);

public Mono<User> findById(String id) {
return Mono.justOrEmpty(users.stream()
.filter(user -> user.getId().equals(id))
.findFirst());
}

public Flux<User> findAll() {
return Flux.fromIterable(users);
}
}

4. Handlers and Router Configuration​

Create handler functions and router configurations to handle HTTP requests and return responses:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Mono;

@Component
public class UserHandler {
private final UserRepository userRepository;

public UserHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}

public Mono<ServerResponse> getUser(ServerRequest request) {
String userId = request.pathVariable("id");
return userRepository.findById(userId)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> getAllUsers(ServerRequest request) {
return ServerResponse.ok().body(userRepository.findAll(), User.class);
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route()
.GET("/user/{id}", handler::getUser)
.GET("/users", handler::getAllUsers)
.build();
}
}

Conclusion​

In this article, we took a deep dive into Reactor and its application in Spring WebFlux. By understanding Mono and Flux and how to handle asynchronous data streams with these tools, we can build more efficient reactive applications.

Reactive Programming in Spring WebFlux Series - III

· 阅读需 4 分钟

Error Handling and Debugging


Error Handling​

Error handling in reactive programming is different from traditional synchronous programming due to the asynchronous nature of reactive streams. In Reactor, errors are handled in various ways:

1. onErrorReturn​

This operator allows returning a default value when an error occurs. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.onErrorReturn("Default");

flux.subscribe(System.out::println);

In this example, when an error occurs, the default value "Default" is returned.

2. onErrorResume​

This operator allows switching to another Publisher when an error occurs. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.onErrorResume(e -> {
System.out.println("Error: " + e.getMessage());
return Flux.just("D", "E");
});

flux.subscribe(System.out::println);

Here, when an error occurs, it switches to another Flux sequence to continue processing new data.

3. onErrorMap​

This operator allows converting an error into a different type of error. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.onErrorMap(e -> new CustomException("Custom message", e));

flux.subscribe(System.out::println, e -> System.out.println("Error: " + e.getMessage()));

In this case, a RuntimeException is converted into a custom CustomException.


Global Error Handling in WebFlux​

In addition to handling errors in streams, Spring WebFlux also allows defining a global error handler for managing errors across the application.

1. Defining a Custom Exception Class​

public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}

2. Creating an Exception Handler​

import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.ServerResponse.BodyBuilder;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

@Component
@Order(-2)
public class GlobalErrorHandler implements WebExceptionHandler {

@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
BodyBuilder response = ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON);

if (ex instanceof CustomException) {
response = ServerResponse.status(HttpStatus.BAD_REQUEST);
}

return response.bodyValue(new ErrorResponse(ex.getMessage()))
.flatMap(resp -> resp.writeTo(exchange, new Context()));
}

private static class ErrorResponse {
private final String message;

public ErrorResponse(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
}

Debugging Reactive Applications​

Debugging reactive applications can be more challenging than debugging traditional synchronous applications. Here are some debugging techniques:

1. Using the log Operator​

Reactor provides the log operator to output log information for each step. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.log();

flux.subscribe(System.out::println);

2. Breakpoint Debugging​

Setting breakpoints in an IDE allows you to step through the execution. Be mindful of thread switching and asynchronous execution details when debugging reactive code.

3. Using Reactor Debugger​

Reactor provides a special debug tool Hooks.onOperatorDebug to help understand and debug operations in reactive streams.

Hooks.onOperatorDebug();

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")));

flux.subscribe(System.out::println);

Conclusion​

In this article, we explored how to handle errors and debug reactive applications in Spring WebFlux, providing techniques for both stream-level and global error handling.

Reactive Programming in Spring WebFlux Series - IV

· 阅读需 4 分钟

Data Stream Transformation and Operations


Data Stream Transformation​

In reactive programming, transforming and operating on data streams is a core aspect. Using different operators, we can handle data streams flexibly, applying transformations, filtering, aggregating, and more. Below are some common data stream operations and examples.

1. map Operator​

The map operator transforms each element in a data stream. It takes a function as an argument and applies it to each element.

Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5)
.map(n -> n * n);

numbers.subscribe(System.out::println); // Output: 1, 4, 9, 16, 25

2. flatMap Operator​

The flatMap operator maps each element to a new Publisher and merges them into a single Flux. It is often used for asynchronous processing.

Flux<String> ids = Flux.just("1", "2", "3");
Flux<User> users = ids.flatMap(id -> userRepository.findById(id));

users.subscribe(System.out::println);

3. filter Operator​

The filter operator filters data streams, keeping only elements that meet a certain condition.

Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5)
.filter(n -> n % 2 == 0);

numbers.subscribe(System.out::println); // Output: 2, 4

4. reduce Operator​

The reduce operator aggregates all elements in a data stream into a single value. It takes an accumulator function, combining the previous result with the current element.

Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5);
Mono<Integer> sum = numbers.reduce((a, b) -> a + b);

sum.subscribe(System.out::println); // Output: 15

Practical Example: Building a Complex Data Stream Processing Application​

Below is an example showing how to use these operators to build a complex reactive application in Spring WebFlux.

1. Project Setup​

Ensure that the following dependencies are included in your project:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

2. Data Model​

Define a simple user data model:

public class User {
private String id;
private String name;

// Constructors, Getters and Setters
}

3. Mock Data Source​

Create a mock data source:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

public class UserRepository {
private static final List<User> users = Arrays.asList(
new User("1", "John Doe"),
new User("2", "Jane Doe"),
new User("3", "Sam Smith")
);

public Mono<User> findById(String id) {
return Mono.justOrEmpty(users.stream()
.filter(user -> user.getId().equals(id))
.findFirst());
}

public Flux<User> findAll() {
return Flux.fromIterable(users);
}
}

4. Handler Functions and Route Configuration​

Set up handler functions and route configuration:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Mono;

@Component
public class UserHandler {
private final UserRepository userRepository;

public UserHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}

public Mono<ServerResponse> getUser(ServerRequest request) {
String userId = request.pathVariable("id");
return userRepository.findById(userId)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> getAllUsers(ServerRequest request) {
return ServerResponse.ok().body(userRepository.findAll(), User.class);
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route()
.GET("/user/{id}", handler::getUser)
.GET("/users", handler::getAllUsers)
.build();
}
}

Conclusion​

In this article, we explored how to transform and operate on data streams in Spring WebFlux. Using different operators, we can flexibly handle data streams, building complex reactive applications.

Reactive Programming in Spring WebFlux Series - IX

· 阅读需 4 分钟


Test


Introduction

In previous articles, we explored the foundational concepts of Spring WebFlux, Reactor, error handling, data flow transformation, reactive database access, performance optimization, complex scenarios, and security. This article delves into how to conduct testing in Spring WebFlux to ensure that our reactive applications run reliably in both development and production environments.

Why Testing is Necessary​

Testing is an indispensable part of the software development process. Through testing, we can validate code correctness, ensure individual modules function as expected, and promptly identify and resolve potential issues. For reactive applications like those built with Spring WebFlux, testing is particularly important because reactive programming introduces asynchronous and non-blocking operations, which increase the complexity of debugging and validation.

Types of Testing in Spring WebFlux​

In Spring WebFlux, there are typically three main types of tests:

  • Unit Testing: Testing individual modules or components to ensure their behavior matches expectations.
  • Integration Testing: Testing the interactions between multiple modules to ensure they work together correctly.
  • End-to-End Testing: Simulating real user actions and verifying the behavior of the entire system.

1. Unit Testing​

Unit testing is the smallest test scope, focusing on individual functionality or components. In Spring WebFlux, we commonly test controllers, services, and data access layers.

1.1 Testing Controllers with WebTestClient​

WebTestClient is a powerful tool for testing the web layer of Spring WebFlux applications.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@WebFluxTest(UserHandler.class)
public class UserHandlerTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testGetUser() {
webTestClient.get().uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("John Doe");
}

@Test
void testGetAllUsers() {
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(3);
}
}

This example shows how to test a controller using WebTestClient, focusing on the GET /user/1 and GET /users endpoints.

1.2 Testing Services with Mockito​

Mockito is a popular mocking framework used to create and manage dependencies in tests.

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;

public class UserServiceTest {

@Mock
private UserRepository userRepository;

@InjectMocks
private UserService userService;

public UserServiceTest() {
MockitoAnnotations.openMocks(this);
}

@Test
void testGetUser() {
User user = new User("1", "John Doe");
when(userRepository.findById("1")).thenReturn(Mono.just(user));

Mono<User> result = userService.getUser("1");
assertEquals("John Doe", result.block().getName());
}
}

In this example, we use Mockito to mock the UserRepository and verify that the UserService correctly retrieves the user.

2. Integration Testing​

Integration tests validate the interactions between multiple modules. In Spring WebFlux, we usually use the @SpringBootTest annotation to load the entire application context and use WebTestClient to perform tests.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationIntegrationTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testGetUser() {
webTestClient.get().uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("John Doe");
}

@Test
void testGetAllUsers() {
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(3);
}
}

In this example, the entire application is loaded and tested for correctness across modules using WebTestClient.

3. End-to-End Testing​

End-to-end testing simulates real user actions and verifies the behavior of the entire system. These tests are generally more complex as they involve starting the whole application and mimicking various user interaction scenarios.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EndToEndTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testUserFlow() {
// Create a new user
User newUser = new User(null, "Alice");
webTestClient.post().uri("/user")
.bodyValue(newUser)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("Alice");

// Get all users
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(4);
}
}

This example tests the flow of creating a new user and then retrieving all users to ensure the system works end-to-end.

Conclusion​

In this article, we discussed how to perform testing in Spring WebFlux. By conducting unit tests, integration tests, and end-to-end tests, you can ensure that every part of your application functions as expected. Additionally, understanding common vulnerabilities like CSRF and XSS is crucial for building secure applications.

Reactive Programming in Spring WebFlux Series - V

· 阅读需 3 分钟

Reactive Database Access


Why Choose Reactive Database Access?​

Traditional database access methods are usually blocking, meaning that when a query is executing, the thread is blocked until the query is completed. This can lead to performance bottlenecks in highly concurrent environments. Reactive database access uses non-blocking I/O operations, allowing applications to handle more concurrent requests, improving system throughput and response times.


Spring Data R2DBC​

Spring Data R2DBC (Reactive Relational Database Connectivity) is a project in the Spring ecosystem that provides reactive programming support for relational databases. R2DBC is a new database connection specification designed to support reactive programming models.

1. Project Setup​

First, create a Spring Boot project and include the necessary dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
</dependency>
</dependencies>

2. Database Connection Configuration​

Configure the H2 database in application.properties:

spring.r2dbc.url=r2dbc:h2:mem:///testdb
spring.r2dbc.username=sa
spring.r2dbc.password=
spring.r2dbc.generate-unique-name=true

spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true

3. Data Model​

Define a simple User entity:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

@Table("users")
public class User {
@Id
private Long id;
private String name;

// Getters and Setters
}

4. Creating a Repository​

Create a reactive UserRepository interface:

import org.springframework.data.repository.reactive.ReactiveCrudRepository;

public interface UserRepository extends ReactiveCrudRepository<User, Long> {
}

5. Data Initialization​

Use an initializer class to preload some data:

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Flux;

@Configuration
public class DataInitializer {

@Bean
CommandLineRunner init(UserRepository repository) {
return args -> {
Flux<User> userFlux = Flux.just(
new User(null, "John Doe"),
new User(null, "Jane Doe"),
new User(null, "Sam Smith"))
.flatMap(repository::save);

userFlux
.thenMany(repository.findAll())
.subscribe(user -> System.out.println("Saving " + user.toString()));
};
}
}

6. Handler Functions and Route Configuration​

Create handler functions and route configurations to handle HTTP requests and return responses:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Mono;

@Component
public class UserHandler {
private final UserRepository userRepository;

public UserHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}

public Mono<ServerResponse> getUser(ServerRequest request) {
Long userId = Long.valueOf(request.pathVariable("id"));
return userRepository.findById(userId)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> getAllUsers(ServerRequest request) {
return ServerResponse.ok().body(userRepository.findAll(), User.class);
}

public Mono<ServerResponse> createUser(ServerRequest request) {
Mono<User> userMono = request.bodyToMono(User.class);
return userMono.flatMap(userRepository::save)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class));
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route()
.GET("/user/{id}", handler::getUser)
.GET("/users", handler::getAllUsers)
.POST("/user", handler::createUser)
.build();
}
}

Conclusion​

In this article, we explored how to perform reactive database access in Spring WebFlux. With Spring Data R2DBC, we can easily achieve non-blocking access to relational databases, greatly improving the performance and concurrency handling of applications.

Reactive Programming in Spring WebFlux Series - VI

· 阅读需 4 分钟

Reactive Programming in Complex Scenarios

In previous articles, we discussed the foundational concepts of Spring WebFlux, Reactor, error handling, data flow transformation, reactive database access, and performance optimization. This article explores how to apply reactive programming in complex scenarios, including flow control, backpressure handling, and integrating external services.

Complex Scenario 1: Flow Control and Backpressure Handling​

In high-concurrency environments, controlling data flow and managing backpressure are critical for ensuring system stability. Backpressure occurs when downstream consumers cannot keep up with the speed of upstream producers, leading to data accumulation and resource exhaustion. Reactor provides various ways to handle backpressure issues.

1. Using limitRate​

The limitRate operator limits the amount of data requested at a time, thereby controlling the speed of data flow.

Flux.range(1, 1000)
.limitRate(100)
.subscribe(System.out::println);

2. Using onBackpressureBuffer​

The onBackpressureBuffer operator buffers data during backpressure until the downstream can process it.

Flux.range(1, 1000)
.onBackpressureBuffer(50)
.subscribe(System.out::println);

3. Using onBackpressureDrop​

The onBackpressureDrop operator drops data during backpressure to prevent downstream overload.

Flux.range(1, 1000)
.onBackpressureDrop()
.subscribe(System.out::println);

Complex Scenario 2: Integrating External Services​

In real projects, interactions with external services are common, such as calling REST APIs or using message queues. Spring WebFlux provides powerful integration capabilities.

1. Using WebClient to Call REST APIs​

WebClient is a non-blocking HTTP client provided by Spring WebFlux, suitable for interacting with external REST services.

import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class WebClientExample {
private final WebClient webClient = WebClient.create("http://example.com");

public Mono<String> fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class);
}
}

2. Using Reactive Kafka for Message Processing​

Spring WebFlux integrates seamlessly with Reactive Kafka to achieve non-blocking message processing.

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-reactive</artifactId>
</dependency>
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import reactor.core.publisher.FluxSink;

@Component
public class KafkaConsumer {
private final FluxSink<String> sink;

public KafkaConsumer(FluxSink<String> sink) {
this.sink = sink;
}

@KafkaListener(topics = "example-topic", groupId = "group_id")
public void listen(String message) {
sink.next(message);
}
}

Complex Scenario 3: Handling Large Files​

When processing large files, traditional reading methods can lead to excessive memory consumption. Using Spring WebFlux allows for non-blocking, streaming file processing.

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class FileHandler {
public Mono<ServerResponse> uploadFile(ServerRequest request) {
return request.multipartData()
.flatMap(parts -> {
FilePart filePart = (FilePart) parts.toSingleValueMap().get("file");
Flux<DataBuffer> content = filePart.content();
return ServerResponse.ok().body(content, DataBuffer.class);
});
}
}

Conclusion​

In this article, we explored several strategies and methods for applying Spring WebFlux in complex scenarios. By effectively managing backpressure, integrating external services, and processing large files in a streaming fashion, we can build more robust and efficient reactive applications.

Furthermore​

  1. Kubernetes for Deployment: Consider deploying your reactive applications using Kubernetes for better scalability and management.
  2. Reactive Programming Patterns: Implement design patterns such as Circuit Breaker and Rate Limiter to enhance application resilience.
  3. Monitoring with Micrometer: Use Micrometer to instrument your reactive applications for better observability and performance metrics.
  4. Serverless Architecture: Explore serverless solutions to handle unpredictable workloads efficiently, integrating with AWS Lambda or Azure Functions.

Reactive Programming in Spring WebFlux Series - VII

· 阅读需 4 分钟

Reactive Programming in Complex Scenarios

Introduction

In previous articles, we discussed the foundational concepts of Spring WebFlux, Reactor, error handling, data flow transformation, reactive database access, and performance optimization. This article explores how to apply reactive programming in complex scenarios, including flow control, backpressure handling, and integrating external services.

Complex Scenario 1: Flow Control and Backpressure Handling​

In high-concurrency environments, controlling data flow and managing backpressure are critical for ensuring system stability. Backpressure occurs when downstream consumers cannot keep up with the speed of upstream producers, leading to data accumulation and resource exhaustion. Reactor provides various ways to handle backpressure issues.

1. Using limitRate​

The limitRate operator limits the amount of data requested at a time, thereby controlling the speed of data flow.

Flux.range(1, 1000)
.limitRate(100)
.subscribe(System.out::println);

2. Using onBackpressureBuffer​

The onBackpressureBuffer operator buffers data during backpressure until the downstream can process it.

Flux.range(1, 1000)
.onBackpressureBuffer(50)
.subscribe(System.out::println);

3. Using onBackpressureDrop​

The onBackpressureDrop operator drops data during backpressure to prevent downstream overload.

Flux.range(1, 1000)
.onBackpressureDrop()
.subscribe(System.out::println);

Complex Scenario 2: Integrating External Services​

In real projects, interactions with external services are common, such as calling REST APIs or using message queues. Spring WebFlux provides powerful integration capabilities.

1. Using WebClient to Call REST APIs​

WebClient is a non-blocking HTTP client provided by Spring WebFlux, suitable for interacting with external REST services.

import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class WebClientExample {
private final WebClient webClient = WebClient.create("http://example.com");

public Mono<String> fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class);
}
}

2. Using Reactive Kafka for Message Processing​

Spring WebFlux integrates seamlessly with Reactive Kafka to achieve non-blocking message processing.

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-reactive</artifactId>
</dependency>
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import reactor.core.publisher.FluxSink;

@Component
public class KafkaConsumer {
private final FluxSink<String> sink;

public KafkaConsumer(FluxSink<String> sink) {
this.sink = sink;
}

@KafkaListener(topics = "example-topic", groupId = "group_id")
public void listen(String message) {
sink.next(message);
}
}

Complex Scenario 3: Handling Large Files​

When processing large files, traditional reading methods can lead to excessive memory consumption. Using Spring WebFlux allows for non-blocking, streaming file processing.

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class FileHandler {
public Mono<ServerResponse> uploadFile(ServerRequest request) {
return request.multipartData()
.flatMap(parts -> {
FilePart filePart = (FilePart) parts.toSingleValueMap().get("file");
Flux<DataBuffer> content = filePart.content();
return ServerResponse.ok().body(content, DataBuffer.class);
});
}
}

Conclusion​

In this article, we explored several strategies and methods for applying Spring WebFlux in complex scenarios. By effectively managing backpressure, integrating external services, and processing large files in a streaming fashion, we can build more robust and efficient reactive applications.

Furthermore​

  1. Kubernetes for Deployment: Consider deploying your reactive applications using Kubernetes for better scalability and management.
  2. Reactive Programming Patterns: Implement design patterns such as Circuit Breaker and Rate Limiter to enhance application resilience.
  3. Monitoring with Micrometer: Use Micrometer to instrument your reactive applications for better observability and performance metrics.
  4. Serverless Architecture: Explore serverless solutions to handle unpredictable workloads efficiently, integrating with AWS Lambda or Azure Functions.

Reactive Programming in Spring WebFlux Series - VIII

· 阅读需 5 分钟

Security


Introduction

In previous articles, we discussed the foundational concepts of Spring WebFlux, Reactor, error handling, data flow transformation, reactive database access, performance optimization, and applying these concepts in complex scenarios. This article will dive into how to implement security in Spring WebFlux to ensure that our applications run safely in high-concurrency environments.

Why Security is Necessary​

In any web application, security is crucial. It protects user data from unauthorized access and modifications while preventing common security threats such as Cross-Site Scripting (XSS), SQL Injection, and Cross-Site Request Forgery (CSRF). In Spring WebFlux, security must be carefully considered and implemented to ensure a secure reactive environment.

Integrating Spring Security with WebFlux​

Spring Security is a powerful framework within the Spring ecosystem, designed to secure Spring applications. It supports integration with Spring WebFlux, providing comprehensive security for reactive applications.

1. Adding Dependencies​

First, add the necessary Spring Security dependencies to your project:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

2. Configuring Security Policies​

Next, create a configuration class to define your security policies:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange(exchanges -> exchanges
.pathMatchers("/login", "/public/**").permitAll()
.anyExchange().authenticated()
)
.httpBasic().and()
.formLogin().and()
.csrf().disable(); // CSRF is disabled for this example but should be enabled in production
return http.build();
}
}

In this configuration, we define basic security rules: allowing unauthenticated users to access the login page and public resources while requiring authentication for other requests. We’ve enabled basic authentication and form login.

3. Customizing User Details Service​

To handle user authentication, you can customize the user details service:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

@Configuration
public class UserDetailsServiceConfig {

@Bean
public MapReactiveUserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
UserDetails admin = User.withDefaultPasswordEncoder()
.username("admin")
.password("password")
.roles("ADMIN")
.build();
return new MapReactiveUserDetailsService(user, admin);
}
}

Preventing Common Security Threats​

In this section, we will cover some common security threats and how to prevent them in Spring WebFlux.

1. CSRF (Cross-Site Request Forgery)​

In production environments, it is recommended to enable CSRF protection to prevent CSRF attacks:

http
.csrf().csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse());

2. XSS (Cross-Site Scripting)​

To prevent XSS attacks, ensure that user input is properly encoded when rendered. Spring Security provides built-in mechanisms to handle encoding in most cases.

Security Best Practices​

  1. Use Strong Passwords: Ensure users create strong passwords and update them regularly.
  2. Principle of Least Privilege: Grant users only the minimum privileges necessary to perform their tasks.
  3. Enable HTTPS: Use HTTPS to encrypt data transmission, preventing data interception or tampering.
  4. Regularly Update Dependencies: Keep Spring Security and other dependencies up-to-date to include the latest security patches.
  5. Implement Multi-Factor Authentication (MFA): Enhance authentication security by adding an extra layer of verification.

CSRF and XSS Attacks​

What is CSRF?​

Cross-Site Request Forgery (CSRF) is an attack where a malicious website tricks a user into executing actions on another website where they are authenticated. For example, if a user is logged into their banking website, a malicious website could make unauthorized money transfers on behalf of the user.

Mitigating CSRF​

  • Use CSRF tokens: These tokens ensure that requests made to sensitive endpoints are genuine by embedding a unique token in each form or API request, which the server validates.
  • SameSite cookies: Restricting cookies with the SameSite attribute can reduce the risk of CSRF by ensuring cookies are sent only for same-site requests.

What is XSS?​

Cross-Site Scripting (XSS) is an attack where malicious scripts are injected into websites and executed in users' browsers. This can be used to steal sensitive information like cookies, session tokens, or passwords.

Mitigating XSS​

  • Input validation and output encoding: Always validate user inputs and encode any untrusted data before displaying it in a browser. This ensures that malicious scripts cannot be injected into the HTML.
  • Content Security Policy (CSP): Use CSP headers to restrict the types of content that can be executed on your web pages, preventing the browser from executing malicious scripts.

Conclusion​

In this article, we explored how to implement security in Spring WebFlux applications. By leveraging Spring Security, we can provide comprehensive security for reactive applications, ensuring protection against common threats like CSRF and XSS. Adopting best practices such as HTTPS, strong passwords, and least privilege can further strengthen your application's security.