Class RegistryAwareClient

  • All Implemented Interfaces:
    javax.ws.rs.client.Client, javax.ws.rs.core.Configurable<javax.ws.rs.client.Client>

    public class RegistryAwareClient
    extends Object
    implements javax.ws.rs.client.Client
    An extension of the JAX-RS Client interface that provides additional target(...) methods that will find service instances from a registry, e.g. Consul.
    • Constructor Summary

      Constructors 
      Constructor Description
      RegistryAwareClient​(javax.ws.rs.client.Client client, org.kiwiproject.registry.client.RegistryClient registryClient)
      Creates a new RegistryAwareClient with the given Client and RegistryClient.
      RegistryAwareClient​(javax.ws.rs.client.Client client, org.kiwiproject.registry.client.RegistryClient registryClient, Supplier<Map<String,​Object>> headersSupplier)
      Creates a new RegistryAwareClient with the given Client, RegistryClient and Supplier that will be used to automatically attach request headers to all requests made by this client.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javax.ws.rs.client.Client client()
      Return the underlying "raw" JAX-RS Client instance.
      javax.ws.rs.client.WebTarget targetForService​(String serviceName)
      Provide a WebTarget by looking up a service in the registry using the given service name.
      javax.ws.rs.client.WebTarget targetForService​(String serviceName, org.kiwiproject.registry.model.Port.PortType portType)
      Provide a WebTarget by looking up a service in the registry using the given service name and Port.PortType .
      javax.ws.rs.client.WebTarget targetForService​(ServiceIdentifier identifier)
      Provide a WebTarget by looking up a service in the registry using the given service identifier.
      javax.ws.rs.client.WebTarget targetForService​(ServiceIdentifier originalIdentifier, org.kiwiproject.registry.model.Port.PortType portType)
      Provide a WebTarget by looking up a service in the registry using the given ServiceIdentifier and PortType.
      javax.ws.rs.client.WebTarget targetForService​(ServiceIdentifier original, org.kiwiproject.registry.model.Port.PortType portType, Function<org.kiwiproject.registry.model.ServiceInstance,​String> pathResolver)
      Provide a WebTarget by looking up a service in the registry using the given service identifier.
      • Methods inherited from interface javax.ws.rs.client.Client

        close, getHostnameVerifier, getSslContext, invocation, target, target, target, target
      • Methods inherited from interface javax.ws.rs.core.Configurable

        getConfiguration, property, register, register, register, register, register, register, register, register
    • Constructor Detail

      • RegistryAwareClient

        public RegistryAwareClient​(javax.ws.rs.client.Client client,
                                   org.kiwiproject.registry.client.RegistryClient registryClient)
        Creates a new RegistryAwareClient with the given Client and RegistryClient.
        Parameters:
        client - the Jersey client to use
        registryClient - the registry lookup client
      • RegistryAwareClient

        public RegistryAwareClient​(javax.ws.rs.client.Client client,
                                   org.kiwiproject.registry.client.RegistryClient registryClient,
                                   @Nullable
                                   Supplier<Map<String,​Object>> headersSupplier)
        Creates a new RegistryAwareClient with the given Client, RegistryClient and Supplier that will be used to automatically attach request headers to all requests made by this client.

        If headersSupplier is null, it is ignored.

        Parameters:
        client - the Jersey client to use
        registryClient - the registry lookup client
        headersSupplier - a supplier of headers to attach to requests, may be null
    • Method Detail

      • client

        public javax.ws.rs.client.Client client()
        Return the underlying "raw" JAX-RS Client instance. Generally won't be needed but this provides an "escape hatch" if it is needed for some reason. Use wisely, sparingly, or not at all...
        Returns:
        the underlying "raw" JAX-RS Client
      • targetForService

        public javax.ws.rs.client.WebTarget targetForService​(String serviceName)
        Provide a WebTarget by looking up a service in the registry using the given service name. Finds the latest available version. If more than one instance is found, then one of them is randomly chosen.

        Note: The returned WebTarget will always be set up to access the application port on the service.

        Parameters:
        serviceName - the service name in the registry
        Returns:
        a WebTarget for a randomly selected service instance
        See Also:
        targetForService(String, PortType), targetForService(ServiceIdentifier)
      • targetForService

        public javax.ws.rs.client.WebTarget targetForService​(String serviceName,
                                                             org.kiwiproject.registry.model.Port.PortType portType)
        Provide a WebTarget by looking up a service in the registry using the given service name and Port.PortType . Finds the latest available version. If more than one instance is found, then one of them is randomly chosen.
        Parameters:
        serviceName - the service name in the registry
        portType - the type of port to connect to
        Returns:
        a WebTarget for a randomly selected service instance
        See Also:
        targetForService(ServiceIdentifier)
      • targetForService

        public javax.ws.rs.client.WebTarget targetForService​(ServiceIdentifier originalIdentifier,
                                                             org.kiwiproject.registry.model.Port.PortType portType)
        Provide a WebTarget by looking up a service in the registry using the given ServiceIdentifier and PortType. Finds the latest available version. If more than one instance is found, then one of them is randomly chosen.
        Parameters:
        originalIdentifier - the original identifier that will be adjusted with the given port type
        portType - the port type to use for the WebTarget
        Returns:
        a WebTarget for a randomly selected service instance
        See Also:
        targetForService(ServiceIdentifier)
      • targetForService

        public javax.ws.rs.client.WebTarget targetForService​(ServiceIdentifier identifier)
        Provide a WebTarget by looking up a service in the registry using the given service identifier. If more than one instance is found, then one of them is randomly chosen.

        Note: By specifying the connector as Port.PortType.ADMIN in identifier the WebTarget will be set up to access the admin port on the service.

        Parameters:
        identifier - uniquely identifies the service
        Returns:
        a WebTarget for a randomly selected service instance
      • targetForService

        public javax.ws.rs.client.WebTarget targetForService​(ServiceIdentifier original,
                                                             org.kiwiproject.registry.model.Port.PortType portType,
                                                             Function<org.kiwiproject.registry.model.ServiceInstance,​String> pathResolver)
        Provide a WebTarget by looking up a service in the registry using the given service identifier. If more than one instance is found, then one of them is randomly chosen. The given pathResolver function allows a path to be chosen from the ServiceInstance and added to the WebTarget path.
        Parameters:
        original - the original ServiceIdentifier used to lookup a service
        portType - the port type (APPLICATION or ADMIN) to use for the WebTarget port
        pathResolver - a function to resolve the path to use from the ServiceInstance
        Returns:
        a WebTarget for a randomly selected service instance