diff --git a/common/extension/config_post_processor.go b/common/extension/config_post_processor.go new file mode 100644 index 0000000000000000000000000000000000000000..db126b744d54562a7f45b59aec26ef6e950a23a8 --- /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/common/proxy/proxy.go b/common/proxy/proxy.go index c91aafecd7b0c21b034365ce92292e1cb82e455d..3a01941700ed277d9481c6c57a739c69f83b06b7 100644 --- a/common/proxy/proxy.go +++ b/common/proxy/proxy.go @@ -234,3 +234,8 @@ func (p *Proxy) Get() common.RPCService { func (p *Proxy) GetCallback() interface{} { return p.callBack } + +// GetInvoker gets Invoker. +func (p *Proxy) GetInvoker() protocol.Invoker { + return p.invoke +} diff --git a/config/interfaces/config_post_processor.go b/config/interfaces/config_post_processor.go new file mode 100644 index 0000000000000000000000000000000000000000..53dd71780ff0807065e8c4b0e00ab4fe8b6a4823 --- /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 05df3477362561644a841b147d67a660536ffdbc..431ec0e2eb3c03b27cca40acd7b721cf6b8f9755 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*") @@ -185,6 +188,11 @@ func (c *ReferenceConfig) GetRPCService() common.RPCService { return c.pxy.Get() } +// GetProxy gets proxy +func (c *ReferenceConfig) GetProxy() *proxy.Proxy { + return c.pxy +} + func (c *ReferenceConfig) getUrlMap() url.Values { urlMap := url.Values{} //first set user params @@ -248,3 +256,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 0a5a37e8fbb8dce48dd422bd8ee761b8e974309c..f0277a85312e60eb78e7f13ad7368eac85d81a51 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -200,6 +200,8 @@ func (c *ServiceConfig) Export() error { ivkURL.AddParam(constant.Tagkey, c.Tag) } + // post process the URL to be exported + c.postProcessConfig(ivkURL) // config post processor may set "export" to false if !ivkURL.GetParamBool(constant.EXPORT_KEY, true) { return nil @@ -342,3 +344,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) + } +}