Class DiscoveryClientNameResolver
java.lang.Object
io.grpc.NameResolver
net.devh.boot.grpc.client.nameresolver.DiscoveryClientNameResolver
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.-
Nested Class Summary
Nested classes/interfaces inherited from class io.grpc.NameResolver
NameResolver.Args, NameResolver.ConfigOrError, NameResolver.Factory, NameResolver.Listener, NameResolver.Listener2, NameResolver.ResolutionResult, NameResolver.ResolutionResultAttr, NameResolver.ServiceConfigParser -
Constructor Summary
ConstructorsConstructorDescriptionDiscoveryClientNameResolver(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. -
Method Summary
Modifier and TypeMethodDescriptionprotected List<org.springframework.cloud.client.ServiceInstance>Discovers matching service instances.protected AttributesgetAttributes(org.springframework.cloud.client.ServiceInstance serviceInstance) Gets the attributes from the service instance for later use in a load balancer.protected intgetGrpcPort(org.springframework.cloud.client.ServiceInstance instance) Extracts the gRPC server port from the given service instance.protected final StringgetName()Gets the name of the service to get the instances of.final Stringprotected StringgetServiceConfig(List<org.springframework.cloud.client.ServiceInstance> instances) Extracts the gRPC service config string from the given service instances.protected final booleanisActive()Checks whether this resolver is active.protected booleanneedsToUpdateConnections(List<org.springframework.cloud.client.ServiceInstance> newInstanceList) Checks whether this instance should update its connections.voidrefresh()voidTriggers a refresh on the listener from non-grpc threads.voidshutdown()voidstart(NameResolver.Listener2 listener) toString()Methods inherited from class io.grpc.NameResolver
start
-
Constructor Details
-
Method Details
-
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.#starthas been called, but not#shutdown().- Returns:
- True, if there is a listener attached. False, otherwise.
-
getServiceAuthority
- Specified by:
getServiceAuthorityin classNameResolver
-
start
- Overrides:
startin classNameResolver
-
refresh
public void refresh()- Overrides:
refreshin classNameResolver
-
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
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.
-
getServiceConfig
Extracts the gRPC service config string from the given service instances.- Parameters:
instances- The list of instances to extract the service config from.- Returns:
- The gRPC service config or null.
-
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:
shutdownin classNameResolver
-
toString
-