提交 f1fff87e authored 作者: mooncake9527's avatar mooncake9527

nacos support username password

上级 dae148c3
......@@ -128,7 +128,14 @@ func nacosRegisterInstance() (err error) {
c.Rd.Nacos.ServiceName = conf.App.Name
c.Rd.Nacos.ServiceIP = localhost
c.Rd.Nacos.ServicePort = conf.HTTP.Port
if c.Rd.Nacos.Username == "" && c.ConfCenter.Nacos.Username != "" {
c.Rd.Nacos.Username = c.ConfCenter.Nacos.Username
}
if c.Rd.Nacos.Password == "" && c.ConfCenter.Nacos.Password != "" {
c.Rd.Nacos.Password = c.ConfCenter.Nacos.Password
}
})
config.Read(func(c *config.Config) {
copier.Copy(&conf, c)
})
......
......@@ -63,7 +63,9 @@ rd:
enable: true
healthy: true
ephemeral: true
username: nacos
password: 4RSl6H3w!xEs
database:
- name: "operator"
driver: "mysql"
......
......@@ -109,6 +109,8 @@ func nacosConf() {
namespace := readEnvVarWithDefault("NACOS_NAMESPACE", "")
dataID := readEnvVarWithDefault("NACOS_DATA_ID", "conf.yaml")
group := readEnvVarWithDefault("NACOS_GROUP", "DEFAULT_GROUP")
username := readEnvVarWithDefault("NACOS_USERNAME", "nacos")
password := readEnvVarWithDefault("NACOS_PASSWORD", "123456")
logger.Infof("[conf]conf host:%s namespace:%s dataId:%s group:%s", host, namespace, dataID, group)
Cfg.ConfCenter.Nacos.IPs = strings.Split(host, ",")
Cfg.ConfCenter.Nacos.Port = cast.ToInt(port)
......@@ -116,6 +118,8 @@ func nacosConf() {
Cfg.ConfCenter.Nacos.DataID = dataID
Cfg.ConfCenter.Nacos.Group = group
Cfg.ConfCenter.Nacos.ContextPath = contextPath
Cfg.ConfCenter.Nacos.Username = username
Cfg.ConfCenter.Nacos.Password = password
logger.Debug("[conf]nacos ", logger.Any("conf", Cfg.ConfCenter.Nacos))
}
......
......@@ -60,8 +60,8 @@ func (x *NacosClient) Connect() *NacosClient {
constant.WithUpdateCacheWhenEmpty(true), //
constant.WithLogDir("/tmp/nacos/log"), // 日志目录
constant.WithCacheDir("/tmp/nacos/cache"), // 缓存目录
// constant.WithUsername("nacos"), // 用户名
// constant.WithPassword("nacos"), // 密码
constant.WithUsername(x.cfg.Username), // 用户名
constant.WithPassword(x.cfg.Password), // 密码
)
logger.Debug("[conf][nacos]", logger.Any("x.cfg", x.cfg))
serverConfigs := make([]constant.ServerConfig, 0, len(x.cfg.IPs))
......
......@@ -7,6 +7,8 @@ type NacosConfConfig struct {
ContextPath string `yaml:"context" json:"context"` // nacos server context path
DataID string `yaml:"dataId" json:"dataId"` // nacos config dataId
Group string `yaml:"group" json:"group"` // nacos config group
Username string `yaml:"username" json:"username"` // nacos username
Password string `yaml:"password" json:"password"` // nacos password
}
type NacosSvcConfig struct {
......@@ -23,4 +25,6 @@ type NacosSvcConfig struct {
Enable bool `yaml:"enable" json:"enable"` // service instance enable
Healthy bool `yaml:"healthy" json:"healthy"` // service instance healthy
Ephemeral bool `yaml:"ephemeral" json:"ephemeral"` // service instance ephemeral
Username string `yaml:"username" json:"username"` //
Password string `yaml:"password" json:"password"` //
}
......@@ -7,7 +7,6 @@ import (
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/vo"
"gitlab.wanzhuangkj.com/tush/xpkg/config"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
)
type NacosServiceClient struct {
......@@ -24,22 +23,21 @@ func NewWithConfig(cfg *config.NacosSvcConfig) (*NacosServiceClient, error) {
serverConfigs := make([]constant.ServerConfig, 0, len(cfg.IPs))
for _, ip := range cfg.IPs {
serverConfig := constant.NewServerConfig(
ip, // Nacos服务地址
uint64(cfg.Port), // Nacos服务端口
ip, //
uint64(cfg.Port), //
constant.WithContextPath(cfg.Context),
)
serverConfigs = append(serverConfigs, *serverConfig)
}
nnc.sc = serverConfigs
logger.Debug("[rd][nacos]", logger.Any("nnc.cfg", nnc.cfg))
nnc.cc = constant.NewClientConfig(
constant.WithNamespaceId(nnc.cfg.Namespace), // 命名空间ID
constant.WithTimeoutMs(5000), // 请求超时时间
constant.WithNotLoadCacheAtStart(false), // 启动时不读取缓存
constant.WithLogDir("/tmp/nacos/log"), // 日志目录
constant.WithCacheDir("/tmp/nacos/cache"), // 缓存目录
// constant.WithUsername("nacos"), // 用户名
// constant.WithPassword("nacos"), // 密码
constant.WithUsername(nnc.cfg.Username), // 用户名
constant.WithPassword(nnc.cfg.Password), // 密码
)
namingClient, err := clients.NewNamingClient(
vo.NacosClientParam{
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论