Updated on 2024-05-20 GMT+08:00

Connecting to an Instance Using Go

This section describes how to use Go to access a GeminiDB Redis instance.

Prerequisites

  • A GeminiDB Redis instance has been created and is in the Available status.
  • An ECS is available. For details, see Purchasing an ECS.
  • The GNU Compiler Collection (GCC) has been installed on the ECS.
  • The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.

Procedure

  1. Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Use the Golang client to connect to the instance. The following uses the go-redis SDK as an example.

    • Go-redis download address: https://github.com/go-redis/redis
    • Connect to the GeminiDB Redis cluster using the single-node API.
      package main
      import (
          "fmt"
          "github.com/go-redis/redis"
          "os"
      )
      func main() {
          // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
          // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
          password = os.Getenv("EXAMPLE_PASSWORD_ENV")
          client := redis.NewClient(&redis.Options{
              Addr:     "xx.xx.xx.xx:8635",  //Enter the load balancer IP address obtained in step 1.
              Password: password, 
              DB:       0,  // Use the default database 0.
          })
          pong, err := client.Ping().Result()
          fmt.Println(pong, err)
          err = client.Set("key1", "value1", 0).Err()
          if err != nil {
              panic(err)
          }
          val, err := client.Get("key1").Result()
          if err != nil {
              panic(err)
          }
          fmt.Println("key1", val)
      }

      The expected output is as follows:

      PONG 
      key1 value1
      • If you use go-redis to connect a GeminiDB Redis instance, use the common mode instead of the cluster mode, as shown in the preceding sample code.
      • In the preceding example, set the GeminiDB Redis address and password based on the site requirements.
    • Connect to the GeminiDB Redis cluster using the cluster API.
      package main
      import (
          "fmt"
          "github.com/go-redis/redis"
      )
      func main() {
      client := redis.NewClusterClient(&redis.ClusterOptions{
               Addrs: []string{ // Enter the load balancer IP address obtained in step 1.
                   " xx.xx.xx.xx:8635",
               },
               Password:     "xx", // Password of the cluster.
           })
          pong, err := client.Ping().Result()
          fmt.Println(pong, err)
      
          err = client.Set("key1", "value1", 0).Err()
          if err != nil {
              panic(err)
          }
          val, err := client.Get("key1").Result()
          if err != nil {
              panic(err)
          }
          fmt.Println("key1", val)
      }