Skip to content
Snippets Groups Projects
Commit ce868208 authored by tiecheng's avatar tiecheng
Browse files

add file service discovery config file

parent d862270c
No related branches found
No related tags found
No related merge requests found
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package file
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/config_center/parser"
)
func init() {
extension.SetConfigCenterFactory("file", func() config_center.DynamicConfigurationFactory { return &fileDynamicConfigurationFactory{} })
}
type fileDynamicConfigurationFactory struct {
}
func (f *fileDynamicConfigurationFactory) GetDynamicConfiguration(url *common.URL) (config_center.DynamicConfiguration, error) {
dynamicConfiguration, err := newFileSystemDynamicConfiguration(url)
if err != nil {
return nil, err
}
dynamicConfiguration.SetParser(&parser.DefaultConfigurationParser{})
return dynamicConfiguration, err
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package file
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"os"
"path/filepath"
"strings"
"sync"
)
import (
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/config_center/parser"
)
import (
gxset "github.com/dubbogo/gost/container/set"
)
type fileSystemDynamicConfiguration struct {
url *common.URL
rootDirectory os.File // the root directory for config center
keyListeners sync.Map
encoding string
rootPath string
}
func newFileSystemDynamicConfiguration(url *common.URL) (*fileSystemDynamicConfiguration, error) {
c := &fileSystemDynamicConfiguration{
url: url,
rootPath: "/" + url.GetParam(constant.CONFIG_NAMESPACE_KEY, config_center.DEFAULT_GROUP) + "/config",
}
return c, nil
}
func (fsdc *fileSystemDynamicConfiguration) Parser() parser.ConfigurationParser {
return nil
}
func (fsdc *fileSystemDynamicConfiguration) SetParser(parser.ConfigurationParser) {
}
func (fsdc *fileSystemDynamicConfiguration) AddListener(key string, listener config_center.ConfigurationListener, opions ...config_center.Option) {
fsdc.addListener(key, listener)
}
func (fsdc *fileSystemDynamicConfiguration) RemoveListener(key string, listener config_center.ConfigurationListener, opions ...config_center.Option) {
}
// GetProperties get properties file
func (fsdc *fileSystemDynamicConfiguration) GetProperties(string, ...config_center.Option) (string, error) {
return "", nil
}
// GetRule get Router rule properties file
func (fsdc *fileSystemDynamicConfiguration) GetRule(string, ...config_center.Option) (string, error) {
return "", nil
}
// GetInternalProperty get value by key in Default properties file(dubbo.properties)
func (fsdc *fileSystemDynamicConfiguration) GetInternalProperty(string, ...config_center.Option) (string, error) {
return "", nil
}
// PublishConfig will publish the config with the (key, group, value) pair
func (fsdc *fileSystemDynamicConfiguration) PublishConfig(key string, group string, value string) error {
path := fsdc.buildPath(key, group)
return write(path, value)
}
func (fsdc *fileSystemDynamicConfiguration) buildPath(key string, group string) string {
return strings.Join([]string{fsdc.rootPath, group, key}, "/")
}
func write(path string, value string) error {
return nil
}
// GetConfigKeysByGroup will return all keys with the group
func (fsdc *fileSystemDynamicConfiguration) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) {
return nil, nil
}
// RemoveConfig will remove the config whit hte (key, group)
func (fsdc *fileSystemDynamicConfiguration) RemoveConfig(string, string) error {
return nil
}
func (fsdc *fileSystemDynamicConfiguration) GetConfigGroups() *gxset.HashSet {
var cg []string
filepath.Walk(fsdc.rootDirectory.Name(), func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
cg = append(cg, info.Name())
}
return nil
})
return gxset.NewSet(cg)
}
func (fsdc *fileSystemDynamicConfiguration) getRootDirectory() os.File {
return fsdc.rootDirectory
}
func (fsdc *fileSystemDynamicConfiguration) ConfigFile(key string, group string) *os.File {
return nil
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package file
import (
"fmt"
"sync"
"testing"
)
import (
"github.com/apache/dubbo-go/config_center"
)
const (
dubboPropertyFileName = "dubbo.properties"
)
func TestListener(t *testing.T) {
fsdc := &fileSystemDynamicConfiguration{
rootPath: "/Users/tc/Documents/workspace_2020/dubbo/dubbo-common/target/test-classes/config-center",
}
listener := &mockDataListener{}
listener.wg.Add(1)
fsdc.addListener("abc-def-ghi", listener)
listener.wg.Wait()
fsdc.close()
}
type mockDataListener struct {
wg sync.WaitGroup
event string
}
func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) {
fmt.Println("process!!!!!")
l.wg.Done()
l.event = configType.Key
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package file
import (
"bytes"
"errors"
"os"
"os/exec"
"os/user"
"runtime"
"strings"
"sync"
)
import (
"github.com/fsnotify/fsnotify"
)
import (
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/config_center"
)
var (
watchStartOnce = sync.Once{}
watch *fsnotify.Watcher
callback map[string]config_center.ConfigurationListener
//path = strings.Join([]string{h, ".dubbo", "registry"}, string(filepath.Separator))
)
func init() {
watch, err := fsnotify.NewWatcher()
if err != nil {
logger.Errorf("file : listen config fail, error:%v ", err)
return
}
watchStartOnce.Do(func() {
go func() {
for {
select {
case event := <-watch.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
}
if event.Op&fsnotify.Create == fsnotify.Create {
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
}
case err := <-watch.Errors:
logger.Errorf("file : listen watch fail:", err)
}
}
}()
})
}
func (fsdc *fileSystemDynamicConfiguration) addListener(key string, listener config_center.ConfigurationListener) {
path := fsdc.buildPath(key, config_center.DEFAULT_GROUP)
_, loaded := fsdc.keyListeners.Load(path)
if !loaded {
if err := watch.Add(path); err != nil {
logger.Errorf("file : listen watch: %s add fail:", key, err)
} else {
fsdc.keyListeners.Store(key, listener)
}
} else {
logger.Infof("profile:%s. this profile is already listening", key)
}
}
func (fsdc *fileSystemDynamicConfiguration) removeListener(key string, listener config_center.ConfigurationListener) {
path := fsdc.buildPath(key, config_center.DEFAULT_GROUP)
_, loaded := fsdc.keyListeners.Load(path)
if !loaded {
if err := watch.Remove(path); err != nil {
logger.Errorf("file : listen watch: %s remove fail:", key, err)
} else {
fsdc.keyListeners.Delete(path)
}
} else {
logger.Infof("profile:%s. this profile is not exist", key)
}
}
func (fsdc *fileSystemDynamicConfiguration) close() {
watch.Close()
}
// Home returns the home directory for the executing user.
//
// This uses an OS-specific method for discovering the home directory.
// An error is returned if a home directory cannot be detected.
func Home() (string, error) {
user, err := user.Current()
if nil == err {
return user.HomeDir, nil
}
// cross compile support
if "windows" == runtime.GOOS {
return homeWindows()
}
// Unix-like system, so just assume Unix
return homeUnix()
}
func homeUnix() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
// If that fails, try the shell
var stdout bytes.Buffer
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
result := strings.TrimSpace(stdout.String())
if result == "" {
return "", errors.New("blank output when reading home directory")
}
return result, nil
}
func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home := drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}
return home, nil
}
......@@ -18,6 +18,7 @@ require (
github.com/elazarl/go-bindata-assetfs v1.0.0 // indirect
github.com/emicklei/go-restful/v3 v3.0.0
github.com/frankban/quicktest v1.4.1 // indirect
github.com/fsnotify/fsnotify v1.4.7
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect
github.com/go-co-op/gocron v0.1.1
github.com/go-resty/resty/v2 v2.1.0
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment