spring-cloud / spring-cloud/spring-cloud-commons

ReactiveCompositeDiscoveryClient does not respect discovery clients order

Open
#1,542 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
751
Forks
744
Avg merge
1d 14h
Merged PRs (30d)
9

Description

Description

The current implementation of ReactiveCompositeDiscoveryClient does not respect the order of discovery clients. Instead of returning results in the specified order, it returns the first non-empty Flux from the fastest responding client. This behavior differs from the non-reactive CompositeDiscoveryClient, which respects the order of discovery clients.

Problem Analysis

The default behavior of ReactiveCompositeDiscoveryClient can lead to unexpected results when multiple discovery clients are configured with different priorities or when specific ordering is required for service discovery.

Proposed Solution

To address this issue, a custom implementation of ReactiveCompositeDiscoveryClient can be provided. This implementation ensures that discovery clients are queried in the specified order and returns results from the first non-empty response.

Author’s Note

Although the author is not an expert in reactive Java, this solution appears to work correctly based on testing. Further review by experienced developers is recommended to ensure optimal performance and correctness.

@Bean
@Primary
ReactiveCompositeDiscoveryClient reactiveCompositeDiscoveryClient(List<ReactiveDiscoveryClient> discoveryClients) {
    return new ReactiveCompositeDiscoveryClient(discoveryClients) {
        @Override
        public Flux<ServiceInstance> getInstances(String serviceId) {
            if (discoveryClients == null || discoveryClients.isEmpty()) {
                return Flux.empty();
            }

            return Flux.fromIterable(discoveryClients)
                    .concatMap(client -> client.getInstances(serviceId).collectList())
                    .filter(Predicate.not(List::isEmpty))
                    .next()
                    .flatMapMany(Flux::fromIterable);
        }
    };
}

Usage Instructions

To apply this solution, exclude the default ReactiveCompositeDiscoveryClientAutoConfiguration and register the custom bean in your Spring configuration.

Notes

  • This implementation preserves the order of discovery clients.
  • It collects results from each client sequentially.
  • The first non-empty result is returned, respecting the configured order.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ReactiveCompositeDiscoveryClient and ReactiveCompositeDiscoveryClientAutoConfiguration, then compare their behavior with the non-reactive CompositeDiscoveryClient. Verify the reactive implementation queries clients in configured order and returns the first non-empty result, with tests covering multiple clients and response timing.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.