diff --git a/common/yaml/yaml_test.go b/common/yaml/yaml_test.go index a239ac7b6cb31b6f9cce8fef7fe0f3b1dad87722..45eee59048c1c074b9c386e26cc7a2252896e6ea 100644 --- a/common/yaml/yaml_test.go +++ b/common/yaml/yaml_test.go @@ -1,3 +1,20 @@ +/* + * 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 yaml import ( diff --git a/config/rest/config_reader/reader_impl/default_config_reader.go b/config/rest/config_reader/reader_impl/default_config_reader.go new file mode 100644 index 0000000000000000000000000000000000000000..89cf247483485092057c505c9b80eae0e6d23f8f --- /dev/null +++ b/config/rest/config_reader/reader_impl/default_config_reader.go @@ -0,0 +1,83 @@ +/* + * 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 ( + "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" + "github.com/apache/dubbo-go/config/rest" + "github.com/apache/dubbo-go/config/rest/config_reader" +) + +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/protocol/rest/client/client_impl/resty_client.go b/protocol/rest/client/client_impl/resty_client.go index 839936695d49b9347448344cdfeb3f5ad6b56491..aa6c23137dc68492948b85a85555a5340572ac49 100644 --- a/protocol/rest/client/client_impl/resty_client.go +++ b/protocol/rest/client/client_impl/resty_client.go @@ -37,14 +37,14 @@ import ( ) func init() { - extension.SetRestClient(constant.DEFAULT_REST_CLIENT, GetRestyClient) + extension.SetRestClient(constant.DEFAULT_REST_CLIENT, NewRestyClient) } type RestyClient struct { client *resty.Client } -func NewRestyClient(restOption *client.RestOptions) *RestyClient { +func NewRestyClient(restOption *client.RestOptions) client.RestClient { client := resty.New() client.SetTransport( &http.Transport{ @@ -83,7 +83,3 @@ func (rc *RestyClient) Do(restRequest *client.RestRequest, res interface{}) erro } return nil } - -func GetRestyClient(restOptions *client.RestOptions) client.RestClient { - return NewRestyClient(restOptions) -} diff --git a/protocol/rest/rest_invoker_test.go b/protocol/rest/rest_invoker_test.go index bea9aa81d97e8080e5b40fed237c34b2147f7645..42a4fbd358955e8bd2d78a80818090baf62fa784 100644 --- a/protocol/rest/rest_invoker_test.go +++ b/protocol/rest/rest_invoker_test.go @@ -147,7 +147,7 @@ func TestRestInvoker_Invoke(t *testing.T) { configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{ RestMethodConfigsMap: methodConfigMap, } - restClient := client_impl.GetRestyClient(&client.RestOptions{ConnectTimeout: 3 * time.Second, RequestTimeout: 3 * time.Second}) + restClient := client_impl.NewRestyClient(&client.RestOptions{ConnectTimeout: 3 * time.Second, RequestTimeout: 3 * time.Second}) invoker := NewRestInvoker(url, &restClient, methodConfigMap) user := &User{} inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"),