From 837cda31fc32f8d6c5a78b162e0b9ef4ffb7d5bd Mon Sep 17 00:00:00 2001 From: tiecheng <18768171164@163.com> Date: Sat, 12 Dec 2020 22:19:31 +0800 Subject: [PATCH 1/3] file system run successful in win --- config_center/file/impl.go | 103 +++++++++++++++++++++++------ registry/file/service_discovery.go | 2 +- 2 files changed, 82 insertions(+), 23 deletions(-) diff --git a/config_center/file/impl.go b/config_center/file/impl.go index f29a33d5e2..f0c64ad6d3 100644 --- a/config_center/file/impl.go +++ b/config_center/file/impl.go @@ -24,7 +24,6 @@ import ( "os" "os/exec" "os/user" - "path" "path/filepath" "runtime" "strings" @@ -41,13 +40,29 @@ import ( "github.com/apache/dubbo-go/config_center/parser" ) +var osType string +var path string + const ( - PARAM_NAME_PREFIX = "dubbo.config-center." - CONFIG_CENTER_DIR_PARAM_NAME = PARAM_NAME_PREFIX + "dir" - CONFIG_CENTER_ENCODING_PARAM_NAME = PARAM_NAME_PREFIX + "encoding" - DEFAULT_CONFIG_CENTER_ENCODING = "UTF-8" + windows = "windows" ) +const ( + ParamNamePrefix = "dubbo.config-center." + ConfigCenterDirParamName = ParamNamePrefix + "dir" + ConfigCenterEncodingParamName = ParamNamePrefix + "encoding" + defaultConfigCenterEncoding = "UTF-8" +) + +func init() { + osType = runtime.GOOS + if os.IsPathSeparator('\\') { //前边的判断是否是系统的分隔符 + path = "\\" + } else { + path = "/" + } +} + // FileSystemDynamicConfiguration type FileSystemDynamicConfiguration struct { config_center.BaseDynamicConfiguration @@ -59,24 +74,14 @@ type FileSystemDynamicConfiguration struct { } func newFileSystemDynamicConfiguration(url *common.URL) (*FileSystemDynamicConfiguration, error) { - encode := url.GetParam(CONFIG_CENTER_ENCODING_PARAM_NAME, DEFAULT_CONFIG_CENTER_ENCODING) + encode := url.GetParam(ConfigCenterEncodingParamName, defaultConfigCenterEncoding) - root := url.GetParam(CONFIG_CENTER_DIR_PARAM_NAME, "") + root := url.GetParam(ConfigCenterDirParamName, "") var c *FileSystemDynamicConfiguration - if _, err := os.Stat(root); err != nil { - // not exist, use default, /XXX/xx/.dubbo/config-center - if rp, err := Home(); err != nil { - return nil, perrors.WithStack(err) - } else { - root = path.Join(rp, ".dubbo", "config-center") - } - } - if _, err := os.Stat(root); err != nil { - // it must be dir, if not exist, will create - if err = createDir(root); err != nil { - return nil, perrors.WithStack(err) - } + root, err := mkdirIfNecessary(root) + if err != nil { + return nil, err } c = &FileSystemDynamicConfiguration{ @@ -195,14 +200,14 @@ func (fsdc *FileSystemDynamicConfiguration) Close() error { // GetPath get path func (fsdc *FileSystemDynamicConfiguration) GetPath(key string, group string) string { if len(key) == 0 { - return path.Join(fsdc.rootPath, group) + return filepath.Join(fsdc.rootPath, group) } if len(group) == 0 { group = config_center.DEFAULT_GROUP } - return path.Join(fsdc.rootPath, group, key) + return filepath.Join(fsdc.rootPath, group, adapterKey(key)) } func (fsdc *FileSystemDynamicConfiguration) deleteDelay(path string) (bool, error) { @@ -307,3 +312,57 @@ func homeWindows() (string, error) { return home, nil } + +func mkdirIfNecessary(urlRoot string) (string, error) { + h := false + if len(urlRoot) == 0 { + h = true + goto Create + } + if _, err := os.Stat(urlRoot); err != nil { + h = true + goto Create + } + +Create: + if h { + // not exist, use default, mac is: /XXX/xx/.dubbo/config-center + rp, err := Home() + if err != nil { + return "", perrors.WithStack(err) + } + + urlRoot = adapterUrl(rp) + } + + if _, err := os.Stat(urlRoot); err != nil { + // it must be dir, if not exist, will create + if err = createDir(urlRoot); err != nil { + return "", perrors.WithStack(err) + } + } + + return urlRoot, nil +} + +func adapterUrl(rp string) string { + if osType == windows { + return filepath.Join(rp, "_dubbo", "config-center") + } + + return filepath.Join(rp, ".dubbo", "config-center") +} + +// used for GetPath. param key default is instance's id. +// e.g: (ip:port) 127.0.0.1:20081, in windows env, will change to 127_0_0_1_20081 +func adapterKey(key string) string { + if len(key) == 0 { + return "" + } + + if osType == windows { + return strings.ReplaceAll(strings.ReplaceAll(key, ".", "_"), ":", "_") + } + + return key +} diff --git a/registry/file/service_discovery.go b/registry/file/service_discovery.go index 254c12688f..ee4a94ae53 100644 --- a/registry/file/service_discovery.go +++ b/registry/file/service_discovery.go @@ -69,7 +69,7 @@ func newFileSystemServiceDiscovery(name string) (registry.ServiceDiscovery, erro fdcf := extension.GetConfigCenterFactory(constant.FILE_KEY) p := path.Join(rp, ".dubbo", constant.REGISTRY_KEY) url, _ := common.NewURL("") - url.AddParamAvoidNil(file.CONFIG_CENTER_DIR_PARAM_NAME, p) + url.AddParamAvoidNil(file.ConfigCenterDirParamName, p) c, err := fdcf.GetDynamicConfiguration(url) if err != nil { return nil, perrors.WithStack(err) From 55e861a0fc6fb29aec3d816974414f97d9831ca3 Mon Sep 17 00:00:00 2001 From: tiecheng <18768171164@163.com> Date: Sun, 13 Dec 2020 22:46:35 +0800 Subject: [PATCH 2/3] resolve --- config_center/file/impl.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/config_center/file/impl.go b/config_center/file/impl.go index f0c64ad6d3..c2af289559 100644 --- a/config_center/file/impl.go +++ b/config_center/file/impl.go @@ -314,18 +314,7 @@ func homeWindows() (string, error) { } func mkdirIfNecessary(urlRoot string) (string, error) { - h := false - if len(urlRoot) == 0 { - h = true - goto Create - } - if _, err := os.Stat(urlRoot); err != nil { - h = true - goto Create - } - -Create: - if h { + if !legalPath(urlRoot) { // not exist, use default, mac is: /XXX/xx/.dubbo/config-center rp, err := Home() if err != nil { @@ -345,6 +334,17 @@ Create: return urlRoot, nil } +func legalPath(path string) bool { + if len(path) == 0 { + return false + } + if _, err := os.Stat(path); err != nil { + return false + } + + return true +} + func adapterUrl(rp string) string { if osType == windows { return filepath.Join(rp, "_dubbo", "config-center") From cc7fb2e8d0569eaeaea45a74e86986aed88e7d56 Mon Sep 17 00:00:00 2001 From: tiecheng <18768171164@163.com> Date: Mon, 28 Dec 2020 13:27:58 +0800 Subject: [PATCH 3/3] rm chinese comment --- config_center/file/impl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config_center/file/impl.go b/config_center/file/impl.go index c2af289559..0233f63a96 100644 --- a/config_center/file/impl.go +++ b/config_center/file/impl.go @@ -56,7 +56,7 @@ const ( func init() { osType = runtime.GOOS - if os.IsPathSeparator('\\') { //前边的判断是否是系统的分隔符 + if os.IsPathSeparator('\\') { path = "\\" } else { path = "/"