文档

使用Go SDK管理事件库EventStore

更新时间:

事件库(EventStore)是日志服务中事件数据的采集、存储和查询单元。每个EventStore隶属于一个Project,每个Project中可创建多个EventStore。本文通过代码示例介绍如何创建、修改、查询、删除EventStore等。

前提条件

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见在Linux、macOS和Windows系统配置环境变量

    重要
    • 阿里云账号的AccessKey拥有所有API的访问权限,建议您使用RAM用户的AccessKey进行API访问或日常运维。

    • 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • 已安装0.1.49及以上版本的Go SDK。更多信息,请参见安装Go SDK

注意事项

本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您希望通过与Project同地域的其他阿里云产品访问日志服务,请使用格式为https://cn-hangzhou-intranet.log.aliyuncs.com的私网Endpoint。关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口

示例代码

本示例中,通过调用阿里云日志服务Go SDK中的相关API实现对事件库(EventStore)的管理,包括创建、更新、列出、获取和删除操作。

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/aliyun/aliyun-log-go-sdk"
)

var (
  //日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
	endpoint       = "cn-hangzhou.log.aliyuncs.com"
  //日志服务的Project名称。
	project        = "ali-test-project"
  //日志服务的事件库(EventStore)名称。
	eventStoreName = "test-events"
  
  // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
	client         = sls.CreateNormalInterface(
		endpoint,
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
		os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
		"",
	)
)

// 创建事件库(EventStore)。
func createEventStore() {
	eventStore := &sls.LogStore{
		Name:          eventStoreName,
		TTL:           30,
		ShardCount:    2,
		AutoSplit:     true,
		MaxSplitShard: 64,
	}
	if err := client.CreateEventStore(project, eventStore); err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("Create eventStore %s succcess", eventStoreName))
}

// 更新事件库(EventStore)。
func updateEventStore() {
	eventStore := &sls.LogStore{
		Name:       eventStoreName,
		TTL:        60,
		ShardCount: 2,
		AutoSplit:  false,
	}
  
  // 调用客户端的UpdateEventStore方法更新事件库。
	if err := client.UpdateEventStore(project, eventStore); err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("Update eventStore %s succcess", eventStoreName))
}

// 列出所有事件库(EventStore)。
func listEventStores() {
	// 调用客户端的ListEventStore方法列出所有事件库。
  eventStores, err := client.ListEventStore(project, 0, 10)
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("List eventStores: %s", strings.Join(eventStores, ",")))
}

// 获取指定事件库(EventStore)的详细信息。
func getEventStore() {
  // 调用客户端的GetEventStore方法获取事件库的详细信息。
	eventStore, err := client.GetEventStore(project, eventStoreName)
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("Get eventStore %s success", eventStore.Name))
}

// 删除事件库(EventStore)。
func deleteEventStore() {
  // 调用客户端的DeleteEventStore方法删除事件库。
	if err := client.DeleteEventStore(project, eventStoreName); err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("Delete eventStore %s success", eventStoreName))
}

func main() {
	createEventStore()
	updateEventStore()
	listEventStores()
	getEventStore()
	deleteEventStore()
}

  • 本页导读 (1)
文档反馈