Skip to content

Commit

Permalink
Fix #2446, move XxxOption definition in global package to separate pa…
Browse files Browse the repository at this point in the history
…ckages (#2447)
  • Loading branch information
chickenlj authored Oct 26, 2023
1 parent 913665d commit 9d62f72
Show file tree
Hide file tree
Showing 22 changed files with 683 additions and 535 deletions.
4 changes: 2 additions & 2 deletions config/protocol_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (

// ProtocolConfig is protocol configuration
type ProtocolConfig struct {
Name string `default:"dubbo" validate:"required" yaml:"name" json:"name,omitempty" property:"name"`
Name string `default:"tri" validate:"required" yaml:"name" json:"name,omitempty" property:"name"`
Ip string `yaml:"ip" json:"ip,omitempty" property:"ip"`
Port string `default:"20000" yaml:"port" json:"port,omitempty" property:"port"`
Port string `default:"50051" yaml:"port" json:"port,omitempty" property:"port"`
Params interface{} `yaml:"params" json:"params,omitempty" property:"params"`

// MaxServerSendMsgSize max size of server send message, 1mb=1000kb=1000000b 1mib=1024kb=1048576b.
Expand Down
27 changes: 0 additions & 27 deletions config_center/dynamic_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

package config_center

import (
"time"
)

import (
gxset "github.com/dubbogo/gost/container/set"
)
Expand Down Expand Up @@ -62,29 +58,6 @@ type DynamicConfiguration interface {
GetConfigKeysByGroup(group string) (*gxset.HashSet, error)
}

// Options ...
type Options struct {
Group string
Timeout time.Duration
}

// Option ...
type Option func(*Options)

// WithGroup assigns group to opt.Group
func WithGroup(group string) Option {
return func(opt *Options) {
opt.Group = group
}
}

// WithTimeout assigns time to opt.Timeout
func WithTimeout(time time.Duration) Option {
return func(opt *Options) {
opt.Timeout = time
}
}

// GetRuleKey The format is '{interfaceName}:[version]:[group]'
func GetRuleKey(url *common.URL) string {
return url.ColonSeparatedKey()
Expand Down
21 changes: 6 additions & 15 deletions config_center/file/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,26 @@ func (fsdc *FileSystemDynamicConfiguration) SetParser(p parser.ConfigurationPars
// AddListener Add listener
func (fsdc *FileSystemDynamicConfiguration) AddListener(key string, listener config_center.ConfigurationListener,
opts ...config_center.Option) {
tmpOpts := &config_center.Options{}
for _, opt := range opts {
opt(tmpOpts)
}
tmpOpts := config_center.NewOptions(opts...)

tmpPath := fsdc.GetPath(key, tmpOpts.Group)
tmpPath := fsdc.GetPath(key, tmpOpts.Center.Group)
fsdc.cacheListener.AddListener(tmpPath, listener)
}

// RemoveListener Remove listener
func (fsdc *FileSystemDynamicConfiguration) RemoveListener(key string, listener config_center.ConfigurationListener,
opts ...config_center.Option) {
tmpOpts := &config_center.Options{}
for _, opt := range opts {
opt(tmpOpts)
}
tmpOpts := config_center.NewOptions(opts...)

tmpPath := fsdc.GetPath(key, tmpOpts.Group)
tmpPath := fsdc.GetPath(key, tmpOpts.Center.Group)
fsdc.cacheListener.RemoveListener(tmpPath, listener)
}

// GetProperties get properties file
func (fsdc *FileSystemDynamicConfiguration) GetProperties(key string, opts ...config_center.Option) (string, error) {
tmpOpts := &config_center.Options{}
for _, opt := range opts {
opt(tmpOpts)
}
tmpOpts := config_center.NewOptions(opts...)

tmpPath := fsdc.GetPath(key, tmpOpts.Group)
tmpPath := fsdc.GetPath(key, tmpOpts.Center.Group)
file, err := ioutil.ReadFile(tmpPath)
if err != nil {
return "", perrors.WithStack(err)
Expand Down
7 changes: 2 additions & 5 deletions config_center/nacos/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,8 @@ func (n *nacosDynamicConfiguration) GetConfigKeysByGroup(group string) (*gxset.H

// GetRule Get router rule
func (n *nacosDynamicConfiguration) GetRule(key string, opts ...config_center.Option) (string, error) {
tmpOpts := &config_center.Options{}
for _, opt := range opts {
opt(tmpOpts)
}
resolvedGroup := n.resolvedGroup(tmpOpts.Group)
tmpOpts := config_center.NewOptions(opts...)
resolvedGroup := n.resolvedGroup(tmpOpts.Center.Group)
content, err := n.client.Client().GetConfig(vo.ConfigParam{
DataId: key,
Group: resolvedGroup,
Expand Down
159 changes: 159 additions & 0 deletions config_center/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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 config_center

import (
"strconv"
"strings"
"time"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/constant/file"
"dubbo.apache.org/dubbo-go/v3/global"
)

type Options struct {
Center *global.CenterConfig
}

func defaultOptions() *Options {
return &Options{Center: global.DefaultCenterConfig()}
}

func NewOptions(opts ...Option) *Options {
centerOptions := defaultOptions()
for _, opt := range opts {
opt(centerOptions)
}
return centerOptions
}

type Option func(*Options)

func WithZookeeper() Option {
return func(opts *Options) {
opts.Center.Protocol = constant.ZookeeperKey
}
}

func WithNacos() Option {
return func(opts *Options) {
opts.Center.Protocol = constant.NacosKey
}
}

func WithAddress(address string) Option {
return func(opts *Options) {
if i := strings.Index(address, "://"); i > 0 {
opts.Center.Protocol = address[0:i]
}
opts.Center.Address = address
}
}

func WithDataID(id string) Option {
return func(opts *Options) {
opts.Center.DataId = id
}
}

func WithCluster(cluster string) Option {
return func(opts *Options) {
opts.Center.Cluster = cluster
}
}

func WithGroup(group string) Option {
return func(opts *Options) {
opts.Center.Group = group
}
}

func WithUsername(username string) Option {
return func(opts *Options) {
opts.Center.Username = username
}
}

func WithPassword(password string) Option {
return func(opts *Options) {
opts.Center.Password = password
}
}

func WithNamespace(namespace string) Option {
return func(opts *Options) {
opts.Center.Namespace = namespace
}
}

func WithAppID(id string) Option {
return func(opts *Options) {
opts.Center.AppID = id
}
}

func WithTimeout(timeout time.Duration) Option {
return func(opts *Options) {
opts.Center.Timeout = strconv.Itoa(int(timeout.Milliseconds()))
}
}

func WithParams(params map[string]string) Option {
return func(opts *Options) {
opts.Center.Params = params
}
}

func WithFile() Option {
return func(opts *Options) {
opts.Center.Protocol = constant.FileKey
}
}

func WithFileExtJson() Option {
return func(opts *Options) {
opts.Center.FileExtension = string(file.JSON)
}
}

func WithFileExtToml() Option {
return func(opts *Options) {
opts.Center.FileExtension = string(file.TOML)
}
}

func WithFileExtYaml() Option {
return func(opts *Options) {
opts.Center.FileExtension = string(file.YAML)
}
}

func WithFileExtYml() Option {
return func(opts *Options) {
opts.Center.FileExtension = string(file.YML)
}
}

func WithFileExtProperties() Option {
return func(opts *Options) {
opts.Center.FileExtension = string(file.PROPERTIES)
}
}
9 changes: 3 additions & 6 deletions config_center/zookeeper/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,13 @@ func (c *zookeeperDynamicConfiguration) RemoveListener(key string, listener conf
}

func (c *zookeeperDynamicConfiguration) GetProperties(key string, opts ...config_center.Option) (string, error) {
tmpOpts := &config_center.Options{}
for _, opt := range opts {
opt(tmpOpts)
}
tmpOpts := config_center.NewOptions(opts...)
/**
* when group is not null, we are getting startup configs from Config Center, for example:
* group=dubbo, key=dubbo.properties
*/
if len(tmpOpts.Group) != 0 {
key = tmpOpts.Group + "/" + key
if len(tmpOpts.Center.Group) != 0 {
key = tmpOpts.Center.Group + "/" + key
} else {
key = c.GetURL().GetParam(constant.ConfigNamespaceKey, config_center.DefaultGroup) + "/" + key
}
Expand Down
79 changes: 0 additions & 79 deletions global/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,82 +29,3 @@ func DefaultCenterConfig() *CenterConfig {
Params: make(map[string]string),
}
}

type CenterOption func(*CenterConfig)

func WithCenter_Protocol(protocol string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Protocol = protocol
}
}

func WithCenter_Address(address string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Address = address
}
}

func WithCenter_DataID(id string) CenterOption {
return func(cfg *CenterConfig) {
cfg.DataId = id
}
}

func WithCenter_Cluster(cluster string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Cluster = cluster
}
}

func WithCenter_Group(group string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Group = group
}
}

func WithCenter_Username(name string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Username = name
}
}

func WithCenter_Password(password string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Password = password
}
}

func WithCenter_Namespace(namespace string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Namespace = namespace
}
}

func WithCenter_AppID(id string) CenterOption {
return func(cfg *CenterConfig) {
cfg.AppID = id
}
}

func WithCenter_Timeout(timeout string) CenterOption {
return func(cfg *CenterConfig) {
cfg.Timeout = timeout
}
}

func WithCenter_Params(params map[string]string) CenterOption {
return func(cfg *CenterConfig) {
if cfg.Params == nil {
cfg.Params = make(map[string]string)
}
for k, v := range params {
cfg.Params[k] = v
}
}
}

func WithCenter_FileExtension(extension string) CenterOption {
return func(cfg *CenterConfig) {
cfg.FileExtension = extension
}
}
6 changes: 6 additions & 0 deletions global/custom_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

package global

// CustomConfig
//
// # Experimental
//
// Notice: This struct is EXPERIMENTAL and may be changed or removed in a
// later release.
type CustomConfig struct {
ConfigMap map[string]interface{} `yaml:"config-map" json:"config-map,omitempty" property:"config-map"`
}
Expand Down
Loading

0 comments on commit 9d62f72

Please sign in to comment.