dubbo.apache.org/dubbo-go/v3@v3.1.1/config_center/nacos/listener.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package nacos
    19  
    20  import (
    21  	"context"
    22  )
    23  
    24  import (
    25  	"github.com/dubbogo/gost/log/logger"
    26  
    27  	constant2 "github.com/nacos-group/nacos-sdk-go/v2/common/constant"
    28  	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  	"dubbo.apache.org/dubbo-go/v3/config_center"
    34  	"dubbo.apache.org/dubbo-go/v3/metrics"
    35  	metricsConfigCenter "dubbo.apache.org/dubbo-go/v3/metrics/config_center"
    36  	"dubbo.apache.org/dubbo-go/v3/remoting"
    37  )
    38  
    39  func callback(listener config_center.ConfigurationListener, _, group, dataId, data string) {
    40  	listener.Process(&config_center.ConfigChangeEvent{Key: dataId, Value: data, ConfigType: remoting.EventTypeUpdate})
    41  	metrics.Publish(metricsConfigCenter.NewIncMetricEvent(dataId, group, remoting.EventTypeUpdate, metricsConfigCenter.Nacos))
    42  }
    43  
    44  func (n *nacosDynamicConfiguration) addListener(key string, listener config_center.ConfigurationListener) {
    45  	_, loaded := n.keyListeners.Load(key)
    46  	if !loaded {
    47  		err := n.client.Client().ListenConfig(vo.ConfigParam{
    48  			DataId: key,
    49  			Group:  n.resolvedGroup(n.url.GetParam(constant.NacosGroupKey, constant2.DEFAULT_GROUP)),
    50  			OnChange: func(namespace, group, dataId, data string) {
    51  				go callback(listener, namespace, group, dataId, data)
    52  			},
    53  		})
    54  		if err != nil {
    55  			logger.Errorf("nacos : listen config fail, error:%v ", err)
    56  			return
    57  		}
    58  		_, cancel := context.WithCancel(context.Background())
    59  		newListener := make(map[config_center.ConfigurationListener]context.CancelFunc)
    60  		newListener[listener] = cancel
    61  		n.keyListeners.Store(key, newListener)
    62  		return
    63  	}
    64  
    65  	// TODO check goroutine alive, but this version of go_nacos_sdk is not support.
    66  	logger.Infof("profile:%s. this profile is already listening", key)
    67  }
    68  
    69  func (n *nacosDynamicConfiguration) removeListener(key string, listener config_center.ConfigurationListener) {
    70  	// TODO: not supported in current go_nacos_sdk version
    71  	logger.Warn("not supported in current go_nacos_sdk version")
    72  }