Connecting
Use the Aerospike Go client to connect and periodically ping nodes for cluster status by creating a Client object to specify the IP address and port of one or more cluster seed nodes.
Single Seed Node
When creating a new Client object, specify the server to connect to using the IP address and port. The client makes initial contact with the specified server, then automatically discovers all other cluster nodes.
To create a new Client object:
import as "github.com/aerospike/aerospike-client-go" client, err := as.NewClient("127.0.0.1", 3000)Multiple Seed Nodes
To connect to any node in the cluster, specify each node in the cluster when creating the client. The client iterates through the array of nodes until it successfully connects to a node, then it discovers the other cluster nodes.
import as "github.com/aerospike/aerospike-client-go" hosts := []*Host { as.NewHost("a.host", 3000), as.NewHost("another.host", 3000), as.NewHost("and.another.host", 3000),} client, err := as.NewClientWithPolicyAndHost(nil, hosts...)The NewClient initializer creates a maintenance goroutine to periodically ping nodes for cluster status. The Client instance is goroutine friendly and can be used concurrently.
Each get/set call is a non-blocking, asynchronous network call to the Aerospike database cluster. Connections are cached with a connection pool for each server node.
TLS secured connection
TLS connections require certificate configuration on both client and server. The TLS connection port is usually set to 4333 instead of the typically unsecured port 3000. See TLS configuration for more information about setting up TLS on your Aerospike Database server.
In the following example, set the credential variables clientPolicy.User and clientPolicy.Password to the correct values for your database configuration.
// Create a certificate pool with the CA certificate serverPool := x509.NewCertPool() if ok := serverPool.AppendCertsFromPEM([]byte(caCertAsString)); !ok { log.Fatalf("unable to parse CA certificate") } // Create a host with the hostname and port host, _ := aerospike.NewHosts(fmt.Sprintf("%s:%s", tlsName, aerospikePort)) // Create a ClientPolicy with the API credentials and TLS configuration clientPolicy := aerospike.NewClientPolicy() clientPolicy.User = credentialID clientPolicy.Password = credentialSecret clientPolicy.TlsConfig = &tls.Config{ ServerName: tlsName, RootCAs: serverPool, } // Create a client with the ClientPolicy and host client, err := aerospike.NewClientWithPolicyAndHost(clientPolicy, host...) if err != nil { log.Fatalf("Failed to connect to Aerospike: %v", err) }Cleaning Up
When all commands complete and the application is ready for a clean shutdown, call the Close() method to free resources held by the Client object. The Client object cannot be used after a Close() call.
client.Close()