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 Attributes
getAttributes
(org.springframework.cloud.client.ServiceInstance serviceInstance) Gets the attributes from the service instance for later use in a load balancer.protected int
getGrpcPort
(org.springframework.cloud.client.ServiceInstance instance) Extracts the gRPC server port from the given service instance.protected final String
getName()
Gets the name of the service to get the instances of.final String
protected final boolean
isActive()
Checks whether this resolver is active.protected boolean
needsToUpdateConnections
(List<org.springframework.cloud.client.ServiceInstance> newInstanceList) Checks whether this instance should update its connections.void
refresh()
void
Triggers a refresh on the listener from non-grpc threads.void
shutdown()
void
start
(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.#start
has been called, but not#shutdown()
.- Returns:
- True, if there is a listener attached. False, otherwise.
-
getServiceAuthority
- Specified by:
getServiceAuthority
in classNameResolver
-
start
- Overrides:
start
in classNameResolver
-
refresh
public void refresh()- Overrides:
refresh
in 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.
-
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 classNameResolver
-
toString
-