本文介绍如何在 Redis 企业版中使用 Go 语言的 OpenAPI-SDK。
需要使用 Go 1.5 或以上版本,推荐使用 Go 1.12 或以上版本。
说明
您可以执行 go version
命令检查当前 Go 版本信息。
Redis Go SDK 源码地址,请参见 Go SDK。
在目标 Go 项目下,执行如下命令安装 Go SDK。
go get github.com/volcengine/volcengine-go-sdk/volcengine/credentials go get github.com/volcengine/volcengine-go-sdk/volcengine/volcengineutil
使用 Go SDK 调用 Redis 接口时,默认无超时时间设置。 您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: <超时时间> * time.Second})
来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。
例如,您可以通过如下代码将客户端的请求超时时间设置为 10 秒。
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")). WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10time.Second})
Redis Go SDK 支持通过 Session 的方式来初始化客户端。通过 Session 方式创建的 Redis 客户端,底层会实现 HTTP/HTTPS 资源(连接或请求)的复用。当需要初始化多个客户端进行操作时,通过 Session 方式创建可以提升客户端的性能。初始化客户端时,需要带上如下信息:
账号的 Access Key ID(简称 AK)和Secret Access Key(简称 SK)信息。获取 AKSK 信息的方法,请参见查看 AKSK 信息。
Region 信息。Redis 支持的 Region,请参见服务地址。
说明
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateEnterpriseDBInstance。
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.CreateEnterpriseDBInstanceOutput 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) // 设置需要创建实例的配置信息 // 如下代码示例中仅使用了 CreateEnterpriseDBInstance 接口中的必填参数和部分选填参数 regionId := "cn-beijing" zoneId := "cn-beijing-a" instanceName := "go_sdk_shardedcluster_test" password := "Pwd@12****" shardNumber := int32(2) ramPerShard := int32(8192) flashPerShard := int32(10240) vpcId := "vpc-1g15eukkrvojk8ibuxxg*****" subnetId := "subnet-2fe941zmjud4w5oxruvf*****" chargeType := "PrePaid" purchaseMonths := int32(3) autoRenew := false configureNodes := []*redis.ConfigureNodeForCreateEnterpriseDBInstanceInput{ { AZ: &zoneId, }, } // 调用 CreateEnterpriseDBInstance 接口创建实例 resp, err = client.CreateEnterpriseDBInstance(&redis.CreateEnterpriseDBInstanceInput{ RegionId: ®ionId, ConfigureNodes: configureNodes, InstanceName: &instanceName, Password: &password, ShardNumber: &shardNumber, RamPerShard: &ramPerShard, FlashPerShard: &flashPerShard, VpcId: &vpcId, SubnetId: &subnetId, ChargeType: &chargeType, PurchaseMonths: &purchaseMonths, AutoRenew: &autoRenew, }) if err != nil { fmt.Printf("Error when calling REDISApi->CreateEnterpriseDBInstance, err: %v\n", err) os.Exit(1) } fmt.Println(resp) }
说明
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 DescribeEnterpriseDBInstanceDetail。
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.DescribeEnterpriseDBInstanceDetailOutput 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*****" // 调用 DescribeEnterpriseDBInstanceDetail 接口查看实例信息 resp, err = client.DescribeEnterpriseDBInstanceDetail(&redis.DescribeEnterpriseDBInstanceDetailInput{ InstanceId: &instanceId, }) if err != nil { fmt.Printf("Error when calling REDISApi->DescribeEnterpriseDBInstanceDetail, err: %v\n", err) os.Exit(1) } fmt.Println(resp) }