Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1a0b2e62cc | ||
|
5b276dc208 | ||
|
e39cfe4cdd |
19
.gitignore
vendored
Normal file
19
.gitignore
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
### Go template
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
.idea
|
||||||
|
vendor/
|
||||||
|
logs
|
||||||
|
go.sum
|
27
README.md
27
README.md
@@ -3,7 +3,7 @@
|
|||||||
Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs.
|
Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
runtime_viper := viper.New()
|
config_viper := viper.New()
|
||||||
|
|
||||||
remote.SetOptions(&remote.Option{
|
remote.SetOptions(&remote.Option{
|
||||||
Url: "localhost",
|
Url: "localhost",
|
||||||
@@ -14,13 +14,30 @@ remote.SetOptions(&remote.Option{
|
|||||||
Auth: nil,
|
Auth: nil,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
remote_viper := viper.New()
|
||||||
err := remote_viper.AddRemoteProvider("nacos", "localhost", "")
|
err := remote_viper.AddRemoteProvider("nacos", "localhost", "")
|
||||||
remote_viper.SetConfigType("yaml")
|
remote_viper.SetConfigType("yaml")
|
||||||
|
err = remote_viper.ReadRemoteConfig() //sync get remote configs to remote_viper instance memory . for example , remote_viper.GetString(key)
|
||||||
|
|
||||||
_ = remote_viper.ReadRemoteConfig() //sync get remote configs to remote_viper instance memory . for example , remote_viper.GetString(key)
|
if err == nil {
|
||||||
_ = remote_viper.WatchRemoteConfigOnChannel() //async watch , auto refresh configs.
|
config_viper = remote_viper
|
||||||
|
fmt.Println("used remote viper")
|
||||||
|
provider := remote.NewRemoteProvider("yaml")
|
||||||
|
respChan := provider.WatchRemoteConfigOnChannel(config_viper)
|
||||||
|
|
||||||
appName := remote_viper.GetString("key") // sync get config by key
|
go func(rc <-chan bool) {
|
||||||
|
for {
|
||||||
|
<-rc
|
||||||
|
fmt.Printf("remote async: %s", config_viper.GetString("yoyogo.application.name"))
|
||||||
|
}
|
||||||
|
}(respChan)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(appName)
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(time.Second * 30) // delay after each request
|
||||||
|
appName = config_viper.GetString("yoyogo.application.name")
|
||||||
|
fmt.Println("sync:" + appName)
|
||||||
|
}
|
||||||
|
}()
|
||||||
```
|
```
|
||||||
|
@@ -2,8 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
remote "github.com/Echo7659/nacos-viper-remote"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
remote "github.com/yoyofxteam/nacos-viper-remote"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
6
go.mod
6
go.mod
@@ -1,8 +1,8 @@
|
|||||||
module github.com/yoyofxteam/nacos-viper-remote
|
module github.com/Echo7659/nacos-viper-remote
|
||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/nacos-group/nacos-sdk-go v1.0.7
|
github.com/nacos-group/nacos-sdk-go/v2 v2.0.0
|
||||||
github.com/spf13/viper v1.8.1
|
github.com/spf13/viper v1.10.1
|
||||||
)
|
)
|
||||||
|
@@ -2,11 +2,11 @@ package nacos_viper_remote
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/nacos-group/nacos-sdk-go/clients"
|
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
||||||
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
|
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
|
||||||
"github.com/nacos-group/nacos-sdk-go/common/constant"
|
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
|
||||||
"github.com/nacos-group/nacos-sdk-go/common/logger"
|
"github.com/nacos-group/nacos-sdk-go/v2/common/logger"
|
||||||
"github.com/nacos-group/nacos-sdk-go/vo"
|
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -30,8 +30,8 @@ func NewNacosConfigManager(option *Option) (*nacosConfigManager, error) {
|
|||||||
NamespaceId: option.NamespaceId,
|
NamespaceId: option.NamespaceId,
|
||||||
TimeoutMs: 5000,
|
TimeoutMs: 5000,
|
||||||
NotLoadCacheAtStart: true,
|
NotLoadCacheAtStart: true,
|
||||||
RotateTime: "1h",
|
LogDir: "logs/nacos/log",
|
||||||
MaxAge: 3,
|
CacheDir: "logs/nacos/cache",
|
||||||
LogLevel: "info",
|
LogLevel: "info",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,10 +44,7 @@ func NewNacosConfigManager(option *Option) (*nacosConfigManager, error) {
|
|||||||
clientConfig.SecretKey = option.Auth.SecretKey
|
clientConfig.SecretKey = option.Auth.SecretKey
|
||||||
clientConfig.OpenKMS = option.Auth.OpenKMS
|
clientConfig.OpenKMS = option.Auth.OpenKMS
|
||||||
}
|
}
|
||||||
client, err := clients.CreateConfigClient(map[string]interface{}{
|
client, err := clients.NewConfigClient(vo.NacosClientParam{ClientConfig: &clientConfig, ServerConfigs: serverConfigs})
|
||||||
"serverConfigs": serverConfigs,
|
|
||||||
"clientConfig": clientConfig,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Error(err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Reference in New Issue
Block a user