Skip to content

Commit

Permalink
Added read-write mode for nacos config
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Aug 5, 2024
1 parent b7c07f6 commit d7a6a45
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/nacos/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package nacos

import (
"context"
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/v2/etc"
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
"time"
)

const (
defaultMode = config.ReadOnly
defaultUrl = "http://127.0.0.1:8848/nacos"
defaultClusterName = "DEFAULT"
defaultGroupName = "DEFAULT_GROUP"
Expand All @@ -26,6 +28,7 @@ const (
)

const (
defaultModeKey = "etc.config.nacos.mode"
defaultUrlsKey = "etc.config.nacos.urls"
defaultClusterNameKey = "etc.config.nacos.clusterName"
defaultGroupNameKey = "etc.config.nacos.groupName"
Expand All @@ -50,6 +53,10 @@ type options struct {
// 默认context.Background
ctx context.Context

// 读写模式
// 支持read-only、write-only和read-write三种模式,默认为read-only模式
mode config.Mode

// 服务器地址 [scheme://]ip:port[/nacos]
// 默认为[]string{http://127.0.0.1:8848/nacos}
urls []string
Expand Down Expand Up @@ -119,6 +126,7 @@ type options struct {
func defaultOptions() *options {
return &options{
ctx: context.Background(),
mode: config.Mode(etc.Get(defaultModeKey, defaultMode).String()),
urls: etc.Get(defaultUrlsKey, []string{defaultUrl}).Strings(),
clusterName: etc.Get(defaultClusterNameKey, defaultClusterName).String(),
groupName: etc.Get(defaultGroupNameKey, defaultGroupName).String(),
Expand All @@ -142,6 +150,11 @@ func WithContext(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}

// WithMode 设置读写模式
func WithMode(mode config.Mode) Option {
return func(o *options) { o.mode = mode }
}

// WithUrls 设置服务器地址
func WithUrls(urls ...string) Option {
return func(o *options) { o.urls = urls }
Expand Down
4 changes: 4 additions & 0 deletions config/nacos/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (s *Source) Store(ctx context.Context, file string, content []byte) error {
return s.err
}

if s.opts.mode != config.WriteOnly && s.opts.mode != config.ReadWrite {
return errors.ErrNoOperationPermission
}

_, err := s.opts.client.PublishConfig(vo.ConfigParam{
DataId: file,
Group: s.opts.groupName,
Expand Down
34 changes: 34 additions & 0 deletions testdata/etc/etc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,40 @@ timezone = "Local"
path = "config"
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-only"
# nacos配置中心
[config.nacos]
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-only"
# 服务器地址 [scheme://]ip:port[/nacos]。默认为["http://127.0.0.1:8848/nacos"]
urls = ["http://127.0.0.1:8848/nacos"]
# 集群名称。默认为DEFAULT
clusterName = "DEFAULT"
# 群组名称。默认为DEFAULT_GROUP
groupName = "DEFAULT_GROUP"
# 请求Nacos服务端超时时间,支持单位:纳秒(ns)、微秒(us | µs)、毫秒(ms)、秒(s)、分(m)、小时(h)、天(d)。默认为3秒
timeout = "3s"
# ACM的命名空间Id。默认为空
namespaceId = ""
# 当使用ACM时,需要该配置,默认为空。详见:https://help.aliyun.com/document_detail/130146.html
endpoint = ""
# ACM&KMS的regionId,用于配置中心的鉴权。默认为空
regionId = ""
# ACM&KMS的AccessKey,用于配置中心的鉴权。默认为空
accessKey = ""
# ACM&KMS的SecretKey,用于配置中心的鉴权。默认为空
secretKey = ""
# 是否开启kms,同时DataId必须以"cipher-"作为前缀才会启动加解密逻辑。kms可以参考文档:https://help.aliyun.com/product/28933.html
openKMS = false
# 缓存service信息的目录。默认为./run/nacos/naming/cache
cacheDir = "./run/nacos/config/cache"
# Nacos服务端的API鉴权Username。默认为空
username = ""
# Nacos服务端的API鉴权Password。默认为空
password = ""
# 日志存储路径。默认为./run/nacos/naming/log
logDir = "./run/nacos/config/log"
# 日志输出级别,可选:debug、info、warn、error。默认为info
logLevel = "info"

[cluster]
# 集群网关配置
Expand Down

0 comments on commit d7a6a45

Please sign in to comment.