Adding Status.Leader endpoint
This commit is contained in:
parent
662a1d9af7
commit
48e3db305b
|
@ -0,0 +1,58 @@
|
|||
# Consul RPC Endpoints
|
||||
|
||||
Consul provides a few high-level services, each of which exposes
|
||||
methods. The services exposed are:
|
||||
|
||||
* Raft : Used to manipulate Raft from non-leader nodes
|
||||
* Status : Used to query status information
|
||||
* Catalog: Used to register, deregister, and query service information
|
||||
* Health: Used to notify of health checks and changes to health
|
||||
|
||||
## Raft Service
|
||||
|
||||
The Raft service is used to manipulate the Raft controls on the Leader
|
||||
node. It is only for internal use. It exposes the following methods:
|
||||
|
||||
* Apply : Used to execute a command against the FSM
|
||||
* AddPeer: Used to add a peer to the group
|
||||
* RemovePeer: Used to remove a peer from the group
|
||||
|
||||
## Status Service
|
||||
|
||||
The status service is used to query for various status information
|
||||
from the Consul service. It exposes the following methods:
|
||||
|
||||
* Ping : Used to test connectivity
|
||||
* Leader : Used to get the address of the leader
|
||||
|
||||
## Catalog Service
|
||||
|
||||
The catalog service is used to manage service discovery and registration.
|
||||
Nodes can register the services they provide, and deregister them later.
|
||||
The service exposes the following methods:
|
||||
|
||||
* Register : Registers that a node provides a given service
|
||||
* Deregister : Deregisters that a node provides a given service
|
||||
|
||||
* RemoveFailedNode: Used to force remove a failed node
|
||||
|
||||
* ListDatacenters: List the known datacenters
|
||||
* ListServices : Lists the available services
|
||||
* ListNodes : Lists the available nodes
|
||||
* ServiceNodes: Returns the nodes that are part of a service
|
||||
* NodeServices: Returns the services that a node is registered for
|
||||
|
||||
## Health Service
|
||||
|
||||
The health service is used to manage health checking. Nodes have system
|
||||
health checks, as well as application health checks. This service is used to
|
||||
query health information, as well as for nodes to publish changes.
|
||||
|
||||
* CheckPass : Used to inform that a check has passed
|
||||
* CheckWarn : Used to inform that a check is warning
|
||||
* CheckFail : Used to inform that a check has failed
|
||||
* RemoveCheck : Used to remove a health check
|
||||
|
||||
* CheckInState : Gets the checks that in a given state
|
||||
* NodeChecks: Gets the checks a given node has
|
||||
|
|
@ -9,3 +9,14 @@ type Status struct {
|
|||
func (s *Status) Ping(args struct{}, reply *struct{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Leader is used to get the address of the leader
|
||||
func (s *Status) Leader(args struct{}, reply *string) error {
|
||||
leader := s.server.raft.Leader()
|
||||
if leader != nil {
|
||||
*reply = leader.String()
|
||||
} else {
|
||||
*reply = ""
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package consul
|
||||
|
||||
import (
|
||||
"github.com/ugorji/go/codec"
|
||||
"net"
|
||||
"net/rpc"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func rpcClient(t *testing.T, s *Server) *rpc.Client {
|
||||
addr := s.config.RPCAddr
|
||||
conn, err := net.DialTimeout("tcp", addr, time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
// Write the Consul RPC byte to set the mode
|
||||
conn.Write([]byte{byte(rpcConsul)})
|
||||
|
||||
cc := codec.GoRpc.ClientCodec(conn, &codec.MsgpackHandle{})
|
||||
return rpc.NewClientWithCodec(cc)
|
||||
}
|
||||
|
||||
func TestStatusLeader(t *testing.T) {
|
||||
dir1, s1 := testServer(t)
|
||||
defer os.RemoveAll(dir1)
|
||||
defer s1.Shutdown()
|
||||
client := rpcClient(t, s1)
|
||||
defer client.Close()
|
||||
|
||||
arg := struct{}{}
|
||||
var leader string
|
||||
if err := client.Call("Status.Leader", arg, &leader); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if leader != "" {
|
||||
t.Fatalf("unexpected leader: %v", leader)
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
if err := client.Call("Status.Leader", arg, &leader); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if leader == "" {
|
||||
t.Fatalf("no leader")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue