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

nacos support username password

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