Class DiscoveryClientNameResolver

java.lang.Object
io.grpc.NameResolver
net.devh.boot.grpc.client.nameresolver.DiscoveryClientNameResolver

public class DiscoveryClientNameResolver extends NameResolver
The DiscoveryClientNameResolver resolves the service hosts and their associated gRPC port using the channel's name and spring's cloud DiscoveryClient. The ports are extracted from the gRPC_port metadata.
  • Constructor Details

    • DiscoveryClientNameResolver

      public DiscoveryClientNameResolver(String name, org.springframework.cloud.client.discovery.DiscoveryClient client, NameResolver.Args args, io.grpc.internal.SharedResourceHolder.Resource<Executor> executorResource, Consumer<DiscoveryClientNameResolver> shutdownHook)
      Creates a new DiscoveryClientNameResolver.
      Parameters:
      name - The name of the service to look up.
      client - The client used to look up the service addresses.
      args - The name resolver args.
      executorResource - The executor resource.
      shutdownHook - The optional cleaner used during shutdown()
  • Method Details

    • getName

      protected final String getName()
      Gets the name of the service to get the instances of.
      Returns:
      The name associated with this resolver.
    • isActive

      protected final boolean isActive()
      Checks whether this resolver is active. E.g. #start has been called, but not #shutdown().
      Returns:
      True, if there is a listener attached. False, otherwise.
    • getServiceAuthority

      public final String getServiceAuthority()
      Specified by:
      getServiceAuthority in class NameResolver
    • start

      public void start(NameResolver.Listener2 listener)
      Overrides:
      start in class NameResolver
    • refresh

      public void refresh()
      Overrides:
      refresh in class NameResolver
    • refreshFromExternal

      public void refreshFromExternal()
      Triggers a refresh on the listener from non-grpc threads. This method can safely be called, even if the listener hasn't been started yet.
      See Also:
    • discoverServers

      protected List<org.springframework.cloud.client.ServiceInstance> discoverServers()
      Discovers matching service instances. Can be overwritten to apply some custom filtering.
      Returns:
      A list of service instances to use.
    • getGrpcPort

      protected int getGrpcPort(org.springframework.cloud.client.ServiceInstance instance)
      Extracts the gRPC server port from the given service instance. Can be overwritten for a custom port mapping.
      Parameters:
      instance - The instance to extract the port from.
      Returns:
      The gRPC server port.
      Throws:
      IllegalArgumentException - If the specified port definition couldn't be parsed.
    • getAttributes

      protected Attributes getAttributes(org.springframework.cloud.client.ServiceInstance serviceInstance)
      Gets the attributes from the service instance for later use in a load balancer. Can be overwritten to convert custom attributes.
      Parameters:
      serviceInstance - The service instance to get them from.
      Returns:
      The newly created attributes for the given instance.
    • needsToUpdateConnections

      protected boolean needsToUpdateConnections(List<org.springframework.cloud.client.ServiceInstance> newInstanceList)
      Checks whether this instance should update its connections.
      Parameters:
      newInstanceList - The new instances that should be compared to the stored ones.
      Returns:
      True, if the given instance list contains different entries than the stored ones.
    • shutdown

      public void shutdown()
      Specified by:
      shutdown in class NameResolver
    • toString

      public String toString()
      Overrides:
      toString in class Object