diff --git a/common/extension/config_post_processor.go b/common/extension/config_post_processor.go new file mode 100644 index 0000000000..db126b744d --- /dev/null +++ b/common/extension/config_post_processor.go @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package extension + +import ( + "github.com/apache/dubbo-go/config/interfaces" +) + +var ( + processors = make(map[string]interfaces.ConfigPostProcessor) +) + +// SetConfigPostProcessor registers a ConfigPostProcessor with the given name. +func SetConfigPostProcessor(name string, processor interfaces.ConfigPostProcessor) { + processors[name] = processor +} + +// GetConfigPostProcessor finds a ConfigPostProcessor by name. +func GetConfigPostProcessor(name string) interfaces.ConfigPostProcessor { + return processors[name] +} + +// GetConfigPostProcessors returns all registered instances of ConfigPostProcessor. +func GetConfigPostProcessors() []interfaces.ConfigPostProcessor { + ret := make([]interfaces.ConfigPostProcessor, 0, len(processors)) + for _, v := range processors { + ret = append(ret, v) + } + return ret +} diff --git a/config/interfaces/config_post_processor.go b/config/interfaces/config_post_processor.go new file mode 100644 index 0000000000..53dd71780f --- /dev/null +++ b/config/interfaces/config_post_processor.go @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package interfaces + +import ( + "github.com/apache/dubbo-go/common" +) + +// ConfigPostProcessor is an extension to give users a chance to customize configs against ReferenceConfig and +// ServiceConfig during deployment time. +type ConfigPostProcessor interface { + // PostProcessReferenceConfig customizes ReferenceConfig's params. + PostProcessReferenceConfig(*common.URL) + + // PostProcessServiceConfig customizes ServiceConfig's params. + PostProcessServiceConfig(*common.URL) +} diff --git a/config/reference_config.go b/config/reference_config.go index 05df347736..d6096e5d90 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -100,6 +100,9 @@ func (c *ReferenceConfig) Refer(_ interface{}) { if c.ForceTag { cfgURL.AddParam(constant.ForceUseTag, "true") } + + c.postProcessConfig(cfgURL) + if c.Url != "" { // 1. user specified URL, could be peer-to-peer address, or register center's address. urlStrings := gxstrings.RegSplit(c.Url, "\\s*[;]+\\s*") @@ -248,3 +251,10 @@ func (c *ReferenceConfig) GenericLoad(id string) { c.Refer(genericService) c.Implement(genericService) } + +// postProcessConfig asks registered ConfigPostProcessor to post-process the current ReferenceConfig. +func (c *ReferenceConfig) postProcessConfig(url *common.URL) { + for _, p := range extension.GetConfigPostProcessors() { + p.PostProcessReferenceConfig(url) + } +} diff --git a/config/service_config.go b/config/service_config.go index 32104f03dd..7efbef708f 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -198,6 +198,8 @@ func (c *ServiceConfig) Export() error { ivkURL.AddParam(constant.Tagkey, c.Tag) } + c.postProcessConfig(ivkURL) + if len(regUrls) > 0 { c.cacheMutex.Lock() if c.cacheProtocol == nil { @@ -332,3 +334,10 @@ func (c *ServiceConfig) GetExportedUrls() []*common.URL { } return nil } + +// postProcessConfig asks registered ConfigPostProcessor to post-process the current ServiceConfig. +func (c *ServiceConfig) postProcessConfig(url *common.URL) { + for _, p := range extension.GetConfigPostProcessors() { + p.PostProcessServiceConfig(url) + } +}