Skip to content

ReactiveCompositeDiscoveryClient does not respect discovery clients order #1542

@Vsevolod123

Description

@Vsevolod123

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions