本文介绍如何快速使用 Volcengine Go SDK 实现基础的 Kafka 实例资源管理流程,包括创建实例、创建 Topic、查看实例等操作。
本文档以消息队列 Kafka 版的基本资源创建流程为例,演示使用 Volcengine Go SDK 实现相关功能的方式。
通过 Volcengine Go SDK 调用消息队列 Kafka版 V2 API CreateInstance 的示例代码如下。
package main import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/kafka" "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() { ak, sk, region := "Your AK", "Your SK", "cn-beijing" config := volcengine.NewConfig(). WithRegion(region). WithCredentials(credentials.NewStaticCredentials(ak, sk, "")) sess, err := session.NewSession(config) if err != nil { panic(err) } svc := kafka.New(sess) resp, err := svc.CreateInstance(&kafka.CreateInstanceInput{ ChargeInfo: &kafka.ChargeInfoForCreateInstanceInput{ AutoRenew: volcengine.Bool(true), ChargeType: volcengine.String("PrePaid"), Period: volcengine.Int32(1), PeriodUnit: volcengine.String("Month"), }, ComputeSpec: volcengine.String("kafka.20xrate.hw"), SubnetId: volcengine.String("subnet-rrps5hvr1bswv0x58fp****"), UserName: volcengine.String("kafka2001"), UserPassword: volcengine.String("Test@123456"), Version: volcengine.String("2.2.2"), VpcId: volcengine.String("vpc-rs4yccs57e9sv0x57bf****"), ZoneId: volcengine.String("cn-beijing-a"), }) if err != nil { panic(err) } fmt.Println(resp) }
通过 Volcengine Go SDK 调用消息队列 Kafka版 V2 API DescribeInstances 的示例代码如下。
package main import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/kafka" "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() { ak, sk, region := "Your AK", "Your SK", "cn-beijing" config := volcengine.NewConfig(). WithRegion(region). WithCredentials(credentials.NewStaticCredentials(ak, sk, "")) sess, err := session.NewSession(config) if err != nil { panic(err) } svc := kafka.New(sess) resp, err := svc.DescribeInstances(&kafka.DescribeInstancesInput{ PageNumber: volcengine.Int32(1), PageSize: volcengine.Int32(10), }) if err != nil { panic(err) } fmt.Println(resp) }
通过 Volcengine Go SDK 调用消息队列 Kafka版 V2 API CreateTopic 的示例代码如下。
package main import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/kafka" "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() { ak, sk, region := "Your AK", "Your SK", "cn-beijing" config := volcengine.NewConfig(). WithRegion(region). WithCredentials(credentials.NewStaticCredentials(ak, sk, "")) sess, err := session.NewSession(config) if err != nil { panic(err) } svc := kafka.New(sess) resp, err := svc.CreateTopic(&kafka.CreateTopicInput{ AccessPolicies: []*kafka.AccessPolicyForCreateTopicInput{ { AccessPolicy: volcengine.String("PubSub"), UserName: volcengine.String("user123"), }, }, AllAuthority: volcengine.Bool(false), Description: volcengine.String("describe"), InstanceId: volcengine.String("kafka-cnngbnntswg1****"), Parameters: volcengine.String("{\"LogRetentionHours\":\"72\",\"MessageMaxByte\":\"10\",\"MinInsyncReplicaNumber\":\"2\"}"), PartitionNumber: volcengine.Int32(3), ReplicaNumber: volcengine.Int32(3), TopicName: volcengine.String("mytopic123"), }) if err != nil { panic(err) } fmt.Println(resp) }