Class AgentClient


  • public class AgentClient
    extends java.lang.Object
    HTTP Client for /v1/agent/ endpoints.
    See Also:
    The Consul API Docs
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Http http  
    • Method Summary

      Modifier and Type Method Description
      void check​(java.lang.String checkId, State state, java.lang.String note)
      Checks in with Consul.
      void checkTtl​(java.lang.String serviceId, State state, java.lang.String note)
      Prepends the default TTL prefix to the serviceId to produce a check id, then delegates to check(String checkId, State state, String note) This method only works with TTL checks that have not been given a custom name.
      void deregister​(java.lang.String serviceId)
      De-register a particular service from the Consul Agent.
      void deregister​(java.lang.String serviceId, QueryOptions options)
      De-register a particular service from the Consul Agent.
      void deregisterCheck​(java.lang.String checkId)
      De-registers a Health Check with the Agent
      void fail​(java.lang.String serviceId)
      Sets a TTL service check to "critical" state.
      void fail​(java.lang.String serviceId, java.lang.String note)
      Sets a TTL service check to "critical" state with a note.
      void failCheck​(java.lang.String checkId)
      Sets a TTL check to "critical" state.
      void failCheck​(java.lang.String checkId, java.lang.String note)
      Sets a TTL check to "critical" state with a note.
      void forceLeave​(java.lang.String node)
      GET /v1/agent/force-leave/{node}
      Agent getAgent()
      Retrieves the Agent's configuration and member information.
      java.util.Map<java.lang.String,​HealthCheck> getChecks()
      Retrieves all checks registered with the Agent.
      ClientConfig getConfig()  
      ClientEventHandler getEventHandler()  
      java.util.List<Member> getMembers()
      Retrieves all members that the Agent can see in the gossip pool.
      ConsulResponse<FullService> getService​(java.lang.String id, QueryOptions queryOptions)
      Retrieves all information about a service.
      void getService​(java.lang.String id, QueryOptions queryOptions, ConsulResponseCallback<FullService> callback)
      Retrieves all information about a service.
      java.util.Map<java.lang.String,​Service> getServices()
      Retrieves all services registered with the Agent.
      boolean isRegistered​(java.lang.String serviceId)
      Indicates whether or not a particular service is registered with the local Consul agent.
      boolean join​(java.lang.String address)
      GET /v1/agent/join/{address} Instructs the agent to join a node.
      boolean join​(java.lang.String address, boolean wan)
      GET /v1/agent/join/{address}?wan=1 Instructs the agent to join a node.
      void pass​(java.lang.String serviceId)
      Sets a TTL service check to "passing" state
      void pass​(java.lang.String serviceId, java.lang.String note)
      Sets a TTL service check to "passing" state with a note
      void passCheck​(java.lang.String checkId)
      Sets a TTL check to "passing" state
      void passCheck​(java.lang.String checkId, java.lang.String note)
      Sets a TTL check to "passing" state with a note
      void ping()
      Pings the Consul Agent.
      void register​(int port, long ttl, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with a ttl check.
      void register​(int port, com.google.common.net.HostAndPort tcp, long interval, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with a TCP based check
      void register​(int port, Registration.RegCheck check, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with an existing Registration.RegCheck
      void register​(int port, java.lang.String args, long interval, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with a script based check.
      void register​(int port, java.net.URL http, long interval, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with an http based check
      void register​(int port, java.util.List<Registration.RegCheck> checks, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with multiple checks
      void register​(int port, java.util.List<java.lang.String> args, long interval, java.lang.String name, java.lang.String id, java.util.List<java.lang.String> tags, java.util.Map<java.lang.String,​java.lang.String> meta)
      Registers the client as a service with Consul with a script based check.
      void register​(Registration registration)  
      void register​(Registration registration, QueryOptions options)
      Registers the client as a service with Consul.
      void registerCheck​(Check check)
      Registers a Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, long ttl)
      Registers a Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, long ttl, java.lang.String notes)
      Registers a Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, com.google.common.net.HostAndPort tcp, long interval)
      Registers a TCP Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, com.google.common.net.HostAndPort tcp, long interval, java.lang.String notes)
      Registers a TCP Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, java.lang.String script, long interval)
      Registers a script Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, java.lang.String args, long interval, java.lang.String notes)
      Registers a script Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, java.net.URL http, long interval)
      Registers an HTTP Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, java.net.URL http, long interval, java.lang.String notes)
      Registers a HTTP Health Check with the Agent.
      void registerCheck​(java.lang.String checkId, java.lang.String name, java.util.List<java.lang.String> args, long interval, java.lang.String notes)
      Registers a script Health Check with the Agent.
      void toggleMaintenanceMode​(java.lang.String serviceId, boolean enable)
      Toggles maintenance mode for a service ID.
      void toggleMaintenanceMode​(java.lang.String serviceId, boolean enable, java.lang.String reason)
      Toggles maintenance mode for a service ID.
      void warn​(java.lang.String serviceId)
      Sets a TTL service check to "warning" state.
      void warn​(java.lang.String serviceId, java.lang.String note)
      Sets a TTL service check to "warning" state with a note.
      void warnCheck​(java.lang.String checkId)
      Sets a TTL check to "warning" state.
      void warnCheck​(java.lang.String checkId, java.lang.String note)
      Sets a TTL check to "warning" state with a note.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • http

        protected final Http http
    • Method Detail

      • isRegistered

        public boolean isRegistered​(java.lang.String serviceId)
        Indicates whether or not a particular service is registered with the local Consul agent.
        Returns:
        true if a particular service is registered with the local Consul agent, otherwise false.
      • ping

        public void ping()
        Pings the Consul Agent.
      • register

        public void register​(int port,
                             long ttl,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with a ttl check.
        Parameters:
        port - The public facing port of the service to register with Consul.
        ttl - Time to live in seconds for the Consul dead man's switch.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(int port,
                             java.lang.String args,
                             long interval,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with a script based check.
        Parameters:
        port - The public facing port of the service to register with Consul.
        args - Specifies command argument to run to update the status of the check..
        interval - Health script run interval in seconds.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(int port,
                             java.util.List<java.lang.String> args,
                             long interval,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with a script based check.
        Parameters:
        port - The public facing port of the service to register with Consul.
        args - Specifies command argument to run to update the status of the check..
        interval - Health script run interval in seconds.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(int port,
                             java.net.URL http,
                             long interval,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with an http based check
        Parameters:
        port - The public facing port of the service to register with Consul.
        http - Health check URL.
        interval - Health script run interval in seconds.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(int port,
                             com.google.common.net.HostAndPort tcp,
                             long interval,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with a TCP based check
        Parameters:
        port - The public facing port of the service to register with Consul.
        tcp - Health check TCP host and port.
        interval - Health script run interval in seconds.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(int port,
                             Registration.RegCheck check,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with an existing Registration.RegCheck
        Parameters:
        port - The public facing port of the service to register with Consul.
        check - The health check to run periodically. Can be null.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(int port,
                             java.util.List<Registration.RegCheck> checks,
                             java.lang.String name,
                             java.lang.String id,
                             java.util.List<java.lang.String> tags,
                             java.util.Map<java.lang.String,​java.lang.String> meta)
        Registers the client as a service with Consul with multiple checks
        Parameters:
        port - The public facing port of the service to register with Consul.
        checks - The health checks to run periodically.
        name - Service name to register.
        id - Service id to register.
        tags - Tags to register with.
        meta - Meta to register with.
      • register

        public void register​(Registration registration,
                             QueryOptions options)
        Registers the client as a service with Consul. Registration enables the use of checks.
        Parameters:
        registration - The registration payload.
        options - An optional QueryOptions instance.
      • register

        public void register​(Registration registration)
      • deregister

        public void deregister​(java.lang.String serviceId,
                               QueryOptions options)
        De-register a particular service from the Consul Agent.
      • deregister

        public void deregister​(java.lang.String serviceId)
        De-register a particular service from the Consul Agent.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  java.lang.String script,
                                  long interval)
        Registers a script Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        script - Health script for Consul to use.
        interval - Health script run interval in seconds.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  java.net.URL http,
                                  long interval)
        Registers an HTTP Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        http - Health check URL.
        interval - Health script run interval in seconds.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  com.google.common.net.HostAndPort tcp,
                                  long interval)
        Registers a TCP Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        tcp - Health check TCP host and port.
        interval - Health script run interval in seconds.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  java.util.List<java.lang.String> args,
                                  long interval,
                                  java.lang.String notes)
        Registers a script Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        args - Health script for Consul to use.
        interval - Health script run interval in seconds.
        notes - Human readable notes. Not used by Consul.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  java.lang.String args,
                                  long interval,
                                  java.lang.String notes)
        Registers a script Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        args - Specifies command argument to run to update the status of the check.
        interval - Health script run interval in seconds.
        notes - Human readable notes. Not used by Consul.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  java.net.URL http,
                                  long interval,
                                  java.lang.String notes)
        Registers a HTTP Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        http - Health check URL.
        interval - Health script run interval in seconds.
        notes - Human readable notes. Not used by Consul.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  com.google.common.net.HostAndPort tcp,
                                  long interval,
                                  java.lang.String notes)
        Registers a TCP Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        tcp - Health check TCP host and port.
        interval - Health script run interval in seconds.
        notes - Human readable notes. Not used by Consul.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  long ttl)
        Registers a Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        ttl - Time to live for the Consul dead man's switch.
      • registerCheck

        public void registerCheck​(java.lang.String checkId,
                                  java.lang.String name,
                                  long ttl,
                                  java.lang.String notes)
        Registers a Health Check with the Agent.
        Parameters:
        checkId - The Check ID to use. Must be unique for the Agent.
        name - The Check Name.
        ttl - Time to live for the Consul dead man's switch.
        notes - Human readable notes. Not used by Consul.
      • registerCheck

        public void registerCheck​(Check check)
        Registers a Health Check with the Agent.
        Parameters:
        check - The Check to register.
      • deregisterCheck

        public void deregisterCheck​(java.lang.String checkId)
        De-registers a Health Check with the Agent
        Parameters:
        checkId - the id of the Check to deregister
      • getAgent

        public Agent getAgent()
        Retrieves the Agent's configuration and member information.

        GET /v1/agent/self

        Returns:
        The Agent information.
      • getChecks

        public java.util.Map<java.lang.String,​HealthCheck> getChecks()
        Retrieves all checks registered with the Agent.

        GET /v1/agent/checks

        Returns:
        Map of Check ID to Checks.
      • getServices

        public java.util.Map<java.lang.String,​Service> getServices()
        Retrieves all services registered with the Agent.

        GET /v1/agent/services

        Returns:
        Map of Service ID to Services.
      • getService

        public void getService​(java.lang.String id,
                               QueryOptions queryOptions,
                               ConsulResponseCallback<FullService> callback)
        Retrieves all information about a service.

        GET /v1/agent/service/:service_id

        Parameters:
        id - The service id.
        queryOptions - The Query Options to use.
        callback - Callback implemented by callee to handle results.
      • getMembers

        public java.util.List<Member> getMembers()
        Retrieves all members that the Agent can see in the gossip pool.

        GET /v1/agent/members

        Returns:
        List of Members.
      • forceLeave

        public void forceLeave​(java.lang.String node)
        GET /v1/agent/force-leave/{node}

        Instructs the agent to force a node into the "left" state.

        Parameters:
        node -
      • check

        public void check​(java.lang.String checkId,
                          State state,
                          java.lang.String note)
                   throws NotRegisteredException
        Checks in with Consul.
        Parameters:
        checkId - The Check ID to check in.
        state - The current state of the Check.
        note - Any note to associate with the Check.
        Throws:
        NotRegisteredException
      • checkTtl

        public void checkTtl​(java.lang.String serviceId,
                             State state,
                             java.lang.String note)
                      throws NotRegisteredException
        Prepends the default TTL prefix to the serviceId to produce a check id, then delegates to check(String checkId, State state, String note) This method only works with TTL checks that have not been given a custom name.
        Throws:
        NotRegisteredException
      • join

        public boolean join​(java.lang.String address)
        GET /v1/agent/join/{address} Instructs the agent to join a node.
        Parameters:
        address - The address to join.
        Returns:
        true if successful, otherwise false.
      • join

        public boolean join​(java.lang.String address,
                            boolean wan)
        GET /v1/agent/join/{address}?wan=1 Instructs the agent to join a node.
        Parameters:
        address - The address to join.
        wan - Use WAN pool.
        Returns:
        true if successful, otherwise false.
      • toggleMaintenanceMode

        public void toggleMaintenanceMode​(java.lang.String serviceId,
                                          boolean enable)
        Toggles maintenance mode for a service ID.
        Parameters:
        serviceId - The service ID.
        enable - true if the service should be in maintenance mode, otherwise false.
      • toggleMaintenanceMode

        public void toggleMaintenanceMode​(java.lang.String serviceId,
                                          boolean enable,
                                          java.lang.String reason)
        Toggles maintenance mode for a service ID.
        Parameters:
        serviceId - The service ID.
        enable - true if the service should be in maintenance mode, otherwise false.
        reason - The reason for maintenance mode.