dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/base_configuration_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 registry
    19  
    20  import (
    21  	"github.com/dubbogo/gost/log/logger"
    22  
    23  	perrors "github.com/pkg/errors"
    24  )
    25  
    26  import (
    27  	"dubbo.apache.org/dubbo-go/v3/common"
    28  	"dubbo.apache.org/dubbo-go/v3/common/config"
    29  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    30  	"dubbo.apache.org/dubbo-go/v3/config_center"
    31  	"dubbo.apache.org/dubbo-go/v3/remoting"
    32  )
    33  
    34  // nolint
    35  type BaseConfigurationListener struct {
    36  	configurators           []config_center.Configurator
    37  	dynamicConfiguration    config_center.DynamicConfiguration
    38  	defaultConfiguratorFunc func(url *common.URL) config_center.Configurator
    39  }
    40  
    41  // Configurators gets Configurator from config center
    42  func (bcl *BaseConfigurationListener) Configurators() []config_center.Configurator {
    43  	return bcl.configurators
    44  }
    45  
    46  // InitWith will init BaseConfigurationListener by @key+@Listener+@f
    47  func (bcl *BaseConfigurationListener) InitWith(key string, listener config_center.ConfigurationListener,
    48  	f func(url *common.URL) config_center.Configurator) {
    49  
    50  	bcl.dynamicConfiguration = config.GetEnvInstance().GetDynamicConfiguration()
    51  	if bcl.dynamicConfiguration == nil {
    52  		//set configurators to empty
    53  		bcl.configurators = []config_center.Configurator{}
    54  		return
    55  	}
    56  	bcl.defaultConfiguratorFunc = f
    57  	bcl.dynamicConfiguration.AddListener(key, listener)
    58  	if rawConfig, err := bcl.dynamicConfiguration.GetRule(key,
    59  		config_center.WithGroup(constant.Dubbo)); err != nil {
    60  		//set configurators to empty
    61  		bcl.configurators = []config_center.Configurator{}
    62  		return
    63  	} else if len(rawConfig) > 0 {
    64  		if err := bcl.genConfiguratorFromRawRule(rawConfig); err != nil {
    65  			logger.Error("bcl.genConfiguratorFromRawRule(rawConfig:%v) = error:%v", rawConfig, err)
    66  		}
    67  	}
    68  }
    69  
    70  // Process the notification event once there's any change happens on the config.
    71  func (bcl *BaseConfigurationListener) Process(event *config_center.ConfigChangeEvent) {
    72  	logger.Infof("Notification of overriding rule, change type is: %v , raw config content is:%v", event.ConfigType, event.Value)
    73  	if event.ConfigType == remoting.EventTypeDel {
    74  		bcl.configurators = nil
    75  	} else {
    76  		if err := bcl.genConfiguratorFromRawRule(event.Value.(string)); err != nil {
    77  			logger.Error(perrors.WithStack(err))
    78  		}
    79  	}
    80  }
    81  
    82  func (bcl *BaseConfigurationListener) genConfiguratorFromRawRule(rawConfig string) error {
    83  	urls, err := bcl.dynamicConfiguration.Parser().ParseToUrls(rawConfig)
    84  	if err != nil {
    85  		return perrors.WithMessage(err, "Failed to parse raw dynamic config and it will not take effect, the raw config is: "+
    86  			rawConfig)
    87  	}
    88  	bcl.configurators = ToConfigurators(urls, bcl.defaultConfiguratorFunc)
    89  	return nil
    90  }
    91  
    92  // OverrideUrl gets existing configuration rule and overrides provider url before exporting.
    93  func (bcl *BaseConfigurationListener) OverrideUrl(url *common.URL) {
    94  	for _, v := range bcl.configurators {
    95  		v.Configure(url)
    96  	}
    97  }
    98  
    99  // ToConfigurators converts @urls by @f to config_center.Configurators
   100  func ToConfigurators(urls []*common.URL, f func(url *common.URL) config_center.Configurator) []config_center.Configurator {
   101  	if len(urls) == 0 {
   102  		return nil
   103  	}
   104  	var configurators []config_center.Configurator
   105  	for _, url := range urls {
   106  		if url.Protocol == constant.EmptyProtocol {
   107  			configurators = []config_center.Configurator{}
   108  			break
   109  		}
   110  
   111  		override := url.GetParams()
   112  		delete(override, constant.AnyhostKey)
   113  		if len(override) == 0 {
   114  			continue
   115  		}
   116  		configurators = append(configurators, f(url))
   117  	}
   118  	return configurators
   119  }