Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
e384306728 | |||
|
1a0b2e62cc | ||
|
5b276dc208 | ||
|
e39cfe4cdd | ||
|
14b8a67811 | ||
|
887c8e78ea | ||
|
a67dc7b1ca | ||
|
984f887d45 | ||
|
c2df5af4f0 |
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
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 yoyofx
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
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.
|
||||
|
||||
```go
|
||||
runtime_viper := viper.New()
|
||||
config_viper := viper.New()
|
||||
|
||||
remote.SetOptions(&remote.Option{
|
||||
Url: "localhost",
|
||||
@@ -14,13 +14,30 @@ remote.SetOptions(&remote.Option{
|
||||
Auth: nil,
|
||||
})
|
||||
|
||||
remote_viper := viper.New()
|
||||
err := remote_viper.AddRemoteProvider("nacos", "localhost", "")
|
||||
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)
|
||||
_ = remote_viper.WatchRemoteConfigOnChannel() //async watch , auto refresh configs.
|
||||
if err == nil {
|
||||
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)
|
||||
}
|
||||
}()
|
||||
```
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package nacos_viper_remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -16,9 +17,9 @@ func NewRemoteProvider(configType string) *ViperRemoteProvider {
|
||||
configSet: "yoyogo.cloud.discovery.metadata"}
|
||||
}
|
||||
|
||||
func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *viper.Viper {
|
||||
func (provider *ViperRemoteProvider) GetProvider(runtimeViper *viper.Viper) *viper.Viper {
|
||||
var option *Option
|
||||
err := runtime_viper.Sub(provider.configSet).Unmarshal(&option)
|
||||
err := runtimeViper.Sub(provider.configSet).Unmarshal(&option)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil
|
||||
@@ -32,7 +33,7 @@ func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *vi
|
||||
remote_viper.SetConfigType(provider.configType)
|
||||
err = remote_viper.ReadRemoteConfig()
|
||||
if err == nil {
|
||||
err = remote_viper.WatchRemoteConfigOnChannel()
|
||||
//err = remote_viper.WatchRemoteConfigOnChannel()
|
||||
if err == nil {
|
||||
fmt.Println("used remote viper")
|
||||
return remote_viper
|
||||
@@ -40,5 +41,22 @@ func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *vi
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
return runtime_viper
|
||||
return runtimeViper
|
||||
}
|
||||
|
||||
func (provider *ViperRemoteProvider) WatchRemoteConfigOnChannel(remoteViper *viper.Viper) <-chan bool {
|
||||
updater := make(chan bool)
|
||||
|
||||
respChan, _ := viper.RemoteConfig.WatchChannel(DefaultRemoteProvider())
|
||||
go func(rc <-chan *viper.RemoteResponse) {
|
||||
for {
|
||||
b := <-rc
|
||||
reader := bytes.NewReader(b.Value)
|
||||
_ = remoteViper.ReadConfig(reader)
|
||||
// configuration on changed
|
||||
updater <- true
|
||||
}
|
||||
}(respChan)
|
||||
|
||||
return updater
|
||||
}
|
||||
|
@@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
remote "github.com/Echo7659/nacos-viper-remote"
|
||||
"github.com/spf13/viper"
|
||||
remote "github.com/yoyofxteam/nacos-viper-remote"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
@@ -32,14 +32,21 @@ func main() {
|
||||
remote_viper := viper.New()
|
||||
err := remote_viper.AddRemoteProvider("nacos", "localhost", "")
|
||||
remote_viper.SetConfigType("yaml")
|
||||
|
||||
err = remote_viper.ReadRemoteConfig()
|
||||
|
||||
if err == nil {
|
||||
err = remote_viper.WatchRemoteConfigOnChannel()
|
||||
if err == nil {
|
||||
config_viper = remote_viper
|
||||
fmt.Println("used remote viper")
|
||||
}
|
||||
config_viper = remote_viper
|
||||
fmt.Println("used remote viper")
|
||||
provider := remote.NewRemoteProvider("yaml")
|
||||
respChan := provider.WatchRemoteConfigOnChannel(config_viper)
|
||||
|
||||
go func(rc <-chan bool) {
|
||||
for {
|
||||
<-rc
|
||||
fmt.Printf("remote async: %s", config_viper.GetString("yoyogo.application.name"))
|
||||
}
|
||||
}(respChan)
|
||||
|
||||
}
|
||||
|
||||
appName := config_viper.GetString("yoyogo.application.name")
|
||||
@@ -50,7 +57,7 @@ func main() {
|
||||
for {
|
||||
time.Sleep(time.Second * 30) // delay after each request
|
||||
appName = config_viper.GetString("yoyogo.application.name")
|
||||
fmt.Println(appName)
|
||||
fmt.Println("sync:" + appName)
|
||||
}
|
||||
}()
|
||||
|
||||
|
6
go.mod
6
go.mod
@@ -1,8 +1,8 @@
|
||||
module github.com/yoyofxteam/nacos-viper-remote
|
||||
module git.echol.cn/loser/nacos-viper-remote
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/spf13/viper v1.7.1
|
||||
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.10.1
|
||||
)
|
||||
|
@@ -2,11 +2,11 @@ package nacos_viper_remote
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/nacos-group/nacos-sdk-go/clients"
|
||||
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
|
||||
"github.com/nacos-group/nacos-sdk-go/common/constant"
|
||||
"github.com/nacos-group/nacos-sdk-go/common/logger"
|
||||
"github.com/nacos-group/nacos-sdk-go/vo"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/common/logger"
|
||||
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
||||
"github.com/spf13/viper"
|
||||
"strings"
|
||||
)
|
||||
@@ -30,8 +30,8 @@ func NewNacosConfigManager(option *Option) (*nacosConfigManager, error) {
|
||||
NamespaceId: option.NamespaceId,
|
||||
TimeoutMs: 5000,
|
||||
NotLoadCacheAtStart: true,
|
||||
RotateTime: "1h",
|
||||
MaxAge: 3,
|
||||
LogDir: "logs/nacos/log",
|
||||
CacheDir: "logs/nacos/cache",
|
||||
LogLevel: "info",
|
||||
}
|
||||
|
||||
@@ -44,10 +44,7 @@ func NewNacosConfigManager(option *Option) (*nacosConfigManager, error) {
|
||||
clientConfig.SecretKey = option.Auth.SecretKey
|
||||
clientConfig.OpenKMS = option.Auth.OpenKMS
|
||||
}
|
||||
client, err := clients.CreateConfigClient(map[string]interface{}{
|
||||
"serverConfigs": serverConfigs,
|
||||
"clientConfig": clientConfig,
|
||||
})
|
||||
client, err := clients.NewConfigClient(vo.NacosClientParam{ClientConfig: &clientConfig, ServerConfigs: serverConfigs})
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
return nil, err
|
||||
|
28
nacosprovider.go
Normal file
28
nacosprovider.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package nacos_viper_remote
|
||||
|
||||
type nacosRemoteProvider struct {
|
||||
provider string
|
||||
endpoint string
|
||||
path string
|
||||
secretKeyring string
|
||||
}
|
||||
|
||||
func DefaultRemoteProvider() *nacosRemoteProvider {
|
||||
return &nacosRemoteProvider{provider: "nacos", endpoint: "localhost", path: "", secretKeyring: ""}
|
||||
}
|
||||
|
||||
func (rp nacosRemoteProvider) Provider() string {
|
||||
return rp.provider
|
||||
}
|
||||
|
||||
func (rp nacosRemoteProvider) Endpoint() string {
|
||||
return rp.endpoint
|
||||
}
|
||||
|
||||
func (rp nacosRemoteProvider) Path() string {
|
||||
return rp.path
|
||||
}
|
||||
|
||||
func (rp nacosRemoteProvider) SecretKeyring() string {
|
||||
return rp.secretKeyring
|
||||
}
|
@@ -7,17 +7,20 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
var nacosOptions = &Option{}
|
||||
//var nacosOptions = &Option{}
|
||||
|
||||
func SetOptions(option *Option) {
|
||||
nacosOptions = option
|
||||
manager, _ := NewNacosConfigManager(option)
|
||||
viper.SupportedRemoteProviders = []string{"nacos"}
|
||||
viper.RemoteConfig = &remoteConfigProvider{ConfigManager: manager}
|
||||
}
|
||||
|
||||
type remoteConfigProvider struct {
|
||||
ConfigManager *nacosConfigManager
|
||||
}
|
||||
|
||||
func (rc *remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
|
||||
cmt, err := getConfigManager(rp)
|
||||
cmt, err := rc.getConfigManager(rp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -37,7 +40,7 @@ func (rc *remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error
|
||||
}
|
||||
|
||||
func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
||||
cmt, err := getConfigManager(rp)
|
||||
cmt, err := rc.getConfigManager(rp)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -52,15 +55,10 @@ func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *v
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func getConfigManager(rp viper.RemoteProvider) (interface{}, error) {
|
||||
func (rc *remoteConfigProvider) getConfigManager(rp viper.RemoteProvider) (interface{}, error) {
|
||||
if rp.Provider() == "nacos" {
|
||||
return NewNacosConfigManager(nacosOptions)
|
||||
return rc.ConfigManager, nil
|
||||
} else {
|
||||
return nil, errors.New("The Nacos configuration manager is not supported!")
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
viper.SupportedRemoteProviders = []string{"nacos"}
|
||||
viper.RemoteConfig = &remoteConfigProvider{}
|
||||
}
|
||||
|
Reference in New Issue
Block a user