Skip to content

io microsphere spring cloud client discovery UnionDiscoveryClient

github-actions[bot] edited this page Apr 8, 2026 · 3 revisions

UnionDiscoveryClient

Type: Class | Module: microsphere-spring-cloud-commons | Package: io.microsphere.spring.cloud.client.discovery | Since: 1.0.0

Source: microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/discovery/UnionDiscoveryClient.java

Overview

The DiscoveryClient implementation for a union of the given DiscoveryClient

Example Usage:

`// Register UnionDiscoveryClient as a Spring bean, then retrieve merged service instances
UnionDiscoveryClient unionDiscoveryClient = applicationContext.getBean(UnionDiscoveryClient.class);
List instances = unionDiscoveryClient.getInstances("test");
List services = unionDiscoveryClient.getServices();
`

Declaration

public final class UnionDiscoveryClient implements DiscoveryClient, ApplicationContextAware, SmartInitializingSingleton, DisposableBean

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.9-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// Register UnionDiscoveryClient as a Spring bean, then retrieve merged service instances
UnionDiscoveryClient unionDiscoveryClient = applicationContext.getBean(UnionDiscoveryClient.class);
List<ServiceInstance> instances = unionDiscoveryClient.getInstances("test");
List<String> services = unionDiscoveryClient.getServices();

Method Examples

description

String desc = unionDiscoveryClient.description();

getInstances

List<ServiceInstance> instances = unionDiscoveryClient.getInstances("test");

getServices

List<String> services = unionDiscoveryClient.getServices();

getDiscoveryClients

UnionDiscoveryClient unionClient = applicationContext.getBean(UnionDiscoveryClient.class);
List<DiscoveryClient> clients = unionClient.getDiscoveryClients();
clients.forEach(c -> System.out.println(c.description()));

getOrder

int order = unionDiscoveryClient.getOrder();

afterSingletonsInstantiated

// Automatically called by the Spring container after singleton initialization
unionDiscoveryClient.afterSingletonsInstantiated();

destroy

// Automatically called by the Spring container on shutdown
unionDiscoveryClient.destroy();

setApplicationContext

// Automatically called by the Spring container
unionDiscoveryClient.setApplicationContext(applicationContext);

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-spring-cloud-commons</artifactId>
    <version>${microsphere-spring-cloud.version}</version>
</dependency>

Tip: Use the BOM (microsphere-spring-cloud-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.spring.cloud.client.discovery.UnionDiscoveryClient;

API Reference

Public Methods

Method Description
description Returns a human-readable description of this DiscoveryClient.
getInstances Returns a merged list of ServiceInstance objects from all registered
getServices Returns a deduplicated list of service names from all registered DiscoveryClient
getDiscoveryClients Returns the sorted list of underlying DiscoveryClient DiscoveryClients, excluding
getOrder Returns the order value of this client. This client uses HIGHEST_PRECEDENCE
afterSingletonsInstantiated Callback invoked after all singleton beans have been instantiated. Eagerly initializes
destroy Clears the cached list of DiscoveryClient instances when this bean is destroyed.
setApplicationContext Sets the ApplicationContext used to look up DiscoveryClient beans.

Method Details

description

public String description()

Returns a human-readable description of this DiscoveryClient.

Example Usage:

`String desc = unionDiscoveryClient.description();
`

getInstances

public List<ServiceInstance> getInstances(String serviceId)

Returns a merged list of ServiceInstance objects from all registered DiscoveryClient instances for the given service ID.

Example Usage:

`List instances = unionDiscoveryClient.getInstances("test");
`

getServices

public List<String> getServices()

Returns a deduplicated list of service names from all registered DiscoveryClient instances, preserving insertion order.

Example Usage:

`List services = unionDiscoveryClient.getServices();
`

getDiscoveryClients

public List<DiscoveryClient> getDiscoveryClients()

Returns the sorted list of underlying DiscoveryClient DiscoveryClients, excluding CompositeDiscoveryClient and this instance itself. The list is lazily initialized from the ApplicationContext on first access and cached for subsequent calls.

Example Usage:

`UnionDiscoveryClient unionClient = applicationContext.getBean(UnionDiscoveryClient.class);
List clients = unionClient.getDiscoveryClients();
clients.forEach(c -> System.out.println(c.description()));
`

getOrder

public int getOrder()

Returns the order value of this client. This client uses HIGHEST_PRECEDENCE to ensure it takes priority over other DiscoveryClient implementations.

Example Usage:

`int order = unionDiscoveryClient.getOrder();
`

afterSingletonsInstantiated

public void afterSingletonsInstantiated()

Callback invoked after all singleton beans have been instantiated. Eagerly initializes the internal list of DiscoveryClient instances.

Example Usage:

`// Automatically called by the Spring container after singleton initialization
unionDiscoveryClient.afterSingletonsInstantiated();
`

destroy

public void destroy()

Clears the cached list of DiscoveryClient instances when this bean is destroyed.

Example Usage:

`// Automatically called by the Spring container on shutdown
unionDiscoveryClient.destroy();
`

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)

Sets the ApplicationContext used to look up DiscoveryClient beans.

Example Usage:

`// Automatically called by the Spring container
unionDiscoveryClient.setApplicationContext(applicationContext);
`

See Also

  • CompositeDiscoveryClient

This documentation was auto-generated from the source code of microsphere-spring-cloud.

Home

spring-cloud-commons

spring-cloud-openfeign

Clone this wiki locally