Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a16a88e3c9 | ||
|
e39cfe4cdd | ||
|
887c8e78ea | ||
|
a67dc7b1ca | ||
|
984f887d45 | ||
|
c2df5af4f0 | ||
|
badd6647dc | ||
|
66c4e16397 | ||
|
59ac25d85d | ||
|
6d316c475f | ||
|
39af901d9d | ||
|
a518073e67 |
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/nacos-viper-remote.iml" filepath="$PROJECT_DIR$/.idea/nacos-viper-remote.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
9
.idea/nacos-viper-remote.iml
generated
Normal file
9
.idea/nacos-viper-remote.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
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.
|
29
README.md
29
README.md
@@ -1,9 +1,9 @@
|
|||||||
# nacos-viper-remote
|
# Viper remote for Nacos
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
```
|
```
|
||||||
|
64
config_remote_provider.go
Normal file
64
config_remote_provider.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package nacos_viper_remote
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ViperRemoteProvider struct {
|
||||||
|
configType string
|
||||||
|
configSet string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRemoteProvider(configType string) *ViperRemoteProvider {
|
||||||
|
return &ViperRemoteProvider{
|
||||||
|
configType: configType,
|
||||||
|
configSet: "yoyogo.cloud.discovery.metadata"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (provider *ViperRemoteProvider) GetProvider(runtimeViper *viper.Viper) *viper.Viper {
|
||||||
|
var option *Option
|
||||||
|
err := runtimeViper.Sub(provider.configSet).Unmarshal(&option)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
SetOptions(option)
|
||||||
|
remote_viper := viper.New()
|
||||||
|
err = remote_viper.AddRemoteProvider("nacos", "localhost", "")
|
||||||
|
if provider.configType == "" {
|
||||||
|
provider.configType = "yaml"
|
||||||
|
}
|
||||||
|
remote_viper.SetConfigType(provider.configType)
|
||||||
|
err = remote_viper.ReadRemoteConfig()
|
||||||
|
if err == nil {
|
||||||
|
//err = remote_viper.WatchRemoteConfigOnChannel()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("config center ..........")
|
||||||
|
fmt.Println("used remote viper by Nacos")
|
||||||
|
fmt.Printf("Nacos config: namespace: %s , group: %s", option.NamespaceId, option.GroupName)
|
||||||
|
return remote_viper
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
@@ -32,14 +32,21 @@ func main() {
|
|||||||
remote_viper := viper.New()
|
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()
|
err = remote_viper.ReadRemoteConfig()
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = remote_viper.WatchRemoteConfigOnChannel()
|
config_viper = remote_viper
|
||||||
if err == nil {
|
fmt.Println("used remote viper")
|
||||||
config_viper = remote_viper
|
provider := remote.NewRemoteProvider("yaml")
|
||||||
fmt.Println("used remote viper")
|
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")
|
appName := config_viper.GetString("yoyogo.application.name")
|
||||||
@@ -50,7 +57,7 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
time.Sleep(time.Second * 30) // delay after each request
|
time.Sleep(time.Second * 30) // delay after each request
|
||||||
appName = config_viper.GetString("yoyogo.application.name")
|
appName = config_viper.GetString("yoyogo.application.name")
|
||||||
fmt.Println(appName)
|
fmt.Println("sync:" + appName)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ yoyogo:
|
|||||||
strategy: "round-robin" # round-robin , weight-time , random
|
strategy: "round-robin" # round-robin , weight-time , random
|
||||||
type: "nacos"
|
type: "nacos"
|
||||||
metadata:
|
metadata:
|
||||||
url: "120.53.133.30"
|
url: "localhost"
|
||||||
port: 80
|
port: 80
|
||||||
namespace: "public"
|
namespace: "public"
|
||||||
group: "DEFAULT_GROUP"
|
group: "DEFAULT_GROUP"
|
||||||
@@ -18,3 +18,8 @@ yoyogo:
|
|||||||
enable: true
|
enable: true
|
||||||
username: "root"
|
username: "root"
|
||||||
password: "1234"
|
password: "1234"
|
||||||
|
endpoint: ""
|
||||||
|
regionId: ""
|
||||||
|
accessKey: ""
|
||||||
|
secretKey: ""
|
||||||
|
openKMS: false
|
||||||
|
2
go.mod
2
go.mod
@@ -3,6 +3,6 @@ module github.com/yoyofxteam/nacos-viper-remote
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
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 v1.0.7
|
||||||
|
github.com/spf13/viper v1.8.1
|
||||||
)
|
)
|
||||||
|
@@ -34,9 +34,15 @@ func NewNacosConfigManager(option *Option) (*nacosConfigManager, error) {
|
|||||||
MaxAge: 3,
|
MaxAge: 3,
|
||||||
LogLevel: "info",
|
LogLevel: "info",
|
||||||
}
|
}
|
||||||
|
|
||||||
if option.Auth != nil && option.Auth.Enable {
|
if option.Auth != nil && option.Auth.Enable {
|
||||||
clientConfig.Username = option.Auth.User
|
clientConfig.Username = option.Auth.User
|
||||||
clientConfig.Password = option.Auth.Password
|
clientConfig.Password = option.Auth.Password
|
||||||
|
clientConfig.Endpoint = option.Auth.Endpoint
|
||||||
|
clientConfig.RegionId = option.Auth.RegionId
|
||||||
|
clientConfig.AccessKey = option.Auth.AccessKey
|
||||||
|
clientConfig.SecretKey = option.Auth.SecretKey
|
||||||
|
clientConfig.OpenKMS = option.Auth.OpenKMS
|
||||||
}
|
}
|
||||||
client, err := clients.CreateConfigClient(map[string]interface{}{
|
client, err := clients.CreateConfigClient(map[string]interface{}{
|
||||||
"serverConfigs": serverConfigs,
|
"serverConfigs": serverConfigs,
|
||||||
|
@@ -17,4 +17,15 @@ type Auth struct {
|
|||||||
Enable bool `mapstructure:"enable"`
|
Enable bool `mapstructure:"enable"`
|
||||||
User string `mapstructure:"username"`
|
User string `mapstructure:"username"`
|
||||||
Password string `mapstructure:"password"`
|
Password string `mapstructure:"password"`
|
||||||
|
|
||||||
|
// ACM Endpoint
|
||||||
|
Endpoint string `mapstructure:"endpoint"`
|
||||||
|
// ACM RegionId
|
||||||
|
RegionId string `mapstructure:"regionId"`
|
||||||
|
// ACM AccessKey
|
||||||
|
AccessKey string `mapstructure:"accessKey"`
|
||||||
|
// ACM SecretKey
|
||||||
|
SecretKey string `mapstructure:"secretKey"`
|
||||||
|
// ACM OpenKMS
|
||||||
|
OpenKMS bool `mapstructure:"openKMS"`
|
||||||
}
|
}
|
||||||
|
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"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
var nacosOptions = &Option{}
|
//var nacosOptions = &Option{}
|
||||||
|
|
||||||
func SetOptions(option *Option) {
|
func SetOptions(option *Option) {
|
||||||
nacosOptions = option
|
manager, _ := NewNacosConfigManager(option)
|
||||||
|
viper.SupportedRemoteProviders = []string{"nacos"}
|
||||||
|
viper.RemoteConfig = &remoteConfigProvider{ConfigManager: manager}
|
||||||
}
|
}
|
||||||
|
|
||||||
type remoteConfigProvider struct {
|
type remoteConfigProvider struct {
|
||||||
|
ConfigManager *nacosConfigManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
|
func (rc *remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
|
||||||
cmt, err := getConfigManager(rp)
|
cmt, err := rc.getConfigManager(rp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
||||||
cmt, err := getConfigManager(rp)
|
cmt, err := rc.getConfigManager(rp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -52,15 +55,10 @@ func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *v
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfigManager(rp viper.RemoteProvider) (interface{}, error) {
|
func (rc *remoteConfigProvider) getConfigManager(rp viper.RemoteProvider) (interface{}, error) {
|
||||||
if rp.Provider() == "nacos" {
|
if rp.Provider() == "nacos" {
|
||||||
return NewNacosConfigManager(nacosOptions)
|
return rc.ConfigManager, nil
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("The Nacos configuration manager is not supported!")
|
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