Skip to content
Snippets Groups Projects
Commit 7b9e9122 authored by fangyincheng's avatar fangyincheng
Browse files

Ref: refact config loader

parent 799b8ea1
No related branches found
No related tags found
No related merge requests found
Showing
with 195 additions and 312 deletions
......@@ -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]()
}
......@@ -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)
}
......@@ -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 {
......
......@@ -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
......
......@@ -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
}
package interfaces
import "bytes"
// ConfigReader
type ConfigReader interface {
ReadConsumerConfig(reader *bytes.Buffer) error
ReadProviderConfig(reader *bytes.Buffer) error
}
......@@ -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
}
......
/*
* 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
}
/*
* 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)
}
/*
* 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")
}
......@@ -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
}
......@@ -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())
}
......@@ -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
}
......@@ -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,
......
......@@ -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})
......
......@@ -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
......
......@@ -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
......
......@@ -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()
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment