This topic describes how to use ListenConfig to listen to configuration changes in Nacos and obtain the latest values of configuration items.
Description
You can call the following operation to listen to configuration changes in Nacos.
ListenConfig(params vo.ConfigParam) (err error)
Request parameters
Parameter | Type | Description |
---|---|---|
ConfigParam.OnChange | Function | The callback function used after the configuration is changed. |
Examples
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"
//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)
},
})
}