dubbo.apache.org/dubbo-go/v3@v3.1.1/config/remote_config.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package config 19 20 import ( 21 "net/url" 22 "time" 23 ) 24 25 import ( 26 perrors "github.com/pkg/errors" 27 ) 28 29 import ( 30 "dubbo.apache.org/dubbo-go/v3/common" 31 "dubbo.apache.org/dubbo-go/v3/common/constant" 32 ) 33 34 // RemoteConfig usually we need some middleware, including nacos, zookeeper 35 // this represents an instance of this middleware 36 // so that other module, like config center, registry could reuse the config 37 // but now, only metadata report, metadata service, service discovery use this structure 38 type RemoteConfig struct { 39 Protocol string `yaml:"protocol" json:"protocol,omitempty" property:"protocol"` 40 Address string `yaml:"address" json:"address,omitempty" property:"address"` 41 Timeout string `default:"5s" yaml:"timeout" json:"timeout,omitempty" property:"timeout"` 42 Username string `yaml:"username" json:"username,omitempty" property:"username"` 43 Password string `yaml:"password" json:"password,omitempty" property:"password"` 44 Params map[string]string `yaml:"params" json:"params,omitempty"` 45 } 46 47 // Prefix dubbo.remote. 48 func (rc *RemoteConfig) Prefix() string { 49 return constant.RemotePrefix 50 } 51 52 func (rc *RemoteConfig) Init() error { 53 return nil 54 } 55 56 // GetTimeout return timeout duration. 57 // if the configure is invalid, or missing, the default value 5s will be returned 58 func (rc *RemoteConfig) GetTimeout() time.Duration { 59 if res, err := time.ParseDuration(rc.Timeout); err == nil { 60 return res 61 } 62 return 5 * time.Second 63 } 64 65 // GetParam will return the value of the key. If not found, def will be return; 66 // def => default value 67 func (rc *RemoteConfig) GetParam(key string, def string) string { 68 param, ok := rc.Params[key] 69 if !ok { 70 return def 71 } 72 return param 73 } 74 75 // ToURL config to url 76 func (rc *RemoteConfig) ToURL() (*common.URL, error) { 77 if len(rc.Protocol) == 0 { 78 return nil, perrors.Errorf("Must provide protocol in RemoteConfig.") 79 } 80 return common.NewURL(rc.Address, 81 common.WithProtocol(rc.Protocol), 82 common.WithUsername(rc.Username), 83 common.WithPassword(rc.Password), 84 common.WithLocation(rc.Address), 85 common.WithParams(rc.getUrlMap()), 86 ) 87 } 88 89 // getUrlMap get url map 90 func (rc *RemoteConfig) getUrlMap() url.Values { 91 urlMap := url.Values{} 92 urlMap.Set(constant.ConfigUsernameKey, rc.Username) 93 urlMap.Set(constant.ConfigPasswordKey, rc.Password) 94 urlMap.Set(constant.ConfigTimeoutKey, rc.Timeout) 95 urlMap.Set(constant.ClientNameKey, clientNameID(rc, rc.Protocol, rc.Protocol)) 96 97 for key, val := range rc.Params { 98 urlMap.Set(key, val) 99 } 100 return urlMap 101 } 102 103 func NewRemoteConfigBuilder() *RemoteConfigBuilder { 104 return &RemoteConfigBuilder{remoteConfig: &RemoteConfig{}} 105 } 106 107 type RemoteConfigBuilder struct { 108 remoteConfig *RemoteConfig 109 } 110 111 func (rcb *RemoteConfigBuilder) SetProtocol(protocol string) *RemoteConfigBuilder { 112 rcb.remoteConfig.Protocol = protocol 113 return rcb 114 } 115 116 func (rcb *RemoteConfigBuilder) SetAddress(address string) *RemoteConfigBuilder { 117 rcb.remoteConfig.Address = address 118 return rcb 119 } 120 121 func (rcb *RemoteConfigBuilder) SetTimeout(timeout string) *RemoteConfigBuilder { 122 rcb.remoteConfig.Timeout = timeout 123 return rcb 124 } 125 126 func (rcb *RemoteConfigBuilder) SetUsername(username string) *RemoteConfigBuilder { 127 rcb.remoteConfig.Username = username 128 return rcb 129 } 130 131 func (rcb *RemoteConfigBuilder) SetPassword(password string) *RemoteConfigBuilder { 132 rcb.remoteConfig.Password = password 133 return rcb 134 } 135 136 func (rcb *RemoteConfigBuilder) SetParams(params map[string]string) *RemoteConfigBuilder { 137 rcb.remoteConfig.Params = params 138 return rcb 139 } 140 141 func (rcb *RemoteConfigBuilder) AddParam(key, value string) *RemoteConfigBuilder { 142 if rcb.remoteConfig.Params == nil { 143 rcb.remoteConfig.Params = make(map[string]string) 144 } 145 rcb.remoteConfig.Params[key] = value 146 return rcb 147 } 148 149 func (rcb *RemoteConfigBuilder) Build() *RemoteConfig { 150 if err := rcb.remoteConfig.Init(); err != nil { 151 panic(err) 152 } 153 return rcb.remoteConfig 154 }