This topic describes how to use the ACM Nacos Go SDK in a Go program to manage configurations in Nacos, including getting, listening to, publishing, and deleting configurations.
Preparations
Common parameters
Parameter | Type | Description |
---|---|---|
Example code
Go
format
package main
import (
"fmt"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"time"
)
func main() {
//Use the values of End Point and Namespace ID on the Namespace Details page.
var endpoint = "${endpoint}"
var namespaceId = "${namespaceId}"
//Use the AccessKey ID and AccessKey secret of the RAM user.
var accessKey = "${accessKey}"
var secretKey = "${secretKey}"
clientConfig := constant.ClientConfig{
//
Endpoint: endpoint + ":8080",
NamespaceId: namespaceId,
AccessKey: accessKey,
SecretKey: secretKey,
TimeoutMs: 5 * 1000,
ListenInterval: 30 * 1000,
}
configClient, err := clients.CreateConfigClient(map[string]interface{}{
"clientConfig": clientConfig,
})
if err ! = nil {
fmt.Println(err)
return
}
var dataId = "com.alibaba.nacos.example.properties"
var group = "DEFAULT_GROUP"
//Publish the configuration.
success, err := configClient.PublishConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
Content: "connectTimeoutInMills=3000"})
if success {
fmt.Println("Publish config successfully.")
}
time.Sleep(3 * time.Second)
//Get the configuration.
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: dataId,
Group: group})
fmt.Println("Get config:" + content)
//Listen to configuration changes.
configClient.ListenConfig(vo.ConfigParam{
DataId: dataId,
Group: group,
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("ListenConfig group:" + group + ", dataId:" + dataId + ", data:" + data)
},
})
//Delete the configuration.
success, err = configClient.DeleteConfig(vo.ConfigParam{
DataId: dataId,
Group: group})
if success {
fmt.Println("Delete config successfully.")
}
}