Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取配置中心时,会获取***_failover 文件,但实际是没有这个文件的,2.2.5版本才出现这个错误 #762

Closed
liuhengbo opened this issue Jun 15, 2024 · 6 comments

Comments

@liuhengbo
Copy link

请教一下为什么会出现这个报错

go版本
go 1.21.1

包版本:

github.com/nacos-group/nacos-sdk-go/v2 v2.2.6

代码如下:

// InitServerConfig 加载配置中心的配置
func InitServerConfig() error {
	// 创建clientConfig
	clientConfig := constant.ClientConfig{
		NamespaceId:         global.LCF.Nacos.NamespaceId, // 如果需要支持多namespace,我们可以创建多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。
		NotLoadCacheAtStart: true,
		LogDir:              "user-web/tmp/nacos/log",
		CacheDir:            "user-web/tmp/nacos/cache",
		Username:            "nacos",
		Password:            "nacos",
		TimeoutMs:           5000,
	}
	// 至少一个ServerConfig
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr: global.LCF.Nacos.Host,
			Port:   uint64(global.LCF.Nacos.Port),
		},
	}
	configClient, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)
	if err != nil {
		return fmt.Errorf("server config client init error:%w", err)
	}
	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: global.LCF.Nacos.DataId,
		Group:  global.LCF.Env,
	})
	if err != nil {
		return fmt.Errorf("server config configClient GetConfig error:%w", err)
	}
	if content == "" {
		return fmt.Errorf("content is empty")
	}
	vp := viper.New()
	vp.SetConfigType("yaml")

	if err := configClient.ListenConfig(vo.ConfigParam{
		DataId: global.LCF.Nacos.DataId,
		Group:  global.LCF.Env,
		OnChange: func(namespace, group, dataId, data string) {
			// 配置变化
			if err := vp.ReadConfig(strings.NewReader(data)); err != nil {
				zap.L().Error("配置中心配置发生变化,读取失败", zap.Error(err))
				return
			}
			if err := vp.Unmarshal(&global.CF); err != nil {
				zap.L().Error("配置中心配置发生变化,Unmarshal失败", zap.Error(err))
				return
			}
			zap.L().Info("配置中心配置发生变化,重新加载后的配置为:", zap.Any("config", global.CF))
		},
	}); err != nil {
		return fmt.Errorf("config configClient ListenConfig error:%w", err)
	}

	if err := vp.ReadConfig(strings.NewReader(content)); err != nil {
		return fmt.Errorf("server config ReadConfig error:%w", err)
	}
	if err := vp.Unmarshal(&global.CF); err != nil {
		zap.L().Error("写入Server配置文件失败", zap.Error(err))
		return err
	}
	zap.L().Info("加载配置中心配置文件成功", zap.Any("config", global.CF))
	return nil
}

文件
实际写入的文件尾部没有 _failover
image

错误信息:

2024-06-15T15:33:12.221+0800    ERROR   cache/disk_cache.go:193 read Config Content failed. cause file doesn't exist, file path: user-web/tmp/nacos/cache/config/user-web2@@local@@a7ade997-2fc9-4926-9d55-3c662fc41965_failover.
github.com/nacos-group/nacos-sdk-go/v2/clients/cache.getFailOverConfig
        /Users/liuhengbo/Desktop/project/golang/pkg/mod/github.com/nacos-group/nacos-sdk-go/[email protected]/clients/cache/disk_cache.go:193
github.com/nacos-group/nacos-sdk-go/v2/clients/cache.GetFailover
        /Users/liuhengbo/Desktop/project/golang/pkg/mod/github.com/nacos-group/nacos-sdk-go/[email protected]/clients/cache/disk_cache.go:182
github.com/nacos-group/nacos-sdk-go/v2/clients/config_client.(*ConfigClient).getConfigInner
        /Users/liuhengbo/Desktop/project/golang/pkg/mod/github.com/nacos-group/nacos-sdk-go/[email protected]/clients/config_client/config_client.go:186
github.com/nacos-group/nacos-sdk-go/v2/clients/config_client.(*ConfigClient).GetConfig
        /Users/liuhengbo/Desktop/project/golang/pkg/mod/github.com/nacos-group/nacos-sdk-go/[email protected]/clients/config_client/config_client.go:160
mxshop-api/user-web/initialize.InitServerConfig
        /Users/liuhengbo/Desktop/project/go_sys_classes/mxshop-api/user-web/initialize/config.go:82
main.main
        /Users/liuhengbo/Desktop/project/go_sys_classes/mxshop-api/user-web/main.go:28
runtime.main
        /Users/liuhengbo/Desktop/project/golang/go1.21.6/src/runtime/proc.go:267

@liuhengbo
Copy link
Author

查看源码发现2.2.5之后才出现的这个报错,请问这个报错是有什么特殊含义吗,因为看起来这个报错没什么意义(也可能是我不理解)
2.2.5之后的源码(包括2.2.5)

// GetFailover , get failover content
func GetFailover(key, dir string) string {
	filePath := GetConfigFailOverContentFileName(key, dir)
	return getFailOverConfig(filePath, ConfigContent)
}
func getFailOverConfig(filePath string, fileType ConfigCachedFileType) string {
	if !file.IsExistFile(filePath) {
		errMsg := fmt.Sprintf("read %s failed. cause file doesn't exist, file path: %s.", fileType, filePath)
		logger.Error(errMsg)
		return ""
	}
	logger.Warnf("reading failover %s from path:%s", fileType, filePath)
	fileContent, err := os.ReadFile(filePath)
	if err != nil {
		logger.Errorf("fail to read failover %s from %s", fileType, filePath)
		return ""
	}
	return string(fileContent)
}

2.2.5之前的代码不包括2.2.5

// GetFailover , get failover content
func GetFailover(key, dir string) string {
	filePath := dir + string(os.PathSeparator) + key + constant.FAILOVER_FILE_SUFFIX
	if !file.IsExistFile(filePath) {
		return ""
	}
	logger.Warnf("reading failover content from path:%s", filePath)
	fileContent, err := os.ReadFile(filePath)
	if err != nil {
		logger.Errorf("fail to read failover content from %s", filePath)
		return ""
	}
	return string(fileContent)
}

@liuhengbo liuhengbo changed the title 获取配置中心时,会获取***_failover 文件,但实际是没有这个文件的 获取配置中心时,会获取***_failover 文件,但实际是没有这个文件的,2.2.5版本才出现 Jun 15, 2024
@liuhengbo liuhengbo changed the title 获取配置中心时,会获取***_failover 文件,但实际是没有这个文件的,2.2.5版本才出现 获取配置中心时,会获取***_failover 文件,但实际是没有这个文件的,2.2.5版本才出现这个错误 Jun 15, 2024
@Abeautifulsnow
Copy link

我也遇到这个问题了,等一个解答!🤔

@acexy
Copy link

acexy commented Jun 17, 2024

看起来可能是做了一次破坏性功能升级,或者是没有兼容一些情况,回退版本可以使用,确实是新版本有这个问题。

@liuhengbo
Copy link
Author

看起来是2.25版本这段加了一个日志打印, 之前版本没有这个打印

if !file.IsExistFile(filePath) {
		errMsg := fmt.Sprintf("read %s failed. cause file doesn't exist, file path: %s.", fileType, filePath)
		logger.Error(errMsg)
		return ""
	}

@a1499418300
Copy link

我也遇到这个问题了,切到2.24无错误输出

@yiboGit
Copy link

yiboGit commented Jun 28, 2024

同样遇到了这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants