mutezebra commented on code in PR #647:
URL: https://github.com/apache/dubbo-go-pixiu/pull/647#discussion_r1913195363
##########
configcenter/nacos_load.go:
##########
@@ -81,56 +99,61 @@ func NewNacosConfig(boot *model.Bootstrap) (configClient
ConfigClient, err error
LogLevel: boot.Nacos.ClientConfig.LogLevel,
}
- pa := vo.NacosClientParam{
- ClientConfig: &cc,
- ServerConfigs: sc,
- }
- nacos, err := clients.NewConfigClient(pa)
- if err != nil {
- return nil, err
- }
- configClient = &NacosConfig{
- client: nacos,
+ clientParam := vo.NacosClientParam{
+ ClientConfig: &clientConfig,
+ ServerConfigs: serverConfigs,
}
- return configClient, nil
+ return clients.NewConfigClient(clientParam)
}
+// LoadConfig retrieves the configuration from Nacos based on the provided
parameters.
func (n *NacosConfig) LoadConfig(param map[string]interface{}) (string, error)
{
return n.client.GetConfig(vo.ConfigParam{
DataId: getOrDefault(param[KeyDataId].(string), DataId),
Group: getOrDefault(param[KeyGroup].(string), Group),
})
}
-func getOrDefault(target string, quiet string) string {
+// getOrDefault returns the target value if it is not empty; otherwise, it
returns the fallback value.
+func getOrDefault(target, fallback string) string {
if len(target) == 0 {
- target = quiet
+ return fallback
}
return target
}
-func (n *NacosConfig) ListenConfig(param map[string]interface{}) (err error) {
- // todo noop, not support
- if true {
- return nil
- }
- listen := n.listen(getOrDefault(param[KeyDataId].(string), DataId),
getOrDefault(param[KeyGroup].(string), Group))
- return listen()
+// ListenConfig listens for configuration changes in Nacos.
+func (n *NacosConfig) ListenConfig(param map[string]interface{}) error {
+ return n.client.ListenConfig(vo.ConfigParam{
+ DataId: getOrDefault(param[KeyDataId].(string), DataId),
+ Group: getOrDefault(param[KeyGroup].(string), Group),
+ OnChange: n.onChange,
+ })
}
-func (n *NacosConfig) listen(dataId, group string) func() error {
- return func() error {
- return n.client.ListenConfig(vo.ConfigParam{
- DataId: dataId,
- Group: group,
- OnChange: func(namespace, group, dataId, data string) {
- if len(data) == 0 {
- logger.Errorf("nacos listen callback
data nil error , namespace : %s,group : %s , dataId : %s , data : %s")
- return
- }
- n.listenConfigCallback(data)
- },
- })
+// onChange is the callback function triggered when the configuration changes
in Nacos.
+func (n *NacosConfig) onChange(namespace, group, dataId, data string) {
+ n.mu.Lock()
+ defer n.mu.Unlock()
+
+ if len(data) == 0 {
Review Comment:
收到
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]