diff --git a/config/config_loader.go b/config/config_loader.go index e1691ce1a85d9d6269f933b9f71ca36bf2e068c6..1740efa2f0751ec2781c636c5cc9e23f61e0db2e 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -1,13 +1,22 @@ package config import ( - "gopkg.in/yaml.v2" + "fmt" "io/ioutil" "os" + "path" + "time" +) + +import ( + gxlog "github.com/AlexStocks/goext/log" + jerrors "github.com/juju/errors" + "gopkg.in/yaml.v2" ) import ( "github.com/dubbo/dubbo-go/common/constant" + "github.com/dubbo/dubbo-go/registry" ) var ( @@ -18,43 +27,49 @@ var ( // loaded comsumer & provider config from xxx.yml // Namely: dubbo.comsumer.xml & dubbo.provider.xml func init() { - // consumer - path := os.Getenv(constant.CONF_CONSUMER_FILE_PATH) - if path == "" { - return - } - file, err := ioutil.ReadFile(path) - if err != nil { - panic(err) - } + var ( + confFile string + ) - con := &ConsumerConfig{} - err = yaml.Unmarshal(file, con) - if err != nil { - panic(err) + // configure + confFile = os.Getenv(constant.CONF_CONSUMER_FILE_PATH) + if confFile == "" { + panic(fmt.Sprintf("application configure file name is nil")) } - - consumerConfig = *con - - // provider - path = os.Getenv(constant.CONF_PROVIDER_FILE_PATH) - if path == "" { - return + if path.Ext(confFile) != ".yml" { + panic(fmt.Sprintf("application configure file name{%v} suffix must be .yml", confFile)) } - file, err = ioutil.ReadFile(path) + confFileStream, err := ioutil.ReadFile(confFile) if err != nil { - panic(err) + panic(fmt.Sprintf("ioutil.ReadFile(file:%s) = error:%s", confFile, jerrors.ErrorStack(err))) } - - pro := &ProviderConfig{} - err = yaml.Unmarshal(file, pro) + err = yaml.Unmarshal(confFileStream, consumerConfig) if err != nil { - panic(err) + panic(fmt.Sprintf("yaml.Unmarshal() = error:%s", jerrors.ErrorStack(err))) } + //鍔ㄦ€佸姞杞絪ervice config end + for _, config := range consumerConfig.Registries { + if config.Timeout, err = time.ParseDuration(config.TimeoutStr); err != nil { + panic(fmt.Sprintf("time.ParseDuration(Registry_Config.Timeout:%#v) = error:%s", config.TimeoutStr, err)) + } + } + + gxlog.CInfo("config{%#v}\n", consumerConfig) + + // log + //confFile = os.Getenv(APP_LOG_CONF_FILE) + //if confFile == "" { + // panic(fmt.Sprintf("log configure file name is nil")) + // return nil + //} + //if path.Ext(confFile) != ".xml" { + // panic(fmt.Sprintf("log configure file name{%v} suffix must be .xml", confFile)) + // return nil + //} + //log.LoadConfiguration(confFile) - providerConfig = *pro } ///////////////////////// @@ -62,7 +77,32 @@ func init() { ///////////////////////// type ConsumerConfig struct { - RegistryConfigs []map[string]string `yaml:"registryConfigs" json:"registryConfigs"` + // pprof + Pprof_Enabled bool `default:"false" yaml:"pprof_enabled" json:"pprof_enabled,omitempty"` + Pprof_Port int `default:"10086" yaml:"pprof_port" json:"pprof_port,omitempty"` + + // client + Connect_Timeout string `default:"100ms" yaml:"connect_timeout" json:"connect_timeout,omitempty"` + ConnectTimeout time.Duration + + Request_Timeout string `yaml:"request_timeout" default:"5s" json:"request_timeout,omitempty"` // 500ms, 1m + RequestTimeout time.Duration + + // codec & selector & transport & registry + Selector string `default:"cache" yaml:"selector" json:"selector,omitempty"` + Selector_TTL string `default:"10m" yaml:"selector_ttl" json:"selector_ttl,omitempty"` + //client load balance algorithm + ClientLoadBalance string `default:"round_robin" yaml:"client_load_balance" json:"client_load_balance,omitempty"` + // application + Application_Config registry.ApplicationConfig `yaml:"application_config" json:"application_config,omitempty"` + Registries []RegistryConfig `yaml:"registries" json:"registries,omitempty"` + References []ReferenceConfig `yaml:"references" json:"references,omitempty"` +} + +type ReferenceConfigTmp struct { + Service string `required:"true" yaml:"service" json:"service,omitempty"` + Registries []referenceConfigRegistry `required:"true" yaml:"registries" json:"registries,omitempty"` + URLs []map[string]string } func SetConsumerConfig(c ConsumerConfig) { diff --git a/config/config_url.go b/config/config_url.go index d4e6182c2720979a573030bd8e635f693b6b192c..469a28263fd0ad4201f47099c5c5651959c0594d 100644 --- a/config/config_url.go +++ b/config/config_url.go @@ -14,12 +14,10 @@ import ( jerrors "github.com/juju/errors" ) -func init(){ +func init() { extension.SetDefaultURLExtension(NewDefaultServiceURL) } - - ////////////////////////////////////////// // service url ////////////////////////////////////////// @@ -38,15 +36,15 @@ type ConfigURL interface { Ip() string Port() string Path() string - Service()string - Methods()string + Service() string + Methods() string } type DefaultServiceURL struct { Service_ string Protocol_ string `required:"true",default:"dubbo" yaml:"protocol" json:"protocol,omitempty"` Location_ string // ip+port - Path_ string `yaml:"path" json:"path,omitempty"`// like /com.ikurento.dubbo.UserProvider3 + Path_ string `yaml:"path" json:"path,omitempty"` // like /com.ikurento.dubbo.UserProvider3 Ip_ string Port_ string Timeout_ time.Duration @@ -108,12 +106,10 @@ func NewDefaultServiceURL(urlString string) (ConfigURL, error) { } - func (c *DefaultServiceURL) Key() string { - return fmt.Sprintf("%s@%s-%s-%s-%s-%s", c.Service_, c.Protocol_,c.Group_,c.Location_,c.Version_,c.Methods_) + return fmt.Sprintf("%s@%s-%s-%s-%s-%s", c.Service_, c.Protocol_, c.Group_, c.Location_, c.Version_, c.Methods_) } - func (c *DefaultServiceURL) ConfigURLEqual(url ConfigURL) bool { if c.Key() != url.Key() { return false diff --git a/config/consumer_config.yml b/config/consumer_config.yml new file mode 100644 index 0000000000000000000000000000000000000000..e6b3df7559f5700476918802a6d0fd9ee3e489dc --- /dev/null +++ b/config/consumer_config.yml @@ -0,0 +1,47 @@ +# dubbo client yaml configure file + +# pprof +pprof_enabled : true +pprof_port : 10086 + +# client +request_timeout : "3500ms" +net_io_timeout : "2s" +retries : 1 +# connect timeout +connect_timeout : "100ms" +selector : "cache" +selector_ttl : "10m" + +client_load_balance: "round_robin" + +# application config +application_config: + organization : "ikurento.com" + name : "BDTService" + module : "dubbogo user-info client" + version : "0.0.1" + owner : "ZX" + environment : "dev" + +registries : + - id: "hangzhouzk" + type: "zookeeper" + timeout : "3s" + address: "127.0.0.1:2181" + username: "" + password: "" + + - id: "shanghaizk" + type: "zookeeper" + timeout : "3s" + address: "127.0.0.1:2181" + username: "" + password: "" + +references: + - registries : + - "hangzhouzk" + - "shanghaizk" +# protocol : "dubbo" + interface : "com.ikurento.user.UserProvider" diff --git a/config/reference_config.go b/config/reference_config.go index ca0a54778ab8d0ba79fd9ae12571093859dcd0b4..2fc85bd6d108beef652d62436c235f76642c81b2 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -1,10 +1,32 @@ package config type ReferenceConfig struct { - Service string `required:"true" yaml:"service" json:"service,omitempty"` - URLs []ConfigURL + Interface string `required:"true" yaml:"interface" json:"interface,omitempty"` + Registries []referenceConfigRegistry `required:"true" yaml:"registries" json:"registries,omitempty"` + URLs []ConfigURL `yaml:"-"` +} +type referenceConfigRegistry struct { + string } func NewReferenceConfig() *ReferenceConfig { return &ReferenceConfig{} -} \ No newline at end of file +} + +func (refconfig *ReferenceConfig) CreateProxy() { + //棣栧厛鏄痷ser specified URL, could be peer-to-peer address, or register center's address. + + //鍏舵鏄痑ssemble URL from register center's configuration妯″紡 + +} + +func (refconfig *ReferenceConfig) loadRegistries() []ConfigURL { + for _, registry := range refconfig.Registries { + for _, registryConf := range consumerConfig.Registries { + if registry.string == registryConf.Id { + + } + } + + } +} diff --git a/config/registry_config.go b/config/registry_config.go new file mode 100644 index 0000000000000000000000000000000000000000..7e41fdfd60ab56061e96424364e81e7b887832f8 --- /dev/null +++ b/config/registry_config.go @@ -0,0 +1,14 @@ +package config + +import "time" + +type RegistryConfig struct { + Id string `required:"true" yaml:"id" json:"id,omitempty"` + Address string `required:"true" yaml:"address" json:"address,omitempty"` + UserName string `yaml:"user_name" json:"user_name,omitempty"` + Password string `yaml:"password" json:"password,omitempty"` + TimeoutStr string `yaml:"timeout" default:"5s" json:"timeout,omitempty"` // unit: second + Timeout time.Duration `yaml:"-" json:"-"` + Url ConfigURL + Type string +} diff --git a/examples/jsonrpc/go-client/app/test.go b/examples/jsonrpc/go-client/app/test.go index 967d69696bd953bdc442520fe1efcb2d10827c9f..f4de4632361ea1a5bd464968e00411d95f5a9ffc 100644 --- a/examples/jsonrpc/go-client/app/test.go +++ b/examples/jsonrpc/go-client/app/test.go @@ -39,7 +39,7 @@ func testJsonrpc(clientConfig *examples.ClientConfig, userKey string, method str } // Create request - // gxlog.CInfo("jsonrpc selected service %#v", clientConfig.ServiceConfigList[serviceIdx]) + // gxlog.CInfo("jsonrpc selected service %#v", clientConfig.References[serviceIdx]) // Attention the last parameter : []UserKey{userKey} req, err = clientInvoker.HttpClient.NewRequest(clientConfig.ServiceConfigList[serviceIdx], method, []string{userKey}) diff --git a/registry/registry.go b/registry/registry.go index 2ca678a9b9d5a072d1835d994686215b393f84dc..1acbf40ca880dba0be59403f75063d4c9c711a5d 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -1,6 +1,8 @@ package registry -import "github.com/dubbo/dubbo-go/config" +import ( + "github.com/dubbo/dubbo-go/config" +) // Extension - Registry type Registry interface { diff --git a/registry/zookeeper/registry.go b/registry/zookeeper/registry.go index 1cd534f9ad6e8ec2fcd3e67ed2bd1f7703f205bd..cb289c38ae2903ad57de150dee249c0db1f78c81 100644 --- a/registry/zookeeper/registry.go +++ b/registry/zookeeper/registry.go @@ -40,17 +40,9 @@ func init() { plugins.PluggableRegistries["zookeeper"] = NewZkRegistry } -type ZkRegistryConfig struct { - Address []string `required:"true" yaml:"address" json:"address,omitempty"` - UserName string `yaml:"user_name" json:"user_name,omitempty"` - Password string `yaml:"password" json:"password,omitempty"` - TimeoutStr string `yaml:"timeout" default:"5s" json:"timeout,omitempty"` // unit: second - Timeout time.Duration `yaml:"-" json:"-"` -} - type Options struct { registry.Options - ZkRegistryConfig + config.RegistryConfig } func (o Options) ToString() string { @@ -64,9 +56,9 @@ func (Option) Name() string { return "dubbogo-zookeeper-registry-option" } -func WithRegistryConf(conf ZkRegistryConfig) Option { +func WithRegistryConf(conf config.RegistryConfig) Option { return func(o *Options) { - o.ZkRegistryConfig = conf + o.RegistryConfig = conf } } @@ -124,8 +116,8 @@ func NewZkRegistry(opts ...registry.RegistryOption) (registry.Registry, error) { r.Version = version.Version } - if r.ZkRegistryConfig.Timeout == 0 { - r.ZkRegistryConfig.Timeout = 1e9 + if r.RegistryConfig.Timeout == 0 { + r.RegistryConfig.Timeout = 1e9 } err = r.validateZookeeperClient() if err != nil { @@ -158,7 +150,8 @@ func (r *ZkRegistry) validateZookeeperClient() error { r.cltLock.Lock() defer r.cltLock.Unlock() if r.client == nil { - r.client, err = newZookeeperClient(RegistryZkClient, r.Address, r.ZkRegistryConfig.Timeout) + //in dubbp ,every registry only connect one node ,so this is []string{r.Address} + r.client, err = newZookeeperClient(RegistryZkClient, []string{r.Address}, r.RegistryConfig.Timeout) if err != nil { log.Warn("newZookeeperClient(name{%s}, zk addresss{%v}, timeout{%d}) = error{%v}", RegistryZkClient, r.Address, r.Timeout.String(), err)