Building a Full Reactive Stack Backend with Spring Cloud, WebFlux, and Reactive MongoDB
This article demonstrates how to create a fully reactive backend using Spring Cloud Finchley, WebFlux, Eureka service discovery, and Reactive MongoDB, covering service registration, inter‑service communication with WebClient, and reactive data access to achieve a complete reactive microservices stack.
Spring Cloud traditionally uses synchronous REST calls via RestTemplate or Feign, but with Spring Cloud Finchley we can adopt reactive, non‑blocking communication.
We build a full reactive backend consisting of a Eureka service registry, two microservices (account and customer), and a MongoDB database accessed through Spring Data Reactive.
First, a single‑node Eureka server is created with the dependency <dependency>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency> and configured in application.yml to run on port 8000 and disable registration/fetching.
In the account service we add dependencies for spring-boot-starter-data-mongodb-reactive , spring-boot-starter-webflux , spring-cloud-starter-netflix-eureka-client , and Lombok. The Account entity is annotated with @Document, and a reactive repository AccountMongoReactiveRepository extends ReactiveCrudRepository<Account, String> provides a method Flux<Account> findByCustomerId(String customerId) . The controller exposes @GetMapping("/customer/{customer}") returning the accounts.
The customer service mirrors the setup, defining a Customer entity and a CustomerMongoReactiveRepository . Its controller offers standard CRUD endpoints and, after adding a WebClient.Builder bean marked @LoadBalanced, implements @GetMapping("/{id}/account") that calls the account service via WebClient to retrieve all accounts for a customer.
WebClient configuration is provided in @Configuration class WebClientConfig with a load‑balanced bean public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } . An alternative usage shows injecting LoadBalancerExchangeFilterFunction and building a WebClient with .filter(lbFunction) .
Docker is used to start a MongoDB container: docker run -d --name mongo -p 27017:27017 mongo . After launching the Eureka server and both microservices, requests to http://localhost:8200/customer/{id}/account are load‑balanced across the two account instances, as demonstrated by timestamped logs.
By employing reactive programming for service registration, inter‑service communication with WebClient, and reactive data access, the example achieves a full reactive stack backend.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.