You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2022.10.19 15:16:07首次发布时间:2022.08.08 17:46:46

本文介绍 Redis Go SDK 的代码示例供您参考。

前提条件

  1. 下载安装 SDK
  2. 初始化客户端

请求超时时间设置

使用 Go SDK 调用 Redis 接口时,默认无超时时间设置。
您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: <超时时间> * time.Second}) 来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。
例如,您可以通过如下代码将客户端的请求超时时间设置为 10 秒。

config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).
   WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})

创建 Redis 实例

说明

关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance

  • 示例 1
    使用如下示例代码创建 1 个启用分片集群的 Redis 实例,配置如下:

    • 实例所在地域和可用区:华北2(北京)-可用区A
    • 数据库版本号: 5.0
    • 可用内存:8GiB
    • 分片数:2
    • 每个分片中的节点数:2
    package main
    
    import (
       "fmt"
       "os"
    
       "github.com/volcengine/volcengine-go-sdk/service/redis"
       "github.com/volcengine/volcengine-go-sdk/volcengine"
       "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
       "github.com/volcengine/volcengine-go-sdk/volcengine/session"
    )
    
    func main() {
       var (
          ak     string
          sk     string
          region string
          config *volcengine.Config
          sess   *session.Session
          client *redis.REDIS
          resp   *redis.CreateDBInstanceOutput
          err    error
       )
    
       ak = "Your AK"
       sk = "Your SK"
       region = "Your Region"
       config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).
          WithRegion(region)
    
       sess, err = session.NewSession(config)
       if err != nil {
          fmt.Printf("Failed to create session, err: %v\n", err)
          os.Exit(1)
       }
    
       client = redis.New(sess)
    
       // 设置需要创建实例的配置信息
       regionId := "cn-beijing"
       zoneId := "cn-beijing-a"
       zoneIds := []*string{&zoneId}
       instanceName := "go_sdk_shardedcluster_test"
       shardedCluster := int32(1)
       password := "Pwd@12****"
       nodeNumber := int32(2)
       shardNumber := int32(2)
       shardCapacity := int64(8192)
       engineVersion := "5.0"
       vpcId := "vpc-1g15eukkrvojk8ibuxxg*****"
       subnetId := "subnet-2fe941zmjud4w5oxruvf*****"
    
       // 调用 CreateDBInstance 接口创建实例
       resp, err = client.CreateDBInstance(&redis.CreateDBInstanceInput{
          RegionId:       &regionId,
          ZoneIds:        zoneIds,
          InstanceName:   &instanceName,
          ShardedCluster: &shardedCluster,
          Password:       &password,
          NodeNumber:     &nodeNumber,
          ShardNumber:    &shardNumber,
          ShardCapacity:  &shardCapacity,
          EngineVersion:  &engineVersion,
          VpcId:          &vpcId,
          SubnetId:       &subnetId,
       })
    
       if err != nil {
          fmt.Printf("Error when calling REDISApi->CreateDBInstance, err: %v\n", err)
          os.Exit(1)
       }
    
       fmt.Println(resp)
    }
    
  • 示例 2
    使用如下示例代码创建 1 个未启用分片集群的 Redis 实例,配置如下:

    • 实例所在地域和可用区:华北2(北京)-可用区A
    • 数据库版本号: 5.0
    • 可用内存:8GiB
    • 节点数:2
    package main
    
    import (
       "fmt"
       "os"
    
       "github.com/volcengine/volcengine-go-sdk/service/redis"
       "github.com/volcengine/volcengine-go-sdk/volcengine"
       "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
       "github.com/volcengine/volcengine-go-sdk/volcengine/session"
    )
    
    func main() {
       var (
          ak     string
          sk     string
          region string
          config *volcengine.Config
          sess   *session.Session
          client *redis.REDIS
          resp   *redis.CreateDBInstanceOutput
          err    error
       )
    
       ak = "Your AK"
       sk = "Your SK"
       region = "Your Region"
       config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).
          WithRegion(region)
    
       sess, err = session.NewSession(config)
       if err != nil {
          fmt.Printf("Failed to create session, err: %v\n", err)
          os.Exit(1)
       }
    
       client = redis.New(sess)
    
       // 设置需要创建实例的配置信息
       
       regionId := "cn-beijing"
       zoneId := "cn-beijing-a"
       zoneIds := []*string{&zoneId}
       instanceName := "go_sdk_test"
       shardedCluster := int32(0) 
       password := "Pwd@12****"
       nodeNumber := int32(2)
       shardCapacity := int64(8192)
       engineVersion := "5.0"
       vpcId := "vpc-1g15eukkrvojk8ibuxxg*****"
       subnetId := "subnet-2fe941zmjud4w5oxruvf*****"
    
       // 调用 CreateDBInstance 接口创建实例
       resp, err = client.CreateDBInstance(&redis.CreateDBInstanceInput{
          RegionId:       &regionId,
          ZoneIds:        zoneIds,
          InstanceName:   &instanceName,
          ShardedCluster: &shardedCluster,
          Password:       &password,
          NodeNumber:     &nodeNumber,
          ShardCapacity:  &shardCapacity,
          EngineVersion:  &engineVersion,
          VpcId:          &vpcId,
          SubnetId:       &subnetId,
       })
    
       if err != nil {
          fmt.Printf("Error when calling REDISApi->CreateDBInstance, err: %v\n", err)
          os.Exit(1)
       }
    
       fmt.Println(resp)
    }
    

查询 Redis 实例信息

说明

关于下述 SDK 请求中所使用参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail

package main

import (
   "fmt"
   "os"

   "github.com/volcengine/volcengine-go-sdk/service/redis"
   "github.com/volcengine/volcengine-go-sdk/volcengine"
   "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
   "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
   var (
      ak     string
      sk     string
      region string
      config *volcengine.Config
      sess   *session.Session
      client *redis.REDIS
      resp   *redis.DescribeDBInstanceDetailOutput
      err    error
   )

   ak = "Your AK"
   sk = "Your SK"
   region = "Your Region"
   config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).
      WithRegion(region)

   sess, err = session.NewSession(config)
   if err != nil {
      fmt.Printf("Failed to create session, err: %v\n", err)
      os.Exit(1)
   }
   client = redis.New(sess)

   // 指定要查看的实例 ID
   instanceId := "redis-cn048413rhre*****"

   // 调用 DescribeDBInstanceDetail 接口查看实例信息
   resp, err = client.DescribeDBInstanceDetail(&redis.DescribeDBInstanceDetailInput{
      InstanceId: &instanceId,
   })

   if err != nil {
      fmt.Printf("Error when calling REDISApi->DescribeDBInstanceDetail, err: %v\n", err)
      os.Exit(1)
   }

   fmt.Println(resp)
}