Skip to content

Commit

Permalink
Merge pull request #2 from LaurenceLiZhixin/config-enhance
Browse files Browse the repository at this point in the history
Metadata、Sevice-disc config fix
  • Loading branch information
zhaoyunxing92 authored Aug 14, 2021
2 parents 2411e31 + 4b6cec3 commit 2b4c477
Show file tree
Hide file tree
Showing 30 changed files with 466 additions and 472 deletions.
1 change: 1 addition & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
DEFAULT_REST_CLIENT = "resty"
DEFAULT_REST_SERVER = "go-restful"
DEFAULT_PORT = 20000
DEFAULT_METADATAPORT = 20005
DEFAULT_SERIALIZATION = HESSIAN2_SERIALIZATION
)

Expand Down
12 changes: 2 additions & 10 deletions common/constant/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,8 @@ package constant

// nolint
const (
// CONF_CONSUMER_FILE_PATH ...
CONF_CONSUMER_FILE_PATH = "CONF_CONSUMER_FILE_PATH"
// CONF_PROVIDER_FILE_PATH ...
CONF_PROVIDER_FILE_PATH = "CONF_PROVIDER_FILE_PATH"
// CONFIG_FILE_ENV_KEY is key of environment variable dubbogo configure file path
CONFIG_FILE_ENV_KEY = "DUBBO_GO_CONFIG_PATH"
// APP_LOG_CONF_FILE ...
APP_LOG_CONF_FILE = "APP_LOG_CONF_FILE"
// CONF_ROUTER_FILE_PATH Specify Path variable of router config file
CONF_ROUTER_FILE_PATH = "CONF_ROUTER_FILE_PATH"
// CONF_VIRTUAL_SERVICE_FILE_PATH Specify path to Virtual service of uniform router config file
CONF_VIRTUAL_SERVICE_FILE_PATH = "CONF_VIRTUAL_SERVICE_FILE_PATH"
// CONF_DEST_RULE_FILE_PATH Specify path to destination rule of uniform router config file
CONF_DEST_RULE_FILE_PATH = "CONF_DEST_RULE_FILE_PATH"
)
28 changes: 16 additions & 12 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ const (
// DUBBO_KEY = "dubbo"
SSL_ENABLED_KEY = "ssl-enabled"
// PARAMS_TYPE_Key key used in pass through invoker factory, to define param type
PARAMS_TYPE_Key = "parameter-type-names"
DEFAULT_Key = "default"
PARAMS_TYPE_Key = "parameter-type-names"
DEFAULT_Key = "default"
METADATATYPE_KEY = "metadata-type"
)

const (
Expand Down Expand Up @@ -122,16 +123,19 @@ const (
)

const (
REGISTRY_KEY = "registry"
REGISTRY_PROTOCOL = "registry"
ROLE_KEY = "registry.role"
REGISTRY_DEFAULT_KEY = "registry.default"
REGISTRY_TIMEOUT_KEY = "registry.timeout"
REGISTRY_LABEL_KEY = "label"
PREFERRED_KEY = "preferred"
ZONE_KEY = "zone"
ZONE_FORCE_KEY = "zone.force"
REGISTRY_TTL_KEY = "registry.ttl"
REGISTRY_KEY = "registry"
REGISTRY_PROTOCOL = "registry"
SERVICE_REGISTRY_PROTOCOL = "service-discovery-registry"
ROLE_KEY = "registry.role"
REGISTRY_DEFAULT_KEY = "registry.default"
REGISTRY_TIMEOUT_KEY = "registry.timeout"
REGISTRY_LABEL_KEY = "label"
PREFERRED_KEY = "preferred"
ZONE_KEY = "zone"
ZONE_FORCE_KEY = "zone.force"
REGISTRY_TTL_KEY = "registry.ttl"
SIMPLIFIED_KEY = "simplified"
NAMESPACE_KEY = "namespace"
)

const (
Expand Down
8 changes: 4 additions & 4 deletions common/extension/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ import (
"dubbo.apache.org/dubbo-go/v3/registry"
)

var discoveryCreatorMap = make(map[string]func(name string) (registry.ServiceDiscovery, error), 4)
var discoveryCreatorMap = make(map[string]func() (registry.ServiceDiscovery, error), 4)

// SetServiceDiscovery will store the @creator and @name
// protocol indicate the implementation, like nacos
// the name like nacos-1...
func SetServiceDiscovery(protocol string, creator func(name string) (registry.ServiceDiscovery, error)) {
func SetServiceDiscovery(protocol string, creator func() (registry.ServiceDiscovery, error)) {
discoveryCreatorMap[protocol] = creator
}

// GetServiceDiscovery will return the registry.ServiceDiscovery
// protocol indicate the implementation, like nacos
// the name like nacos-1...
// if not found, or initialize instance failed, it will return error.
func GetServiceDiscovery(protocol string, name string) (registry.ServiceDiscovery, error) {
func GetServiceDiscovery(protocol string) (registry.ServiceDiscovery, error) {
creator, ok := discoveryCreatorMap[protocol]
if !ok {
return nil, perrors.New("Could not find the service discovery with discovery protocol: " + protocol)
}
return creator(name)
return creator()
}
2 changes: 1 addition & 1 deletion common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,4 +878,4 @@ func (c *URL) GetParamDuration(s string, d string) time.Duration {
return t
}
return 3 * time.Second
}
}
2 changes: 1 addition & 1 deletion config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ApplicationConfig struct {
Owner string `default:"dubbo-go" yaml:"owner" json:"owner,omitempty" property:"owner"`
Environment string `default:"dev" yaml:"environment" json:"environment,omitempty" property:"environment"`
// the metadata type. remote or local
MetadataType string `default:"local" yaml:"metadataType" json:"metadataType,omitempty" property:"metadataType"`
MetadataType string `default:"local" yaml:"metadata-type" json:"metadataType,omitempty" property:"metadataType"`
}

// Prefix dubbo.applicationConfig
Expand Down
176 changes: 79 additions & 97 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ package config

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/registry"
"errors"
"fmt"
hessian "github.com/apache/dubbo-go-hessian2"
perrors "github.com/pkg/errors"
"reflect"
"strconv"
)
import (
"github.com/knadh/koanf"
Expand All @@ -36,20 +43,13 @@ import (

var (
rootConfig *RootConfig
//consumerConfig *consumer.ShutdownConfig
//providerConfig *provider.ProviderConfig
//// baseConfig = providerConfig.BaseConfig or consumerConfig
//baseConfig *root.ShutdownConfig
//sslEnabled = false
//
//// configAccessMutex is used to make sure that xxxxConfig will only be created once if needed.
//// it should be used combine with double-check to avoid the race condition
//configAccessMutex sync.Mutex
//
maxWait = 3
maxWait = 3
)

func Load(opts ...LoaderConfOption) error {
hessian.RegisterPOJO(&common.MetadataInfo{})
hessian.RegisterPOJO(&common.ServiceInfo{})
hessian.RegisterPOJO(&common.URL{})
// conf
conf := NewLoaderConf(opts...)
// init config
Expand All @@ -60,7 +60,12 @@ func Load(opts ...LoaderConfOption) error {
return err
}
rootConfig.refresh = false
return rootConfig.InitConfig()
extension.SetAndInitGlobalDispatcher(rootConfig.EventDispatcherType)
if err := rootConfig.InitConfig(); err != nil {
return err
}
registerServiceInstance()
return nil
}

func check() error {
Expand Down Expand Up @@ -356,92 +361,65 @@ func getKoanf(conf *loaderConf) *koanf.Koanf {
// registerServiceInstance()
//}
//
//// registerServiceInstance register service instance
//func registerServiceInstance() {
// url := selectMetadataServiceExportedURL()
// if url == nil {
// return
// }
// instance, err := createInstance(url)
// if err != nil {
// panic(err)
// }
// p := extension.GetProtocol(constant.REGISTRY_KEY)
// var rp registry.RegistryFactory
// var ok bool
// if rp, ok = p.(registry.RegistryFactory); !ok {
// panic("dubbo registry protocol{" + reflect.TypeOf(p).String() + "} is invalid")
// }
// rs := rp.GetRegistries()
// for _, r := range rs {
// var sdr registry.ServiceDiscoveryHolder
// if sdr, ok = r.(registry.ServiceDiscoveryHolder); !ok {
// continue
// }
// err := sdr.GetServiceDiscovery().Register(instance)
// if err != nil {
// panic(err)
// }
// }
// // todo publish metadata to remote
// if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil {
// remoteMetadataService.PublishMetadata(GetApplicationConfig().Name)
// }
//}
// registerServiceInstance register service instance
func registerServiceInstance() {
url := selectMetadataServiceExportedURL()
if url == nil {
return
}
instance, err := createInstance(url)
if err != nil {
panic(err)
}
p := extension.GetProtocol(constant.REGISTRY_KEY)
var rp registry.RegistryFactory
var ok bool
if rp, ok = p.(registry.RegistryFactory); !ok {
panic("dubbo registry protocol{" + reflect.TypeOf(p).String() + "} is invalid")
}
rs := rp.GetRegistries()
for _, r := range rs {
var sdr registry.ServiceDiscoveryHolder
if sdr, ok = r.(registry.ServiceDiscoveryHolder); !ok {
continue
}
// publish app level data to registry
err := sdr.GetServiceDiscovery().Register(instance)
if err != nil {
panic(err)
}
}
}

//
//// nolint
//func createInstance(url *common.URL) (registry.ServiceInstance, error) {
// appConfig := GetApplicationConfig()
// port, err := strconv.ParseInt(url.Port, 10, 32)
// if err != nil {
// return nil, perrors.WithMessage(err, "invalid port: "+url.Port)
// }
//
// host := url.Ip
// if len(host) == 0 {
// host = common.GetLocalIp()
// }
//
// // usually we will add more metadata
// metadata := make(map[string]string, 8)
// metadata[constant.METADATA_STORAGE_TYPE_PROPERTY_NAME] = appConfig.MetadataType
//
// return &registry.DefaultServiceInstance{
// ServiceName: appConfig.Name,
// Host: host,
// Port: int(port),
// ID: host + constant.KEY_SEPARATOR + url.Port,
// Enable: true,
// Healthy: true,
// Metadata: metadata,
// }, nil
//}
//
//// selectMetadataServiceExportedURL get already be exported url
//func selectMetadataServiceExportedURL() *common.URL {
// var selectedUrl *common.URL
// metaDataService, err := extension.GetLocalMetadataService("")
// if err != nil {
// logger.Warn(err)
// return nil
// }
// urlList, err := metaDataService.GetExportedURLs(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE)
// if err != nil {
// panic(err)
// }
// if len(urlList) == 0 {
// return nil
// }
// for _, url := range urlList {
// selectedUrl = url
// // rest first
// if url.Protocol == "rest" {
// break
// }
// }
// return selectedUrl
//}
//
func createInstance(url *common.URL) (registry.ServiceInstance, error) {
appConfig := GetApplicationConfig()
port, err := strconv.ParseInt(url.Port, 10, 32)
if err != nil {
return nil, perrors.WithMessage(err, "invalid port: "+url.Port)
}

host := url.Ip
if len(host) == 0 {
host = common.GetLocalIp()
}

// usually we will add more metadata
metadata := make(map[string]string, 8)
metadata[constant.METADATA_STORAGE_TYPE_PROPERTY_NAME] = appConfig.MetadataType

return &registry.DefaultServiceInstance{
ServiceName: appConfig.Name,
Host: host,
Port: int(port),
ID: host + constant.KEY_SEPARATOR + url.Port,
Enable: true,
Healthy: true,
Metadata: metadata,
}, nil
}

//func initRouter() {
// if uniformDestRuleConfigPath != "" && uniformVirtualServiceConfigPath != "" {
// if err := router.RouterInit(uniformVirtualServiceConfigPath, uniformDestRuleConfigPath); err != nil {
Expand Down Expand Up @@ -474,7 +452,7 @@ func getKoanf(conf *loaderConf) *koanf.Koanf {
// // init the shutdown callback
// shutdown.GracefulShutdownInit()
//}
//

// GetRPCService get rpc service for consumer
func GetRPCService(name string) common.RPCService {
return rootConfig.Consumer.References[name].GetRPCService()
Expand Down Expand Up @@ -503,6 +481,10 @@ func GetMetricConfig() *MetricConfig {
return rootConfig.MetricConfig
}

func GetMetadataReportConfg() *MetadataReportConfig {
return rootConfig.MetadataReportConfig
}

// GetApplicationConfig find the applicationConfig config
// if not, we will create one
// Usually applicationConfig will be initialized when system start
Expand Down
8 changes: 7 additions & 1 deletion config/config_loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package config

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"fmt"
"os"
"path/filepath"
Expand All @@ -39,9 +40,14 @@ type loaderConf struct {
}

func NewLoaderConf(opts ...LoaderConfOption) *loaderConf {
configFilePath := "../conf/dubbogo.yaml"
if configFilePathFromEnv := os.Getenv(constant.CONFIG_FILE_ENV_KEY); configFilePathFromEnv != "" {
configFilePath = configFilePathFromEnv
}

conf := &loaderConf{
genre: "yaml",
path: "./conf/application.yaml",
path: configFilePath,
delim: ".",
}

Expand Down
Loading

0 comments on commit 2b4c477

Please sign in to comment.