RPC Protocol

The RPC Protocol is deprecated and support was removed in Consul 0.8. Please use the HTTP API, which has support for all features of the RPC Protocol.

The Consul agent provides a complete RPC mechanism that can be used to control the agent programmatically. This RPC mechanism is the same one used by the CLI but can be used by other applications to easily leverage the power of Consul without directly embedding.

It is important to note that the RPC protocol does not support all the same operations as the HTTP API.

Implementation Details

The RPC protocol is implemented using MsgPack over TCP. This choice was driven by the fact that all operating systems support TCP, and MsgPack provides a fast serialization format that is broadly available across languages.

All RPC requests have a request header, and some requests have a request body. The request header looks like:

  1. {
  2. "Command": "Handshake",
  3. "Seq": 0
  4. }
  1. {
  2. "Command": "Handshake",
  3. "Seq": 0
  4. }

All responses have a response header, and some may contain a response body. The response header looks like:

  1. {
  2. "Seq": 0,
  3. "Error": ""
  4. }
  1. {
  2. "Seq": 0,
  3. "Error": ""
  4. }

The Command in the request is used to specify what command the server should run, and the Seq is used to track the request. Responses are tagged with the same Seq as the request. This allows for some concurrency on the server side as requests are not purely FIFO. Thus, the Seq value should not be re-used between commands. All responses may be accompanied by an error.

Possible commands include:

  • handshake - Initializes the connection and sets the version
  • force-leave - Removes a failed node from the cluster
  • join - Requests Consul join another node
  • members-lan - Returns the list of LAN members
  • members-wan - Returns the list of WAN members
  • monitor - Starts streaming logs over the connection
  • stop - Stops streaming logs
  • leave - Instructs the Consul agent to perform a graceful leave and shutdown
  • stats - Provides various debugging statistics
  • reload - Triggers a configuration reload

Each command is documented below along with any request or response body that is applicable.

handshake

This command is used to initialize an RPC connection. As it informs the server which version the client is using, handshake MUST be the first command sent.

The request header must be followed by a handshake body, like:

  1. {
  2. "Version": 1
  3. }
  1. {
  2. "Version": 1
  3. }

The body specifies the IPC version being used; however, only version 1 is currently supported. This is to ensure backwards compatibility in the future.

There is no special response body, but the client should wait for the response and check for an error.

force-leave

This command is used to remove failed nodes from a cluster. It takes the following body:

  1. {
  2. "Node": "failed-node-name"
  3. }
  1. {
  2. "Node": "failed-node-name"
  3. }

There is no special response body.

join

This command is used to join an existing cluster using one or more known nodes. It takes the following body:

  1. {
  2. "Existing": [
  3. "192.168.0.1:6000",
  4. "192.168.0.2:6000"
  5. ],
  6. "WAN": false
  7. }
  1. {
  2. "Existing": [
  3. "192.168.0.1:6000",
  4. "192.168.0.2:6000"
  5. ],
  6. "WAN": false
  7. }

The Existing nodes are each contacted, and WAN controls if we are adding a WAN member or LAN member. LAN members are expected to be in the same datacenter and should be accessible at relatively low latencies. WAN members are expected to be operating in different datacenters with relatively high access latencies. It is important that only agents running in “server” mode are able to join nodes over the WAN.

The response contains both a header and body. The body looks like:

  1. {
  2. "Num": 2
  3. }
  1. {
  2. "Num": 2
  3. }

‘Num’ indicates the number of nodes successfully joined.

members-lan

This command is used to return all the known LAN members and associated information. All agents will respond to this command.

There is no request body, but the response looks like:

  1. {
  2. "Members": [
  3. {
  4. "Name": "TestNode"
  5. "Addr": [127, 0, 0, 1],
  6. "Port": 5000,
  7. "Tags": {
  8. "role": "test"
  9. },
  10. "Status": "alive",
  11. "ProtocolMin": 0,
  12. "ProtocolMax": 3,
  13. "ProtocolCur": 2,
  14. "DelegateMin": 0,
  15. "DelegateMax": 1,
  16. "DelegateCur": 1,
  17. },
  18. ...
  19. ]
  20. }
  1. {
  2. "Members": [
  3. {
  4. "Name": "TestNode"
  5. "Addr": [127, 0, 0, 1],
  6. "Port": 5000,
  7. "Tags": {
  8. "role": "test"
  9. },
  10. "Status": "alive",
  11. "ProtocolMin": 0,
  12. "ProtocolMax": 3,
  13. "ProtocolCur": 2,
  14. "DelegateMin": 0,
  15. "DelegateMax": 1,
  16. "DelegateCur": 1,
  17. },
  18. ...
  19. ]
  20. }

members-wan

This command is used to return all the known WAN members and associated information. Only agents in server mode will respond to this command.

There is no request body, and the response is the same as members-lan

monitor

The monitor command subscribes the channel to log messages from the Agent.

The request looks like:

  1. {
  2. "LogLevel": "DEBUG"
  3. }
  1. {
  2. "LogLevel": "DEBUG"
  3. }

This subscribes the client to all messages of at least DEBUG level.

The server will respond with a standard response header indicating if the monitor was successful. If so, any future logs will be sent and tagged with the same Seq as in the monitor request.

Assume we issued the previous monitor command with "Seq": 50. We may start getting messages like:

  1. {
  2. "Seq": 50,
  3. "Error": ""
  4. }
  5. {
  6. "Log": "2013/12/03 13:06:53 [INFO] agent: Received event: member-join"
  7. }
  1. {
  2. "Seq": 50,
  3. "Error": ""
  4. }
  5. {
  6. "Log": "2013/12/03 13:06:53 [INFO] agent: Received event: member-join"
  7. }

It is important to realize that these messages are sent asynchronously and not in response to any command. If a client is streaming commands, there may be logs streamed while a client is waiting for a response to a command. This is why the Seq must be used to pair requests with their corresponding responses.

The client can only be subscribed to at most a single monitor instance. To stop streaming, the stop command is used.

stop

This command stops a monitor.

The request looks like:

  1. {
  2. "Stop": 50
  3. }
  1. {
  2. "Stop": 50
  3. }

This unsubscribes the client from the monitor with Seq value of 50.

There is no response body.

leave

This command is used to trigger a graceful leave and shutdown. There is no request body or response body.

stats

This command provides debug information. There is no request body, and the response body looks like:

  1. {
  2. "agent": {
  3. "check_monitors": 0,
  4. ...
  5. },
  6. "consul: {
  7. "server": "true",
  8. ...
  9. },
  10. ...
  11. }
  1. {
  2. "agent": {
  3. "check_monitors": 0,
  4. ...
  5. },
  6. "consul: {
  7. "server": "true",
  8. ...
  9. },
  10. ...
  11. }

reload

This command is used to trigger a reload of configurations. There is no request body or response body.