本文介绍 Redis Go SDK 的代码示例供您参考。
使用 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})
说明
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance。
acl-cq014ioqkm9zt5j****
和 acl-cq015pp15v8buvl****
两个白名单department:game
和 owner:Alice
两个标签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: ®ionId, 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) }
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: ®ionId, 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) }
说明
关于下述 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) }