-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from lepdou/config
support dynamic config
- Loading branch information
Showing
45 changed files
with
4,671 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Tencent is pleased to support the open source community by making Polaris available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* Licensed under the BSD 3-Clause License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* 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 api | ||
|
||
import "github.com/polarismesh/polaris-go/pkg/model" | ||
|
||
// ConfigFileAPI 配置文件的 API | ||
type ConfigFileAPI interface { | ||
SDKOwner | ||
// GetConfigFile 获取配置文件 | ||
GetConfigFile(namespace, fileGroup, fileName string) (model.ConfigFile, error) | ||
} | ||
|
||
var ( | ||
// NewConfigFileAPIBySDKContext 通过 SDKContext 创建 ConfigFileAPI | ||
NewConfigFileAPIBySDKContext = newConfigFileAPIBySDKContext | ||
// NewConfigFileAPI 通过 polaris.yaml 创建 ConfigFileAPI | ||
NewConfigFileAPI = newConfigFileAPI | ||
// NewConfigFileAPIByConfig 通过 Configuration 创建 ConfigFileAPI | ||
NewConfigFileAPIByConfig = newConfigFileAPIByConfig | ||
// NewConfigFileAPIByFile 通过配置文件创建 ConfigFileAPI | ||
NewConfigFileAPIByFile = newConfigFileAPIByFile | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Tencent is pleased to support the open source community by making Polaris available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* Licensed under the BSD 3-Clause License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* 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 api | ||
|
||
import ( | ||
"github.com/polarismesh/polaris-go/pkg/config" | ||
"github.com/polarismesh/polaris-go/pkg/model" | ||
) | ||
|
||
type configFileAPI struct { | ||
context SDKContext | ||
} | ||
|
||
func newConfigFileAPI() (ConfigFileAPI, error) { | ||
return newConfigFileAPIByConfig(config.NewDefaultConfigurationWithDomain()) | ||
} | ||
|
||
func newConfigFileAPIByConfig(cfg config.Configuration) (ConfigFileAPI, error) { | ||
context, err := InitContextByConfig(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &configFileAPI{context}, nil | ||
} | ||
|
||
func newConfigFileAPIByFile(path string) (ConfigFileAPI, error) { | ||
context, err := InitContextByFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &configFileAPI{context}, nil | ||
} | ||
|
||
func newConfigFileAPIBySDKContext(context SDKContext) ConfigFileAPI { | ||
return &configFileAPI{ | ||
context: context, | ||
} | ||
} | ||
|
||
// GetConfigFile 获取配置文件 | ||
func (c *configFileAPI) GetConfigFile(namespace, fileGroup, fileName string) (model.ConfigFile, error) { | ||
return c.context.GetEngine().SyncGetConfigFile(namespace, fileGroup, fileName) | ||
} | ||
|
||
// SDKContext 获取SDK上下文 | ||
func (c *configFileAPI) SDKContext() SDKContext { | ||
return c.context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Tencent is pleased to support the open source community by making Polaris available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* Licensed under the BSD 3-Clause License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* 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 polaris | ||
|
||
import ( | ||
"github.com/polarismesh/polaris-go/api" | ||
"github.com/polarismesh/polaris-go/pkg/config" | ||
) | ||
|
||
type configAPI struct { | ||
rawAPI api.ConfigFileAPI | ||
} | ||
|
||
// NewConfigAPI 获取配置中心 API | ||
func NewConfigAPI() (ConfigAPI, error) { | ||
rawAPI, err := api.NewConfigFileAPI() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &configAPI{ | ||
rawAPI: rawAPI, | ||
}, nil | ||
} | ||
|
||
// NewConfigAPIByConfig 通过配置对象获取配置中心 API | ||
func NewConfigAPIByConfig(cfg config.Configuration) (ConfigAPI, error) { | ||
rawAPI, err := api.NewConfigFileAPIByConfig(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &configAPI{ | ||
rawAPI: rawAPI, | ||
}, nil | ||
} | ||
|
||
// NewConfigAPIByFile 通过配置文件获取配置中心 API | ||
func NewConfigAPIByFile(path string) (ConfigAPI, error) { | ||
rawAPI, err := api.NewConfigFileAPIByFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &configAPI{ | ||
rawAPI: rawAPI, | ||
}, nil | ||
} | ||
|
||
// NewConfigAPIByContext 通过上下文对象获取配置中心 API | ||
func NewConfigAPIByContext(context api.SDKContext) ConfigAPI { | ||
rawAPI := api.NewConfigFileAPIBySDKContext(context) | ||
return &configAPI{ | ||
rawAPI: rawAPI, | ||
} | ||
} | ||
|
||
// GetConfigFile 获取配置文件 | ||
func (c *configAPI) GetConfigFile(namespace, fileGroup, fileName string) (ConfigFile, error) { | ||
return c.rawAPI.GetConfigFile(namespace, fileGroup, fileName) | ||
} | ||
|
||
// SDKContext 获取SDK上下文 | ||
func (c *configAPI) SDKContext() api.SDKContext { | ||
return c.rawAPI.SDKContext() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Tencent is pleased to support the open source community by making polaris-go available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* Licensed under the BSD 3-Clause License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
* | ||
* 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 main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/polarismesh/polaris-go" | ||
"github.com/polarismesh/polaris-go/pkg/model" | ||
) | ||
|
||
func main() { | ||
configAPI, err := polaris.NewConfigAPI() | ||
|
||
if err != nil { | ||
fmt.Println("fail to start example.", err) | ||
return | ||
} | ||
|
||
//获取远程的配置文件 | ||
namespace := "default" | ||
fileGroup := "polaris-config-example" | ||
fileName := "example.yaml" | ||
|
||
configFile, err := configAPI.GetConfigFile(namespace, fileGroup, fileName) | ||
if err != nil { | ||
fmt.Println("fail to get config.", err) | ||
return | ||
} | ||
|
||
// 打印配置文件内容 | ||
fmt.Println(configFile.GetContent()) | ||
|
||
// 方式一:添加监听器 | ||
configFile.AddChangeListener(changeListener) | ||
|
||
//方式二:添加监听器 | ||
changeChan := make(chan model.ConfigFileChangeEvent) | ||
configFile.AddChangeListenerWithChannel(changeChan) | ||
|
||
for { | ||
select { | ||
case event := <-changeChan: | ||
fmt.Println(fmt.Sprintf("received change event by channel. %+v", event)) | ||
} | ||
} | ||
} | ||
|
||
func changeListener(event model.ConfigFileChangeEvent) { | ||
fmt.Println(fmt.Sprintf("received change event. %+v", event)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
global: | ||
serverConnector: | ||
addresses: | ||
- 127.0.0.1:8091 | ||
config: | ||
configConnector: | ||
addresses: | ||
- 127.0.0.1:8093 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.