You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2024.10.28 17:41:13首次发布时间: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 实例,配置如下:
    • 数据库版本号: 5.0
    • 可用内存:8GiB(即 8192MiB)
    • 分片数:2
    • 每个分片中的节点数:3
    • 在华北2(北京)地域下使用多可用区部署,其中主节点部署在可用区 A,2 个从节点均在可用区 B
    • 绑定至 acl-cq014ioqkm9zt5j**** 和 acl-cq015pp15v8buvl**** 两个白名单
    • 绑定 department:game 和 owner:Alice 两个标签
    • 计费类型:包年包月,预计使用 3 个月,且不开启自动续费
    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"
       multiAZ := "enabled"
       aza := "cn-beijing-a"
       azb := "cn-beijing-b"
       azc := "cn-beijing-c"
       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-1g15eukkrvojk8ibuxxg3****"
       subnetId := "subnet-2fe941zmjud4w5oxruvfw****"
       allowListId1 := "acl-cq0118bj1dah84v****"
       allowListId2 := "acl-cq011jlt9rkap8n****"
       key1 := "department"
       value1 := "game"
       key2 := "owner"
       value2 := "Alice"
       chargeType := "PrePaid"
       purchaseMonths := int32(3)
       autoRenew := false
       // 调用 CreateDBInstance 接口创建实例
       resp, err = client.CreateDBInstance(&redis.CreateDBInstanceInput{
           RegionId:       &regionId,
           MultiAZ:  &multiAZ,
           ConfigureNodes: []*redis.ConfigureNodeForCreateDBInstanceInput{
    		       {
    				       AZ: &aza,
    				    },
    				    {
    						    AZ: &azb,
    						},
    						{
    								AZ: &azc,
    						},
    				},
    				InstanceName:   &instanceName,
    				ShardedCluster: &shardedCluster,
    				Password:       &password,
    				NodeNumber:     &nodeNumber,
    				ShardNumber:    &shardNumber,
    				ShardCapacity:  &shardCapacity,
    				EngineVersion:  &engineVersion,
    				VpcId:          &vpcId,
    				SubnetId:       &subnetId,
    				AllowListIds: []*string{
    				&allowListId1,
            &allowListId2,
            },
            Tags: []*redis.TagForCreateDBInstanceInput{
    		        {
    				        Key:   &key1,
    						    Value: &value1,
    						},
    						{
    								Key:   &key2,
    								Value: &value2,
    						},
    				},
    				ChargeType:     &chargeType,
    				PurchaseMonths: &purchaseMonths,
    				AutoRenew:      &autoRenew,
    		})
    
       if err != nil {
          fmt.Printf("Error when calling REDISApi->CreateDBInstance, err: %v\n", err)
          os.Exit(1)
       }
    
       fmt.Println(resp)
    }
    
  • 示例 2
    使用如下示例代码创建 1 个未启用分片集群的 Redis 实例,配置如下:
    • 数据库版本号: 5.0
    • 可用内存:8GiB(即 8192MiB)
    • 节点数:2
    • 在华北2(北京)地域下使用单可用区部署,且所有节点部署在可用区 A
    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"
       multiAZ := "disabled"
       aza := "cn-beijing-a"
       instanceName := "go_sdk_test"
       shardedCluster := int32(0) 
       password := "Pwd@12****"
       nodeNumber := int32(2)
       shardCapacity := int64(8192)
       engineVersion := "5.0"
       vpcId := "vpc-1g15eukkrvojk8ibuxxg3****"
       subnetId := "subnet-2fe941zmjud4w5oxruvfw****"
    
       // 调用 CreateDBInstance 接口创建实例
       resp, err = client.CreateDBInstance(&redis.CreateDBInstanceInput{
          RegionId:       &regionId,
          MultiAZ:  &multiAZ,
          ConfigureNodes: []*redis.ConfigureNodeForCreateDBInstanceInput{
              {
    		          AZ: &aza,
    		      },
    		  },
          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)
}