Skip to content
Snippets Groups Projects
Commit 5e137121 authored by fangyincheng's avatar fangyincheng
Browse files

Imp: improve config reader

parent 50f60037
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@ import (
var (
configReaders = make(map[string]func() interfaces.ConfigReader)
defaults = make(map[string]string)
)
// SetConfigReaders set a creator of config reader.
......@@ -37,3 +38,13 @@ func GetConfigReaders(name string) interfaces.ConfigReader {
}
return configReaders[name]()
}
// SetDefaultConfitReader set {name} to default config reader for {module}
func SetDefaultConfitReader(module, name string) {
defaults[module] = name
}
// GetDefaultConfitReader
func GetDefaultConfitReader() map[string]string {
return defaults
}
......@@ -18,6 +18,7 @@
package config
import (
"bytes"
"reflect"
"strconv"
"strings"
......@@ -47,6 +48,8 @@ type BaseConfig struct {
fatherConfig interface{}
MetricConfig *MetricConfig `yaml:"metrics" json:"metrics,omitempty"`
fileStream *bytes.Buffer
}
// startConfigCenter will start the config center.
......@@ -361,5 +364,4 @@ func initializeStruct(t reflect.Type, v reflect.Value) {
}
}
}
......@@ -19,6 +19,8 @@ package config
import (
"fmt"
"github.com/apache/dubbo-go/common/extension"
perrors "github.com/pkg/errors"
"log"
"os"
"time"
......@@ -79,6 +81,29 @@ func checkApplicationName(config *ApplicationConfig) {
// Load Dubbo Init
func Load() {
// init other consumer config
conConfigType := consumerConfig.ConfigType
for key, value := range extension.GetDefaultConfitReader() {
if v, ok := conConfigType[key]; ok {
value = v
}
if err := extension.GetConfigReaders(value).ReadConsumerConfig(consumerConfig.fileStream); err != nil {
logger.Errorf("ReadConsumerConfig error: %#v for %s", perrors.WithStack(err), value)
}
}
// init other provider config
proConfigType := providerConfig.ConfigType
for key, value := range extension.GetDefaultConfitReader() {
if v, ok := proConfigType[key]; ok {
value = v
}
if err := extension.GetConfigReaders(value).ReadProviderConfig(providerConfig.fileStream); err != nil {
logger.Errorf("ReadProviderConfig error: %#v for %s", perrors.WithStack(err), value)
}
}
// init router
if confRouterFile != "" {
if errPro := RouterInit(confRouterFile); errPro != nil {
......
......@@ -19,9 +19,6 @@ package config
import (
"bytes"
"fmt"
"github.com/apache/dubbo-go/common/extension"
"strings"
"time"
)
......@@ -62,7 +59,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" `
ConfigType string `yaml:"config_type" json:"config_type,omitempty" property:"config_type"`
ConfigType map[string]string `yaml:"config_type" json:"config_type,omitempty" property:"config_type"`
}
// UnmarshalYAML ...
......@@ -97,6 +94,7 @@ func ConsumerInit(confConFile string) error {
if err != nil {
return perrors.Errorf("unmarshalYmlConfig error %v", perrors.WithStack(err))
}
consumerConfig.fileStream = bytes.NewBuffer(fileStream)
//set method interfaceId & interfaceName
for k, v := range consumerConfig.References {
//set id for reference
......@@ -121,18 +119,6 @@ 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
}
......
......@@ -19,8 +19,6 @@ package config
import (
"bytes"
"fmt"
"strings"
)
import (
......@@ -30,7 +28,6 @@ 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"
)
......@@ -53,7 +50,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" `
ConfigType string `yaml:"config_type" json:"config_type,omitempty" property:"config_type"`
ConfigType map[string]string `yaml:"config_type" json:"config_type,omitempty" property:"config_type"`
}
// UnmarshalYAML ...
......@@ -88,6 +85,8 @@ func ProviderInit(confProFile string) error {
if err != nil {
return perrors.Errorf("unmarshalYmlConfig error %v", perrors.WithStack(err))
}
providerConfig.fileStream = bytes.NewBuffer(fileStream)
//set method interfaceId & interfaceName
for k, v := range providerConfig.Services {
//set id for reference
......@@ -99,18 +98,6 @@ 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
}
......
......@@ -40,6 +40,7 @@ const REST = "rest"
func init() {
extension.SetConfigReaders(REST, NewRestConfigReader)
extension.SetDefaultConfitReader(REST, REST)
}
type RestConfigReader struct {
......
......@@ -2,6 +2,9 @@
filter: ""
config_type:
rest: "rest"
# client
request_timeout : "100ms"
# connect timeout
......
# dubbo server yaml configure file
filter: ""
config_type:
rest: "rest"
# application config
application:
organization : "ikurento.com"
......
......@@ -33,6 +33,7 @@ import (
"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/config/reader"
"github.com/apache/dubbo-go/protocol/rest/server"
_ "github.com/apache/dubbo-go/protocol/rest/server/server_impl"
)
......
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