Skip to content

Commit

Permalink
Merge pull request #54 from lepdou/config
Browse files Browse the repository at this point in the history
support dynamic config
  • Loading branch information
andrewshan authored May 7, 2022
2 parents 873a6ae + c6f6c47 commit f4d6b1d
Show file tree
Hide file tree
Showing 45 changed files with 4,671 additions and 7 deletions.
11 changes: 11 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,14 @@ type LimitAPI interface {
func NewQuotaRequest() QuotaRequest {
return &model.QuotaRequestImpl{}
}

//config

type ConfigFile model.ConfigFile

// ConfigAPI 配置文件的 API
type ConfigAPI interface {
api.SDKOwner
// GetConfigFile 获取配置文件
GetConfigFile(namespace, fileGroup, fileName string) (ConfigFile, error)
}
38 changes: 38 additions & 0 deletions api/config_file.go
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
)
63 changes: 63 additions & 0 deletions api/config_file_impl.go
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
}
78 changes: 78 additions & 0 deletions api_config.go
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()
}
65 changes: 65 additions & 0 deletions examples/configuration/main.go
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))
}
8 changes: 8 additions & 0 deletions examples/configuration/polaris.yaml
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
2 changes: 1 addition & 1 deletion examples/quickstart/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (svr *PolarisConsumer) runWebServer() {
_, _ = rw.Write([]byte(fmt.Sprintf("[errot] send request to %s:%d fail : %s", instance.GetHost(), instance.GetPort(), err)))
return
}
delay := time.Now().Add(time.Duration(10 * time.Second)).Sub(start)
delay := time.Now().Sub(start)

ret := &polaris.ServiceCallResult{
ServiceCallResult: model.ServiceCallResult{
Expand Down
23 changes: 23 additions & 0 deletions pkg/config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ type ProviderConfig interface {
GetRateLimit() RateLimitConfig
}

// ConfigFileConfig 配置中心的配置
type ConfigFileConfig interface {
BaseConfig
// IsEnable 是否启用配置中心
IsEnable() bool
// GetConfigConnectorConfig 配置文件连接器
GetConfigConnectorConfig() ConfigConnectorConfig
// GetPropertiesValueCacheSize 值缓存的最大数量
GetPropertiesValueCacheSize() int32
// GetPropertiesValueExpireTime 缓存的过期时间,默认为 60s
GetPropertiesValueExpireTime() int64
}

// RateLimitConfig 限流相关配置
type RateLimitConfig interface {
BaseConfig
Expand Down Expand Up @@ -407,6 +420,8 @@ type Configuration interface {
GetConsumer() ConsumerConfig
// GetProvider provider前缀开头的所有配置项
GetProvider() ProviderConfig
// GetConfigFile config前缀开头的所有配置项
GetConfigFile() ConfigFileConfig
}

// When when to active health check
Expand Down Expand Up @@ -465,3 +480,11 @@ type ServiceSpecificConfig interface {

GetServiceRouter() ServiceRouterConfig
}

// ConfigConnectorConfig 配置中心连接相关的配置
type ConfigConnectorConfig interface {
ServerConnectorConfig

// GetConnectorType 后端服务器类型,默认是 polaris
GetConnectorType() string
}
Loading

0 comments on commit f4d6b1d

Please sign in to comment.