EventBridge provides two Go SDKs:
Management API SDK -- Perform operations on the EventBridge console programmatically.
Data API SDK -- Publish events to an event bus through the
PutEventsoperation.
Prerequisites
Before you begin, make sure that you have:
Go 1.12.0 or later (download)
Verify your Go version:
go versionInstall the SDK
Enable Go modules, configure the Go module proxy, and initialize your project:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go mod initThen install the package for your use case:
| SDK type | Install command |
|---|---|
| Management API | go get github.com/alibabacloud-go/eventbridge-20200401/v2 |
| Data API | go get github.com/alibabacloud-go/eventbridge-sdk |
(Optional) Install the output library for formatted console output:
go get github.com/alibabacloud/tea-consolePublish events with the data API SDK
The data API SDK supports only the PutEvents operation.
Sample code
This example publishes a single event to an event bus:
package main
import (
"fmt"
"os"
eventbridge "github.com/alibabacloud-go/eventbridge-sdk/eventbridge"
)
func main() {
// Configure the client with credentials from environment variables.
config := new(eventbridge.Config).
SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
SetEndpoint("<endpoint>")
client, err := eventbridge.NewClient(config)
if err != nil {
panic(err)
}
// Build a CloudEvent with the required attributes.
event := new(eventbridge.CloudEvent).
SetDatacontenttype("application/json").
SetData([]byte("test")).
SetId("id").
SetSource("source").
SetTime("2020-08-24T13:54:05.965Asia/Shanghai").
SetSubject("1.0").
SetType("type").
SetExtensions(map[string]interface{}{
"aliyuneventbusname": "BusName",
})
// Publish the event.
resp, err := client.PutEvents([]*eventbridge.CloudEvent{event})
if err != nil {
panic(err)
}
fmt.Println(resp)
}Set the following environment variables before you run the code:
| Environment variable | Description |
|---|---|
ALIBABA_CLOUD_ACCESS_KEY_ID | Your AccessKey ID |
ALIBABA_CLOUD_ACCESS_KEY_SECRET | Your AccessKey secret |
In the code, replace the following placeholder values:
| Placeholder | Description | Example |
|---|---|---|
<endpoint> | EventBridge endpoint for your region | <account-id>.eventbridge.<region-id>.aliyuncs.com |
BusName | Name of the target event bus | my-event-bus |
id | Unique identifier for the event | abc-1234 |
source | Event producer identity | myapp.orders |
type | Event type descriptor | order.created |
The sample code uses panic(err) for brevity. In production, implement proper error handling, including retry logic for transient failures.CloudEvent attributes
Each event requires the following attributes:
| Attribute | Method | Description |
|---|---|---|
id | SetId | Unique identifier for the event |
source | SetSource | Event producer identity |
type | SetType | Event type descriptor |
time | SetTime | Timestamp of the event |
datacontenttype | SetDatacontenttype | Media type of the data field (for example, application/json) |
data | SetData | Event payload as a byte slice |
subject | SetSubject | Subject of the event in context of the producer |
extensions | SetExtensions | Custom extension attributes. Use the aliyuneventbusname key to specify the target event bus |
Use the management API SDK
The management API SDK provides programmatic access to EventBridge console operations.
After installation, generate sample code for any management API operation through OpenAPI Explorer. For details, see Automatic generation of SDK examples.