You need to enable JavaScript to run this app.
导航
快速开始
最近更新时间:2023.08.18 19:13:49首次发布时间:2023.08.18 19:13:49

本文介绍如何快速使用 Volcengine Go SDK 实现基础的 Kafka 实例资源管理流程,包括创建实例、创建 Topic、查看实例等操作。

前提条件

  • 已安装 Volcengine Go SDK。更多信息,请参见安装 Go SDK
  • 已创建并获取火山引擎访问密钥 AccessKey。访问密钥 AccessKey 拥有所有 API 的全部权限。建议您通过 IAM 用户进行 API 相关操作和日常运维。使用 IAM 用户前,主账号需要为 IAM 用户授予消息队列 Kafka版相关资源和操作的权限。

示例代码

本文档以消息队列 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)
}

创建 Topic

通过 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)
}