Interface CapabilityDiscoveryMode

All Known Implementing Classes:
IgnoreListingDiscoveryMode, RestCapabilityDiscoveryMode

public interface CapabilityDiscoveryMode
Discovers the MemberCapabilities of the ServiceInstances reported by Spring Cloud Discovery, and publishes this application's own.

Every application is identified by a node id of its own, which it reports along with its capabilities. Members are told apart by that id rather than by what discovery reports about them, so that one application reported more than once is still one member, and so that this application recognizes its own instance among the others without having to match its address.

Capabilities cannot travel through service instance metadata: that metadata is fixed at registration time, and mutating it after the fact is silently ignored by several discovery implementations — Kubernetes among them. Since the set of subscribed command names changes as an application starts up and is reconfigured, capabilities are instead served over an endpoint of their own, which is what RestCapabilityDiscoveryMode does.

Since:
5.4.0
Author:
Allard Buijze, Steven van Beelen
  • Method Details

    • localNodeId

      String localNodeId()
      Returns the identifier of this application, which it reports to other members along with its capabilities.

      Unique to the running process: an application that restarts is a new member, which matters because it has not seen what the previous process did.

      Returns:
      the identifier of this application
    • updateLocalCapabilities

      void updateLocalCapabilities(MemberCapabilities capabilities)
      Publishes the given capabilities as this application's own, replacing whatever was published before.

      Called whenever a command or query handler is subscribed to, or unsubscribed from, this application's connector.

      Parameters:
      capabilities - the messages this application handles, and the command load it asks for
    • discover

      Optional<MemberAdvertisement> discover(org.springframework.cloud.client.ServiceInstance serviceInstance)
      Discovers which member the given serviceInstance is, and what it handles.

      An empty Optional means the instance should be left out of the routing ring entirely — it is not a member of this cluster, or is not currently answering for one. That differs from an advertisement carrying MemberCapabilities.INCAPABLE, which keeps the instance in the ring as a member that handles nothing.

      This application's own instance is discovered like any other, answering with this application's node id.

      Called concurrently, once per discovered instance, for every instance of one discovery round. Implementations must therefore be thread-safe.

      Parameters:
      serviceInstance - the instance to discover
      Returns:
      the member the given serviceInstance is and what it handles, or Optional.empty() when it should not be part of the routing ring
      Throws:
      ServiceInstanceClientException - when the given serviceInstance answers with a client error, indicating it does not serve capabilities at all
    • localCapabilities

      MemberCapabilities localCapabilities()
      Returns this application's own capabilities, as last published through updateLocalCapabilities(MemberCapabilities).

      A mode is told what this application handles so it can publish it; that same knowledge is what MemberCapabilitiesController serves to other members, which is why reading it back is part of this contract rather than of any one implementation.

      Returns:
      this application's own capabilities, or MemberCapabilities.INCAPABLE while nothing has been published yet
    • retainOnly

      default void retainOnly(Set<ServiceInstanceKey> knownInstances)
      Signals that the given knownInstances are the only instances discovery currently reports, allowing this mode to discard whatever it remembers about instances that have gone away.

      Called at the end of each discovery round. Implementations that keep no per-instance state need not do anything, which is what this default implementation does.

      Parameters:
      knownInstances - the keys of every instance the current discovery round reported