老师你好,我照着你的代码写了一遍,代码的意思就是从consul中获取mysql配置,
func GetConsulConfig(host string, port int64, prefix string) (config.Config, error) {
source := consul.NewSource(
consul.WithAddress(host+":"+strconv.FormatInt(port, 10)),
consul.WithPrefix(prefix),
consul.StripPrefix(true),
)
newConfig, err := config.NewConfig()
if err != nil {
return newConfig, err
}
err = config.Load(source)
return newConfig, err
}
func GetMysqlFromConsul(config config.Config, path ...string) *MysqlConfig {
mysqlConfig := &MysqlConfig{}
config.Get(path...).Scan(mysqlConfig)
return mysqlConfig
}
func main(){
consulConfig, err := common.GetConsulConfig("127.0.0.1", 8500, "/micro/config")
......
mysqlInfo := common.GetMysqlFromConsul(consulConfig, "mysql")
......
fmt.Printf("Host:%s\nuser:%s\npassword:%s\ndatabase:%s\nport:%d\n", mysqlInfo.Host, mysqlInfo.User, mysqlInfo.Password, mysqlInfo.Database, mysqlInfo.Port)
}
output:
Host:
user:
password:
database:
port:0
然后我试着打印从consul获取到的mysql配置,发现为空,获取不了,这么是什么情况呢?