Preface
We introduced the overall architecture of etcd earlier. Learning the communication between the client and the etcd server and the internal communication interface of the etcd cluster nodes is very helpful for us to better use and master the etcd components, and it is also a necessary understanding. We will introduce etcd's gRPC communication interface and client practice.
etcd clientv3 client
The example of etcd client clientv3 access will be mainly Go clients, and readers need to prepare a basic development environment.
First, the initialization of etcd clientv3. We establish a connection between the client and the etcd cluster based on the specified etcd node.
cli,err := ({ Endpoints:[]string{"localhost:2379"}, DialTimeout: 5 * , })
As mentioned above, the code instantiates a client, and two parameters are required to be passed in here:
- Endpoints: Multiple node service addresses of etcd, because I am a single-point native test, so I only pass 1.
- DialTimeout: The first connection timeout for creating a client is passed, and 5 seconds is passed here. If the connection is not successful in 5 seconds, err will be returned; it is worth noting that once the client is successfully created, we no longer have to care about the subsequent underlying connection status, and the client will reconnect within it.
etcd client initialization
After solving the package dependencies, we initialize the etcd client. The client initialization code is as follows:
// client_init_test.go package client import ( "context" "fmt" "/etcd/clientv3" "testing" "time" ) // Test client connectionfunc TestEtcdClientInit(t *) { var ( config client * err error ) // Client configuration config = { // Node configuration Endpoints: []string{"localhost:2379"}, DialTimeout: 5 * , } // Create a connection if client, err = (config); err != nil { (err) } else { // Output cluster information ((())) } () }
As shown in the above code, the expected execution result is as follows:
=== RUN TestEtcdClientInit
&{cluster_id:14841639068965178418 member_id:10276657743932975437 raft_term:3 [ID:10276657743932975437 name:"default" peerURLs:"http://localhost:2380" clientURLs:"http://0.0.0.0:2379" ] {} [] 0} <nil>
--- PASS: TestEtcdClientInit (0.08s)
PASS
You can see that clientv3 and the node localhost:2379 of etcd Server successfully established a connection and output cluster information. Now we can operate etcd.
client definition
Next, let’s take a look at the definition of client:
type Client struct { Cluster KV Lease Watcher Auth Maintenance // Username is a user name for authentication. Username string // Password is a password for authentication. Password string }
Note that the exportable module structure fields are displayed here, representing several core modules that the client can use. The specific functions are as follows:
- Cluster: Adding etcd server nodes to the cluster is an administrator operation.
- KV: The main function we use is to operate K-V.
- Lease: Lease-related operations, such as applying for a lease with TTL=10 seconds.
- Watcher: Watch subscriptions to listen for the latest data changes.
- Auth: Manage etcd users and permissions, which are an administrator operation.
- Maintenance: Maintenance etcd, such as actively migrating the leader node of etcd, is an administrator operation
The above is the detailed content of the core methods of the etcd communication interface client API. For more information about the etcd communication interface client API, please pay attention to my other related articles!