diff --git a/common/extension/rest_config_reader.go b/common/extension/confit_reader.go similarity index 61% rename from common/extension/rest_config_reader.go rename to common/extension/confit_reader.go index 1bd338cc203583636a8c2c94213ea4c14bea2ae5..fe7124cf42dcb7e3cc27f88e98769e7f0f838902 100644 --- a/common/extension/rest_config_reader.go +++ b/common/extension/confit_reader.go @@ -18,24 +18,22 @@ package extension import ( - "github.com/apache/dubbo-go/config/rest/config_reader" + "github.com/apache/dubbo-go/config/interfaces" ) var ( - restConfigReaders = make(map[string]func() config_reader.RestConfigReader) + configReaders = make(map[string]func() interfaces.ConfigReader) ) -func SetRestConfigReader(name string, fun func() config_reader.RestConfigReader) { - restConfigReaders[name] = fun +// SetConfigReaders set a creator of config reader. +func SetConfigReaders(name string, v func() interfaces.ConfigReader) { + configReaders[name] = v } -func GetSingletonRestConfigReader(name string) config_reader.RestConfigReader { - if name == "" { - name = "default" +// GetConfigReaders get a config reader by name. +func GetConfigReaders(name string) interfaces.ConfigReader { + if configReaders[name] == nil { + panic("config reader for " + name + " is not existing, make sure you have imported the package.") } - if restConfigReaders[name] == nil { - panic("restConfigReaders for " + name + " is not existing, make sure you have import the package.") - } - return restConfigReaders[name]() - + return configReaders[name]() } diff --git a/common/yaml/yaml.go b/common/yaml/yaml.go index 1d8ac65e90d0e041647c70db07b92b9df5d9644d..7c31d71c35fff547d2ed0a765e8245717148a451 100644 --- a/common/yaml/yaml.go +++ b/common/yaml/yaml.go @@ -41,10 +41,10 @@ func LoadYMLConfig(confProFile string) ([]byte, error) { } // unmarshalYMLConfig Load yml config byte from file , then unmarshal to object -func UnmarshalYMLConfig(confProFile string, out interface{}) error { +func UnmarshalYMLConfig(confProFile string, out interface{}) ([]byte, error) { confFileStream, err := LoadYMLConfig(confProFile) if err != nil { - return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err)) + return confFileStream, perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err)) } - return yaml.Unmarshal(confFileStream, out) + return confFileStream, yaml.Unmarshal(confFileStream, out) } diff --git a/common/yaml/yaml_test.go b/common/yaml/yaml_test.go index 8d0eefddc28ff8013fa8bc8074ef3abfa96f2b2d..a239ac7b6cb31b6f9cce8fef7fe0f3b1dad87722 100644 --- a/common/yaml/yaml_test.go +++ b/common/yaml/yaml_test.go @@ -13,7 +13,8 @@ func TestUnmarshalYMLConfig(t *testing.T) { conPath, err := filepath.Abs("./testdata/config.yml") assert.NoError(t, err) c := &Config{} - assert.NoError(t, UnmarshalYMLConfig(conPath, c)) + _, err = UnmarshalYMLConfig(conPath, c) + assert.NoError(t, err) assert.Equal(t, "strTest", c.StrTest) assert.Equal(t, 11, c.IntTest) assert.Equal(t, false, c.BooleanTest) @@ -22,8 +23,10 @@ func TestUnmarshalYMLConfig(t *testing.T) { func TestUnmarshalYMLConfig_Error(t *testing.T) { c := &Config{} - assert.Error(t, UnmarshalYMLConfig("./testdata/config", c)) - assert.Error(t, UnmarshalYMLConfig("", c)) + _, err := UnmarshalYMLConfig("./testdata/config", c) + assert.Error(t, err) + _, err = UnmarshalYMLConfig("", c) + assert.Error(t, err) } type Config struct { diff --git a/config/config_loader.go b/config/config_loader.go index 138106541c6acd691c9dffd209191ffe0c07e886..14acc7bd7335dcc5de7849f4a899f2dfee3cb29d 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -90,10 +90,6 @@ func Load() { if consumerConfig == nil { logger.Warnf("consumerConfig is nil!") } else { - // init rest consumer config - if err := ConsumerRestConfigInit(consumerConfig.RestConfigType); err != nil { - log.Printf("[initConsumerRestConfig] %#v", err) - } metricConfig = consumerConfig.MetricConfig applicationConfig = consumerConfig.ApplicationConfig @@ -154,10 +150,6 @@ func Load() { if providerConfig == nil { logger.Warnf("providerConfig is nil!") } else { - // init rest provider config - if err := ProviderRestConfigInit(providerConfig.RestConfigType); err != nil { - log.Printf("[initProviderRestConfig] %#v", err) - } // so, you should know that the consumer's config will be override metricConfig = providerConfig.MetricConfig applicationConfig = providerConfig.ApplicationConfig diff --git a/config/consumer_config.go b/config/consumer_config.go index c3fe12d7036371636cd0d4531b49fec976b906d8..21034e0dd51837557e94c236b657e917521cc3f5 100644 --- a/config/consumer_config.go +++ b/config/consumer_config.go @@ -18,6 +18,10 @@ package config import ( + "bytes" + "fmt" + "github.com/apache/dubbo-go/common/extension" + "strings" "time" ) @@ -58,7 +62,7 @@ type ConsumerConfig struct { ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf"` FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" ` - RestConfigType string `default:"default" yaml:"rest_config_type" json:"rest_config_type,omitempty" property:"rest_config_type"` + ConfigType string `default:"default" yaml:"config_type" json:"config_type,omitempty" property:"config_type"` } // UnmarshalYAML ... @@ -89,7 +93,7 @@ func ConsumerInit(confConFile string) error { return perrors.Errorf("application configure(consumer) file name is nil") } consumerConfig = &ConsumerConfig{} - err := yaml.UnmarshalYMLConfig(confConFile, consumerConfig) + fileStream, err := yaml.UnmarshalYMLConfig(confConFile, consumerConfig) if err != nil { return perrors.Errorf("unmarshalYmlConfig error %v", perrors.WithStack(err)) } @@ -116,6 +120,19 @@ func ConsumerInit(confConFile string) error { } } logger.Debugf("consumer config{%#v}\n", consumerConfig) + + // init other consumer config + conConfigType := consumerConfig.ConfigType + if len(conConfigType) > 0 { + for _, t := range strings.Split(conConfigType, ",") { + if len(t) > 0 { + if err = extension.GetConfigReaders(t).ReadConsumerConfig(bytes.NewBuffer(fileStream)); err != nil { + return perrors.New(fmt.Sprintf("ReadConsumerConfig error: %v for %s", perrors.WithStack(err), t)) + } + } + } + } + return nil } @@ -139,5 +156,6 @@ func configCenterRefreshConsumer() error { return perrors.WithMessagef(err, "time.ParseDuration(Connect_Timeout{%#v})", consumerConfig.Connect_Timeout) } } - return err + + return nil } diff --git a/config/interfaces/config_reader.go b/config/interfaces/config_reader.go new file mode 100644 index 0000000000000000000000000000000000000000..23f2225e1bd670d43065f3b6eca08385a5c964a2 --- /dev/null +++ b/config/interfaces/config_reader.go @@ -0,0 +1,9 @@ +package interfaces + +import "bytes" + +// ConfigReader +type ConfigReader interface { + ReadConsumerConfig(reader *bytes.Buffer) error + ReadProviderConfig(reader *bytes.Buffer) error +} diff --git a/config/provider_config.go b/config/provider_config.go index d8562863ed8da68c3688afa739bed63b7a82b343..a45228f0a43d9473ca071861cc35ed560355f1e4 100644 --- a/config/provider_config.go +++ b/config/provider_config.go @@ -17,6 +17,12 @@ package config +import ( + "bytes" + "fmt" + "strings" +) + import ( "github.com/creasty/defaults" perrors "github.com/pkg/errors" @@ -24,6 +30,7 @@ import ( import ( "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/common/yaml" ) @@ -46,7 +53,7 @@ type ProviderConfig struct { ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf" ` FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` ShutdownConfig *ShutdownConfig `yaml:"shutdown_conf" json:"shutdown_conf,omitempty" property:"shutdown_conf" ` - RestConfigType string `default:"default" yaml:"rest_config_type" json:"rest_config_type,omitempty" property:"rest_config_type"` + ConfigType string `default:"default" yaml:"config_type" json:"config_type,omitempty" property:"config_type"` } // UnmarshalYAML ... @@ -77,7 +84,7 @@ func ProviderInit(confProFile string) error { return perrors.Errorf("application configure(provider) file name is nil") } providerConfig = &ProviderConfig{} - err := yaml.UnmarshalYMLConfig(confProFile, providerConfig) + fileStream, err := yaml.UnmarshalYMLConfig(confProFile, providerConfig) if err != nil { return perrors.Errorf("unmarshalYmlConfig error %v", perrors.WithStack(err)) } @@ -92,6 +99,18 @@ func ProviderInit(confProFile string) error { logger.Debugf("provider config{%#v}\n", providerConfig) + // init other provider config + proConfigType := providerConfig.ConfigType + if len(proConfigType) > 0 { + for _, t := range strings.Split(proConfigType, ",") { + if len(t) > 0 { + if err = extension.GetConfigReaders(t).ReadProviderConfig(bytes.NewBuffer(fileStream)); err != nil { + return perrors.New(fmt.Sprintf("ReadProviderConfig error: %v for %s", perrors.WithStack(err), t)) + } + } + } + } + return nil } diff --git a/config/rest/config_reader/reader_impl/default_config_reader.go b/config/rest/config_reader/reader_impl/default_config_reader.go deleted file mode 100644 index 40e1ecf9a0e629f9f42e28dccf1531c4ab6b4311..0000000000000000000000000000000000000000 --- a/config/rest/config_reader/reader_impl/default_config_reader.go +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 reader_impl - -import ( - "github.com/apache/dubbo-go/config/rest" - "github.com/apache/dubbo-go/config/rest/config_reader" - "os" -) - -import ( - perrors "github.com/pkg/errors" -) - -import ( - "github.com/apache/dubbo-go/common/constant" - "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/common/yaml" -) - -var ( - defaultConfigReader *DefaultConfigReader -) - -func init() { - extension.SetRestConfigReader(constant.DEFAULT_KEY, GetDefaultConfigReader) -} - -type DefaultConfigReader struct { -} - -func NewDefaultConfigReader() *DefaultConfigReader { - return &DefaultConfigReader{} -} - -func (dcr *DefaultConfigReader) ReadConsumerConfig() (*rest.RestConsumerConfig, error) { - confConFile := os.Getenv(constant.CONF_CONSUMER_FILE_PATH) - if len(confConFile) == 0 { - return nil, nil - } - restConsumerConfig := &rest.RestConsumerConfig{} - err := yaml.UnmarshalYMLConfig(confConFile, restConsumerConfig) - if err != nil { - return nil, perrors.Errorf("[Rest Config] unmarshal Consumer RestYmlConfig error %v", perrors.WithStack(err)) - } - return restConsumerConfig, nil -} - -func (dcr *DefaultConfigReader) ReadProviderConfig() (*rest.RestProviderConfig, error) { - confProFile := os.Getenv(constant.CONF_PROVIDER_FILE_PATH) - if len(confProFile) == 0 { - return nil, nil - } - restProviderConfig := &rest.RestProviderConfig{} - err := yaml.UnmarshalYMLConfig(confProFile, restProviderConfig) - if err != nil { - return nil, perrors.Errorf("[Rest Config] unmarshal Provider RestYmlConfig error %v", perrors.WithStack(err)) - - } - return restProviderConfig, nil -} - -func GetDefaultConfigReader() config_reader.RestConfigReader { - if defaultConfigReader == nil { - defaultConfigReader = NewDefaultConfigReader() - } - return defaultConfigReader -} diff --git a/config/rest/config_reader/rest_config_reader.go b/config/rest/config_reader/rest_config_reader.go deleted file mode 100644 index 366cde6a0db561140570068f4a5f9d2c9690d409..0000000000000000000000000000000000000000 --- a/config/rest/config_reader/rest_config_reader.go +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 config_reader - -import "github.com/apache/dubbo-go/config/rest" - -type RestConfigReader interface { - ReadConsumerConfig() (*rest.RestConsumerConfig, error) - ReadProviderConfig() (*rest.RestProviderConfig, error) -} diff --git a/config/rest_config_loader_test.go b/config/rest_config_loader_test.go deleted file mode 100644 index 7b2491a501428f7e5c1701d8d49fbd0048913c6f..0000000000000000000000000000000000000000 --- a/config/rest_config_loader_test.go +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 config - -import ( - "os" - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -import ( - "github.com/apache/dubbo-go/common/constant" -) - -func TestGetRestConsumerServiceConfig(t *testing.T) { - err := os.Setenv(constant.CONF_CONSUMER_FILE_PATH, "./rest/config_reader/reader_impl/testdata/consumer_config.yml") - assert.NoError(t, err) - err = ConsumerRestConfigInit("default") - assert.NoError(t, err) - serviceConfig := GetRestConsumerServiceConfig("UserProvider") - assert.NotEmpty(t, serviceConfig) - assert.NotEmpty(t, serviceConfig.RestMethodConfigsMap) - assert.NotEmpty(t, serviceConfig.RestMethodConfigsMap["GetUser"]) - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].QueryParamsMap[1], "userid") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].HeadersMap[3], "age") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].PathParamsMap[4], "time") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].Body, 0) - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].Produces, "application/xml") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].Consumes, "application/xml") - assert.Equal(t, serviceConfig.Client, "resty1") -} - -func TestGetRestProviderServiceConfig(t *testing.T) { - err := os.Setenv(constant.CONF_PROVIDER_FILE_PATH, "./rest/config_reader/reader_impl/testdata/provider_config.yml") - assert.NoError(t, err) - err = ProviderRestConfigInit("default") - assert.NoError(t, err) - serviceConfig := GetRestProviderServiceConfig("UserProvider") - assert.NotEmpty(t, serviceConfig) - assert.NotEmpty(t, serviceConfig.RestMethodConfigsMap) - assert.NotEmpty(t, serviceConfig.RestMethodConfigsMap["GetUser"]) - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].QueryParamsMap[1], "userid") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].HeadersMap[3], "age") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].PathParamsMap[4], "time") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].Body, 0) - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].Produces, "application/xml") - assert.Equal(t, serviceConfig.RestMethodConfigsMap["GetUser"].Consumes, "application/xml") - assert.Equal(t, serviceConfig.Server, "go-restful1") - -} diff --git a/config/rest_config_loader.go b/protocol/rest/config/reader/rest_config_reader.go similarity index 60% rename from config/rest_config_loader.go rename to protocol/rest/config/reader/rest_config_reader.go index 6596f7b274e8545fc8e053c3c4f88217338db9ea..ff19e97e2edb9932f259d40a547102c16291198e 100644 --- a/config/rest_config_loader.go +++ b/protocol/rest/config/reader/rest_config_reader.go @@ -15,73 +15,78 @@ * limitations under the License. */ -package config +package reader import ( + "bytes" + "github.com/apache/dubbo-go/protocol/rest/config" "strconv" "strings" ) import ( perrors "github.com/pkg/errors" + "gopkg.in/yaml.v2" ) import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" - "github.com/apache/dubbo-go/config/rest" - _ "github.com/apache/dubbo-go/config/rest/config_reader/reader_impl" + "github.com/apache/dubbo-go/config/interfaces" ) -var ( - restConsumerServiceConfigMap map[string]*rest.RestServiceConfig - restProviderServiceConfigMap map[string]*rest.RestServiceConfig -) +const REST = "rest" -// initConsumerRestConfig ... -func ConsumerRestConfigInit(configType string) error { - consumerConfigReader := extension.GetSingletonRestConfigReader(configType) - var restConsumerConfig *rest.RestConsumerConfig - var err error - if restConsumerConfig, err = consumerConfigReader.ReadConsumerConfig(); err != nil { - return err - } - if restConsumerConfig == nil || len(restConsumerConfig.RestServiceConfigsMap) == 0 { - return perrors.New("Consumer don't has RestServiceConfigsMap ") +func init() { + extension.SetConfigReaders(REST, NewRestConfigReader) +} + +type RestConfigReader struct { +} + +func NewRestConfigReader() interfaces.ConfigReader { + return &RestConfigReader{} +} + +// ReadConsumerConfig read consumer config for rest protocol +func (cr *RestConfigReader) ReadConsumerConfig(reader *bytes.Buffer) error { + restConsumerConfig := &config.RestConsumerConfig{} + err := yaml.Unmarshal(reader.Bytes(), restConsumerConfig) + if err != nil { + return perrors.Errorf("[Rest Config] unmarshal Consumer error %#v", perrors.WithStack(err)) } - restConsumerServiceConfigMap = make(map[string]*rest.RestServiceConfig, len(restConsumerConfig.RestServiceConfigsMap)) + + restConsumerServiceConfigMap := make(map[string]*config.RestServiceConfig, len(restConsumerConfig.RestServiceConfigsMap)) for key, rc := range restConsumerConfig.RestServiceConfigsMap { rc.Client = getNotEmptyStr(rc.Client, restConsumerConfig.Client, constant.DEFAULT_REST_CLIENT) rc.RestMethodConfigsMap = initMethodConfigMap(rc, restConsumerConfig.Consumes, restConsumerConfig.Produces) restConsumerServiceConfigMap[strings.TrimPrefix(key, "/")] = rc } + config.SetRestConsumerServiceConfigMap(restConsumerServiceConfigMap) return nil } -// initProviderRestConfig ... -func ProviderRestConfigInit(configType string) error { - providerConfigReader := extension.GetSingletonRestConfigReader(configType) - var restProviderConfig *rest.RestProviderConfig - var err error - if restProviderConfig, err = providerConfigReader.ReadProviderConfig(); err != nil { - return err +// ReadProviderConfig read provider config for rest protocol +func (cr *RestConfigReader) ReadProviderConfig(reader *bytes.Buffer) error { + restProviderConfig := &config.RestProviderConfig{} + err := yaml.Unmarshal(reader.Bytes(), restProviderConfig) + if err != nil { + return perrors.Errorf("[Rest Config] unmarshal Provider error %#v", perrors.WithStack(err)) } - if restProviderConfig == nil || len(restProviderConfig.RestServiceConfigsMap) == 0 { - return perrors.New("Provider don't has RestServiceConfigsMap") - } - restProviderServiceConfigMap = make(map[string]*rest.RestServiceConfig, len(restProviderConfig.RestServiceConfigsMap)) + restProviderServiceConfigMap := make(map[string]*config.RestServiceConfig, len(restProviderConfig.RestServiceConfigsMap)) for key, rc := range restProviderConfig.RestServiceConfigsMap { rc.Server = getNotEmptyStr(rc.Server, restProviderConfig.Server, constant.DEFAULT_REST_SERVER) rc.RestMethodConfigsMap = initMethodConfigMap(rc, restProviderConfig.Consumes, restProviderConfig.Produces) restProviderServiceConfigMap[strings.TrimPrefix(key, "/")] = rc } + config.SetRestProviderServiceConfigMap(restProviderServiceConfigMap) return nil } // initProviderRestConfig ... -func initMethodConfigMap(rc *rest.RestServiceConfig, consumes string, produces string) map[string]*rest.RestMethodConfig { - mcm := make(map[string]*rest.RestMethodConfig, len(rc.RestMethodConfigs)) +func initMethodConfigMap(rc *config.RestServiceConfig, consumes string, produces string) map[string]*config.RestMethodConfig { + mcm := make(map[string]*config.RestMethodConfig, len(rc.RestMethodConfigs)) for _, mc := range rc.RestMethodConfigs { mc.InterfaceName = rc.InterfaceName mc.Path = rc.Path + mc.Path @@ -107,7 +112,7 @@ func getNotEmptyStr(args ...string) string { } // transformMethodConfig -func transformMethodConfig(methodConfig *rest.RestMethodConfig) *rest.RestMethodConfig { +func transformMethodConfig(methodConfig *config.RestMethodConfig) *config.RestMethodConfig { if len(methodConfig.PathParamsMap) == 0 && len(methodConfig.PathParams) > 0 { paramsMap, err := parseParamsString2Map(methodConfig.PathParams) if err != nil { @@ -150,23 +155,3 @@ func parseParamsString2Map(params string) (map[int]string, error) { } return m, nil } - -// GetRestConsumerServiceConfig ... -func GetRestConsumerServiceConfig(path string) *rest.RestServiceConfig { - return restConsumerServiceConfigMap[path] -} - -// GetRestProviderServiceConfig ... -func GetRestProviderServiceConfig(path string) *rest.RestServiceConfig { - return restProviderServiceConfigMap[path] -} - -// SetRestConsumerServiceConfigMap ... -func SetRestConsumerServiceConfigMap(configMap map[string]*rest.RestServiceConfig) { - restConsumerServiceConfigMap = configMap -} - -// SetRestProviderServiceConfigMap ... -func SetRestProviderServiceConfigMap(configMap map[string]*rest.RestServiceConfig) { - restProviderServiceConfigMap = configMap -} diff --git a/config/rest/config_reader/reader_impl/default_config_reader_test.go b/protocol/rest/config/reader/rest_config_reader_test.go similarity index 55% rename from config/rest/config_reader/reader_impl/default_config_reader_test.go rename to protocol/rest/config/reader/rest_config_reader_test.go index eca7436a1016af5f1500a5ba240fc86d01375730..a4e09d2d9c434a1c5f3c8b222433a522183f78bd 100644 --- a/config/rest/config_reader/reader_impl/default_config_reader_test.go +++ b/protocol/rest/config/reader/rest_config_reader_test.go @@ -15,10 +15,12 @@ * limitations under the License. */ -package reader_impl +package reader import ( - "os" + "bytes" + "github.com/apache/dubbo-go/common/yaml" + "github.com/apache/dubbo-go/protocol/rest/config" "testing" ) @@ -26,24 +28,20 @@ import ( "github.com/stretchr/testify/assert" ) -import ( - "github.com/apache/dubbo-go/common/constant" -) - -func TestDefaultConfigReader_ReadConsumerConfig(t *testing.T) { - err := os.Setenv(constant.CONF_CONSUMER_FILE_PATH, "./testdata/consumer_config.yml") +func TestRestConfigReader_ReadConsumerConfig(t *testing.T) { + bs, err := yaml.LoadYMLConfig("./testdata/consumer_config.yml") assert.NoError(t, err) - reader := GetDefaultConfigReader() - config, err := reader.ReadConsumerConfig() - assert.Nil(t, err) - assert.NotEmpty(t, config) + configReader := NewRestConfigReader() + err = configReader.ReadConsumerConfig(bytes.NewBuffer(bs)) + assert.NoError(t, err) + assert.NotEmpty(t, config.GetRestConsumerServiceConfigMap()) } -func TestDefaultConfigReader_ReadProviderConfig(t *testing.T) { - err := os.Setenv(constant.CONF_PROVIDER_FILE_PATH, "./testdata/provider_config.yml") +func TestRestConfigReader_ReadProviderConfig(t *testing.T) { + bs, err := yaml.LoadYMLConfig("./testdata/provider_config.yml") + assert.NoError(t, err) + configReader := NewRestConfigReader() + err = configReader.ReadProviderConfig(bytes.NewBuffer(bs)) assert.NoError(t, err) - reader := GetDefaultConfigReader() - config, err := reader.ReadProviderConfig() - assert.Nil(t, err) - assert.NotEmpty(t, config) + assert.NotEmpty(t, config.GetRestProviderServiceConfigMap()) } diff --git a/config/rest/config_reader/reader_impl/testdata/consumer_config.yml b/protocol/rest/config/reader/testdata/consumer_config.yml similarity index 100% rename from config/rest/config_reader/reader_impl/testdata/consumer_config.yml rename to protocol/rest/config/reader/testdata/consumer_config.yml diff --git a/config/rest/config_reader/reader_impl/testdata/provider_config.yml b/protocol/rest/config/reader/testdata/provider_config.yml similarity index 100% rename from config/rest/config_reader/reader_impl/testdata/provider_config.yml rename to protocol/rest/config/reader/testdata/provider_config.yml diff --git a/config/rest/rest_config.go b/protocol/rest/config/rest_config.go similarity index 83% rename from config/rest/rest_config.go rename to protocol/rest/config/rest_config.go index 7262776ec3c4ea7ab6ddcd30d93227dacb83d95e..168ec8ce525fc7fd5d4a30d4f11ba7bf42d1c921 100644 --- a/config/rest/rest_config.go +++ b/protocol/rest/config/rest_config.go @@ -15,9 +15,16 @@ * limitations under the License. */ -package rest +package config -import "github.com/creasty/defaults" +import ( + "github.com/creasty/defaults" +) + +var ( + restConsumerServiceConfigMap map[string]*RestServiceConfig + restProviderServiceConfigMap map[string]*RestServiceConfig +) // RestConsumerConfig ... type RestConsumerConfig struct { @@ -114,3 +121,33 @@ func (c *RestMethodConfig) UnmarshalYAML(unmarshal func(interface{}) error) erro } return nil } + +// GetRestConsumerServiceConfig ... +func GetRestConsumerServiceConfig(path string) *RestServiceConfig { + return restConsumerServiceConfigMap[path] +} + +// GetRestProviderServiceConfig ... +func GetRestProviderServiceConfig(path string) *RestServiceConfig { + return restProviderServiceConfigMap[path] +} + +// SetRestConsumerServiceConfigMap ... +func SetRestConsumerServiceConfigMap(configMap map[string]*RestServiceConfig) { + restConsumerServiceConfigMap = configMap +} + +// SetRestProviderServiceConfigMap ... +func SetRestProviderServiceConfigMap(configMap map[string]*RestServiceConfig) { + restProviderServiceConfigMap = configMap +} + +// GetRestConsumerServiceConfigMap ... +func GetRestConsumerServiceConfigMap() map[string]*RestServiceConfig { + return restConsumerServiceConfigMap +} + +// GetRestProviderServiceConfigMap ... +func GetRestProviderServiceConfigMap() map[string]*RestServiceConfig { + return restProviderServiceConfigMap +} diff --git a/protocol/rest/rest_invoker.go b/protocol/rest/rest_invoker.go index 02efba59d95ac6e049279b78dafdf36364b022ed..0c82035ac5eb9a52ab188baa971dbdf1b864e970 100644 --- a/protocol/rest/rest_invoker.go +++ b/protocol/rest/rest_invoker.go @@ -28,19 +28,19 @@ import ( import ( "github.com/apache/dubbo-go/common" - "github.com/apache/dubbo-go/config/rest" "github.com/apache/dubbo-go/protocol" invocation_impl "github.com/apache/dubbo-go/protocol/invocation" "github.com/apache/dubbo-go/protocol/rest/client" + "github.com/apache/dubbo-go/protocol/rest/config" ) type RestInvoker struct { protocol.BaseInvoker client client.RestClient - restMethodConfigMap map[string]*rest.RestMethodConfig + restMethodConfigMap map[string]*config.RestMethodConfig } -func NewRestInvoker(url common.URL, client *client.RestClient, restMethodConfig map[string]*rest.RestMethodConfig) *RestInvoker { +func NewRestInvoker(url common.URL, client *client.RestClient, restMethodConfig map[string]*config.RestMethodConfig) *RestInvoker { return &RestInvoker{ BaseInvoker: *protocol.NewBaseInvoker(url), client: *client, diff --git a/protocol/rest/rest_invoker_test.go b/protocol/rest/rest_invoker_test.go index b6eaa17334fd6a86738594fbdf86a7667c0eecad..bea9aa81d97e8080e5b40fed237c34b2147f7645 100644 --- a/protocol/rest/rest_invoker_test.go +++ b/protocol/rest/rest_invoker_test.go @@ -19,8 +19,6 @@ package rest import ( "context" - "github.com/apache/dubbo-go/protocol/rest/client" - "github.com/apache/dubbo-go/protocol/rest/client/client_impl" "testing" "time" ) @@ -33,9 +31,10 @@ import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/config" - "github.com/apache/dubbo-go/config/rest" - _ "github.com/apache/dubbo-go/config/rest/config_reader/reader_impl" "github.com/apache/dubbo-go/protocol/invocation" + "github.com/apache/dubbo-go/protocol/rest/client" + "github.com/apache/dubbo-go/protocol/rest/client/client_impl" + rest_config "github.com/apache/dubbo-go/protocol/rest/config" ) func TestRestInvoker_Invoke(t *testing.T) { @@ -52,8 +51,8 @@ func TestRestInvoker_Invoke(t *testing.T) { assert.NoError(t, err) con := config.ProviderConfig{} config.SetProviderConfig(con) - configMap := make(map[string]*rest.RestServiceConfig) - methodConfigMap := make(map[string]*rest.RestMethodConfig) + configMap := make(map[string]*rest_config.RestServiceConfig) + methodConfigMap := make(map[string]*rest_config.RestMethodConfig) queryParamsMap := make(map[int]string) queryParamsMap[1] = "age" queryParamsMap[2] = "name" @@ -61,7 +60,7 @@ func TestRestInvoker_Invoke(t *testing.T) { pathParamsMap[0] = "userid" headersMap := make(map[int]string) headersMap[3] = "Content-Type" - methodConfigMap["GetUserOne"] = &rest.RestMethodConfig{ + methodConfigMap["GetUserOne"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUserOne", Path: "/GetUserOne", @@ -74,7 +73,7 @@ func TestRestInvoker_Invoke(t *testing.T) { QueryParamsMap: nil, Body: 0, } - methodConfigMap["GetUserTwo"] = &rest.RestMethodConfig{ + methodConfigMap["GetUserTwo"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUserTwo", Path: "/GetUserTwo", @@ -87,7 +86,7 @@ func TestRestInvoker_Invoke(t *testing.T) { QueryParamsMap: nil, Body: 0, } - methodConfigMap["GetUserThree"] = &rest.RestMethodConfig{ + methodConfigMap["GetUserThree"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUserThree", Path: "/GetUserThree", @@ -100,7 +99,7 @@ func TestRestInvoker_Invoke(t *testing.T) { QueryParamsMap: nil, Body: 0, } - methodConfigMap["GetUserFour"] = &rest.RestMethodConfig{ + methodConfigMap["GetUserFour"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUserFour", Path: "/GetUserFour", @@ -113,7 +112,7 @@ func TestRestInvoker_Invoke(t *testing.T) { QueryParamsMap: nil, Body: 0, } - methodConfigMap["GetUserFive"] = &rest.RestMethodConfig{ + methodConfigMap["GetUserFive"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUserFive", Path: "/GetUserFive", @@ -121,7 +120,7 @@ func TestRestInvoker_Invoke(t *testing.T) { Consumes: "*/*", MethodType: "GET", } - methodConfigMap["GetUser"] = &rest.RestMethodConfig{ + methodConfigMap["GetUser"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUser", Path: "/GetUser/{userid}", @@ -136,16 +135,16 @@ func TestRestInvoker_Invoke(t *testing.T) { HeadersMap: headersMap, } - configMap["com.ikurento.user.UserProvider"] = &rest.RestServiceConfig{ + configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{ Server: "go-restful", RestMethodConfigsMap: methodConfigMap, } - config.SetRestProviderServiceConfigMap(configMap) + rest_config.SetRestProviderServiceConfigMap(configMap) proxyFactory := extension.GetProxyFactory("default") proto.Export(proxyFactory.GetInvoker(url)) time.Sleep(5 * time.Second) - configMap = make(map[string]*rest.RestServiceConfig) - configMap["com.ikurento.user.UserProvider"] = &rest.RestServiceConfig{ + configMap = make(map[string]*rest_config.RestServiceConfig) + configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{ RestMethodConfigsMap: methodConfigMap, } restClient := client_impl.GetRestyClient(&client.RestOptions{ConnectTimeout: 3 * time.Second, RequestTimeout: 3 * time.Second}) diff --git a/protocol/rest/rest_protocol.go b/protocol/rest/rest_protocol.go index d068d9b6c41ed2f7c11978773ed436d8fdf46d5c..880169753412a9eae6f77dde0724cde52ec933a2 100644 --- a/protocol/rest/rest_protocol.go +++ b/protocol/rest/rest_protocol.go @@ -32,6 +32,7 @@ import ( "github.com/apache/dubbo-go/protocol" "github.com/apache/dubbo-go/protocol/rest/client" _ "github.com/apache/dubbo-go/protocol/rest/client/client_impl" + rest_config "github.com/apache/dubbo-go/protocol/rest/config" "github.com/apache/dubbo-go/protocol/rest/server" _ "github.com/apache/dubbo-go/protocol/rest/server/server_impl" ) @@ -66,7 +67,7 @@ func (rp *RestProtocol) Export(invoker protocol.Invoker) protocol.Exporter { url := invoker.GetUrl() serviceKey := url.ServiceKey() exporter := NewRestExporter(serviceKey, invoker, rp.ExporterMap()) - restServiceConfig := config.GetRestProviderServiceConfig(strings.TrimPrefix(url.Path, "/")) + restServiceConfig := rest_config.GetRestProviderServiceConfig(strings.TrimPrefix(url.Path, "/")) if restServiceConfig == nil { logger.Errorf("%s service doesn't has provider config", url.Path) return nil @@ -85,7 +86,7 @@ func (rp *RestProtocol) Refer(url common.URL) protocol.Invoker { if t, err := time.ParseDuration(requestTimeoutStr); err == nil { requestTimeout = t } - restServiceConfig := config.GetRestConsumerServiceConfig(strings.TrimPrefix(url.Path, "/")) + restServiceConfig := rest_config.GetRestConsumerServiceConfig(strings.TrimPrefix(url.Path, "/")) if restServiceConfig == nil { logger.Errorf("%s service doesn't has consumer config", url.Path) return nil diff --git a/protocol/rest/rest_protocol_test.go b/protocol/rest/rest_protocol_test.go index 30d41b352be60a8dcee5f2e2a6374035e853541b..8af73a1839c159fdf58c64d12e039c20bb3221c6 100644 --- a/protocol/rest/rest_protocol_test.go +++ b/protocol/rest/rest_protocol_test.go @@ -35,7 +35,7 @@ import ( "github.com/apache/dubbo-go/common/extension" _ "github.com/apache/dubbo-go/common/proxy/proxy_factory" "github.com/apache/dubbo-go/config" - "github.com/apache/dubbo-go/config/rest" + rest_config "github.com/apache/dubbo-go/protocol/rest/config" ) func TestRestProtocol_Refer(t *testing.T) { @@ -52,11 +52,11 @@ func TestRestProtocol_Refer(t *testing.T) { RequestTimeout: 5 * time.Second, } config.SetConsumerConfig(con) - configMap := make(map[string]*rest.RestServiceConfig) - configMap["com.ikurento.user.UserProvider"] = &rest.RestServiceConfig{ + configMap := make(map[string]*rest_config.RestServiceConfig) + configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{ Client: "resty", } - config.SetRestConsumerServiceConfigMap(configMap) + rest_config.SetRestConsumerServiceConfigMap(configMap) invoker := proto.Refer(url) // make sure url @@ -84,14 +84,14 @@ func TestRestProtocol_Export(t *testing.T) { assert.NoError(t, err) con := config.ProviderConfig{} config.SetProviderConfig(con) - configMap := make(map[string]*rest.RestServiceConfig) - methodConfigMap := make(map[string]*rest.RestMethodConfig) + configMap := make(map[string]*rest_config.RestServiceConfig) + methodConfigMap := make(map[string]*rest_config.RestMethodConfig) queryParamsMap := make(map[int]string) queryParamsMap[1] = "age" queryParamsMap[2] = "name" pathParamsMap := make(map[int]string) pathParamsMap[0] = "userid" - methodConfigMap["GetUser"] = &rest.RestMethodConfig{ + methodConfigMap["GetUser"] = &rest_config.RestMethodConfig{ InterfaceName: "", MethodName: "GetUser", Path: "/GetUser/{userid}", @@ -104,11 +104,11 @@ func TestRestProtocol_Export(t *testing.T) { QueryParamsMap: queryParamsMap, Body: -1, } - configMap["com.ikurento.user.UserProvider"] = &rest.RestServiceConfig{ + configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{ Server: "go-restful", RestMethodConfigsMap: methodConfigMap, } - config.SetRestProviderServiceConfigMap(configMap) + rest_config.SetRestProviderServiceConfigMap(configMap) proxyFactory := extension.GetProxyFactory("default") exporter := proto.Export(proxyFactory.GetInvoker(url)) // make sure url diff --git a/protocol/rest/server/rest_server.go b/protocol/rest/server/rest_server.go index e938c4d1e0f780cfd6c831bfd89aa46b75aa2fee..c10c98a7b677d47c43b64643a69d5b3768a6c663 100644 --- a/protocol/rest/server/rest_server.go +++ b/protocol/rest/server/rest_server.go @@ -19,13 +19,13 @@ package server import ( "github.com/apache/dubbo-go/common" - "github.com/apache/dubbo-go/config/rest" "github.com/apache/dubbo-go/protocol" + "github.com/apache/dubbo-go/protocol/rest/config" ) type RestServer interface { Start(url common.URL) - Deploy(invoker protocol.Invoker, restMethodConfig map[string]*rest.RestMethodConfig) - UnDeploy(restMethodConfig map[string]*rest.RestMethodConfig) + Deploy(invoker protocol.Invoker, restMethodConfig map[string]*config.RestMethodConfig) + UnDeploy(restMethodConfig map[string]*config.RestMethodConfig) Destroy() } diff --git a/protocol/rest/server/server_impl/go_restful_server.go b/protocol/rest/server/server_impl/go_restful_server.go index a630a34edb55ef39b356ea04a940b0120e38912c..51f64e1293e609bf65dd70f65af330c18e6622eb 100644 --- a/protocol/rest/server/server_impl/go_restful_server.go +++ b/protocol/rest/server/server_impl/go_restful_server.go @@ -20,6 +20,7 @@ package server_impl import ( "context" "fmt" + "github.com/apache/dubbo-go/protocol/rest/config" "github.com/apache/dubbo-go/protocol/rest/server" "net" "net/http" @@ -30,7 +31,6 @@ import ( ) import ( - "github.com/apache/dubbo-go/config/rest" "github.com/emicklei/go-restful/v3" perrors "github.com/pkg/errors" ) @@ -75,7 +75,7 @@ func (grs *GoRestfulServer) Start(url common.URL) { }() } -func (grs *GoRestfulServer) Deploy(invoker protocol.Invoker, restMethodConfig map[string]*rest.RestMethodConfig) { +func (grs *GoRestfulServer) Deploy(invoker protocol.Invoker, restMethodConfig map[string]*config.RestMethodConfig) { svc := common.ServiceMap.GetService(invoker.GetUrl().Protocol, strings.TrimPrefix(invoker.GetUrl().Path, "/")) for methodName, config := range restMethodConfig { // get method @@ -93,7 +93,7 @@ func (grs *GoRestfulServer) Deploy(invoker protocol.Invoker, restMethodConfig ma } func getFunc(methodName string, invoker protocol.Invoker, argsTypes []reflect.Type, - replyType reflect.Type, config *rest.RestMethodConfig) func(req *restful.Request, resp *restful.Response) { + replyType reflect.Type, config *config.RestMethodConfig) func(req *restful.Request, resp *restful.Response) { return func(req *restful.Request, resp *restful.Response) { var ( err error @@ -119,7 +119,7 @@ func getFunc(methodName string, invoker protocol.Invoker, argsTypes []reflect.Ty } } } -func (grs *GoRestfulServer) UnDeploy(restMethodConfig map[string]*rest.RestMethodConfig) { +func (grs *GoRestfulServer) UnDeploy(restMethodConfig map[string]*config.RestMethodConfig) { for _, config := range restMethodConfig { ws := new(restful.WebService) ws.Path(config.Path) @@ -139,7 +139,7 @@ func (grs *GoRestfulServer) Destroy() { logger.Infof("[Go Restful] Server exiting") } -func getArgsInterfaceFromRequest(req *restful.Request, config *rest.RestMethodConfig) []interface{} { +func getArgsInterfaceFromRequest(req *restful.Request, config *config.RestMethodConfig) []interface{} { argsMap := make(map[int]interface{}, 8) maxKey := 0 for k, v := range config.PathParamsMap { @@ -186,7 +186,7 @@ func getArgsInterfaceFromRequest(req *restful.Request, config *rest.RestMethodCo return args } -func getArgsFromRequest(req *restful.Request, argsTypes []reflect.Type, config *rest.RestMethodConfig) []interface{} { +func getArgsFromRequest(req *restful.Request, argsTypes []reflect.Type, config *config.RestMethodConfig) []interface{} { argsLength := len(argsTypes) args := make([]interface{}, argsLength) for i, t := range argsTypes {