Skip to content
Snippets Groups Projects
Commit f7d8d3ca authored by Xin.Zh's avatar Xin.Zh Committed by GitHub
Browse files

Merge pull request #113 from hxmhlt/config_center

Add:registries config option in referenceConfig & serviceConfig
parents f56227f6 70c54d59
No related branches found
No related tags found
No related merge requests found
Showing
with 121 additions and 52 deletions
...@@ -83,7 +83,34 @@ func TestLoad(t *testing.T) { ...@@ -83,7 +83,34 @@ func TestLoad(t *testing.T) {
consumerConfig = nil consumerConfig = nil
providerConfig = nil providerConfig = nil
} }
func TestWithNoRegLoad(t *testing.T) {
doInit()
doinit()
providerConfig.Services["MockService"].Registry = ""
consumerConfig.References["MockService"].Registry = ""
ms := &MockService{}
SetConsumerService(ms)
SetProviderService(ms)
extension.SetProtocol("registry", GetProtocol)
extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)
extension.SetProxyFactory("default", proxy_factory.NewDefaultProxyFactory)
Load()
assert.Equal(t, ms, GetRPCService(ms.Service()))
ms2 := &struct {
MockService
}{}
RPCService(ms2)
assert.NotEqual(t, ms2, GetRPCService(ms2.Service()))
conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
common.ServiceMap.UnRegister("mock", "MockService")
consumerConfig = nil
providerConfig = nil
}
func TestConfigLoaderWithConfigCenter(t *testing.T) { func TestConfigLoaderWithConfigCenter(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory { extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{} return &config_center.MockDynamicConfigurationFactory{}
......
...@@ -38,29 +38,27 @@ import ( ...@@ -38,29 +38,27 @@ import (
type ReferenceConfig struct { type ReferenceConfig struct {
context context.Context context context.Context
pxy *proxy.Proxy pxy *proxy.Proxy
InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"`
Check *bool `yaml:"check" json:"check,omitempty" property:"check"` Check *bool `yaml:"check" json:"check,omitempty" property:"check"`
Url string `yaml:"url" json:"url,omitempty" property:"url"` Url string `yaml:"url" json:"url,omitempty" property:"url"`
Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
Protocol string `yaml:"protocol" json:"protocol,omitempty" property:"protocol"` Protocol string `yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
//Registries []ConfigRegistry `required:"true" yaml:"registries" json:"registries,omitempty" property:"registries"` Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"`
Cluster string `yaml:"cluster" json:"cluster,omitempty" property:"cluster"` Cluster string `yaml:"cluster" json:"cluster,omitempty" property:"cluster"`
Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"` Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"`
Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"` Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"`
Group string `yaml:"group" json:"group,omitempty" property:"group"` Group string `yaml:"group" json:"group,omitempty" property:"group"`
Version string `yaml:"version" json:"version,omitempty" property:"version"` Version string `yaml:"version" json:"version,omitempty" property:"version"`
Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"`
async bool `yaml:"async" json:"async,omitempty" property:"async"` async bool `yaml:"async" json:"async,omitempty" property:"async"`
invoker protocol.Invoker invoker protocol.Invoker
urls []*common.URL urls []*common.URL
} }
func (c *ReferenceConfig) Prefix() string { func (c *ReferenceConfig) Prefix() string {
return constant.ReferenceConfigPrefix + c.InterfaceName + "." return constant.ReferenceConfigPrefix + c.InterfaceName + "."
} }
type ConfigRegistry string
func NewReferenceConfig(ctx context.Context) *ReferenceConfig { func NewReferenceConfig(ctx context.Context) *ReferenceConfig {
return &ReferenceConfig{context: ctx} return &ReferenceConfig{context: ctx}
} }
...@@ -102,7 +100,7 @@ func (refconfig *ReferenceConfig) Refer() { ...@@ -102,7 +100,7 @@ func (refconfig *ReferenceConfig) Refer() {
} }
} else { } else {
//2. assemble SubURL from register center's configuration模式 //2. assemble SubURL from register center's configuration模式
refconfig.urls = loadRegistries(consumerConfig.Registries, common.CONSUMER) refconfig.urls = loadRegistries(refconfig.Registry, consumerConfig.Registries, common.CONSUMER)
//set url to regUrls //set url to regUrls
for _, regUrl := range refconfig.urls { for _, regUrl := range refconfig.urls {
......
...@@ -80,6 +80,7 @@ func doInit() { ...@@ -80,6 +80,7 @@ func doInit() {
}, },
References: map[string]*ReferenceConfig{ References: map[string]*ReferenceConfig{
"MockService": { "MockService": {
Registry: "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2",
InterfaceName: "MockService", InterfaceName: "MockService",
Protocol: "mock", Protocol: "mock",
Cluster: "failover", Cluster: "failover",
......
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"context" "context"
"net/url" "net/url"
"strconv" "strconv"
"strings"
) )
import ( import (
...@@ -44,22 +45,42 @@ func (*RegistryConfig) Prefix() string { ...@@ -44,22 +45,42 @@ func (*RegistryConfig) Prefix() string {
return constant.RegistryConfigPrefix return constant.RegistryConfigPrefix
} }
func loadRegistries(registries map[string]*RegistryConfig, roleType common.RoleType) []*common.URL { func loadRegistries(targetRegistries string, registries map[string]*RegistryConfig, roleType common.RoleType) []*common.URL {
var urls []*common.URL var urls []*common.URL
for k, registryConf := range registries { trSlice := strings.Split(targetRegistries, ",")
url, err := common.NewURL( for k, registryConf := range registries {
context.TODO(), target := false
constant.REGISTRY_PROTOCOL+"://"+registryConf.Address,
common.WithParams(registryConf.getUrlMap(roleType)),
common.WithUsername(registryConf.Username),
common.WithPassword(registryConf.Password),
)
if err != nil { // if user not config targetRegistries,default load all
logger.Errorf("The registry id:%s url is invalid ,and will skip the registry, error: %#v", k, err) // Notice:in func "func Split(s, sep string) []string" comment : if s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.
// So we have to add the condition when targetRegistries string is not set (it will be "" when not set)
if len(trSlice) == 0 || (len(trSlice) == 1 && trSlice[0] == "") {
target = true
} else { } else {
urls = append(urls, &url) // else if user config targetRegistries
for _, tr := range trSlice {
if tr == k {
target = true
break
}
}
}
if target {
url, err := common.NewURL(
context.TODO(),
constant.REGISTRY_PROTOCOL+"://"+registryConf.Address,
common.WithParams(registryConf.getUrlMap(roleType)),
common.WithUsername(registryConf.Username),
common.WithPassword(registryConf.Password),
)
if err != nil {
logger.Errorf("The registry id:%s url is invalid ,and will skip the registry, error: %#v", k, err)
} else {
urls = append(urls, &url)
}
} }
} }
......
...@@ -43,17 +43,17 @@ import ( ...@@ -43,17 +43,17 @@ import (
type ServiceConfig struct { type ServiceConfig struct {
context context.Context context context.Context
Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` //multi protocol support, split by ',' Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` //multi protocol support, split by ','
InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"` InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"`
Registries []ConfigRegistry `required:"true" yaml:"registries" json:"registries,omitempty" property:"registries"` Registry string `yaml:"registry" json:"registry,omitempty" property:"registry"`
Cluster string `default:"failover" yaml:"cluster" json:"cluster,omitempty" property:"cluster"` Cluster string `default:"failover" yaml:"cluster" json:"cluster,omitempty" property:"cluster"`
Loadbalance string `default:"random" yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"` Loadbalance string `default:"random" yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"`
Group string `yaml:"group" json:"group,omitempty" property:"group"` Group string `yaml:"group" json:"group,omitempty" property:"group"`
Version string `yaml:"version" json:"version,omitempty" property:"version" ` Version string `yaml:"version" json:"version,omitempty" property:"version" `
Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"`
Warmup string `yaml:"warmup" json:"warmup,omitempty" property:"warmup"` Warmup string `yaml:"warmup" json:"warmup,omitempty" property:"warmup"`
Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"` Retries int64 `yaml:"retries" json:"retries,omitempty" property:"retries"`
unexported *atomic.Bool unexported *atomic.Bool
exported *atomic.Bool exported *atomic.Bool
rpcService common.RPCService rpcService common.RPCService
...@@ -88,7 +88,7 @@ func (srvconfig *ServiceConfig) Export() error { ...@@ -88,7 +88,7 @@ func (srvconfig *ServiceConfig) Export() error {
return nil return nil
} }
regUrls := loadRegistries(providerConfig.Registries, common.PROVIDER) regUrls := loadRegistries(srvconfig.Registry, providerConfig.Registries, common.PROVIDER)
urlMap := srvconfig.getUrlMap() urlMap := srvconfig.getUrlMap()
for _, proto := range loadProtocol(srvconfig.Protocol, providerConfig.Protocols) { for _, proto := range loadProtocol(srvconfig.Protocol, providerConfig.Protocols) {
......
...@@ -76,7 +76,7 @@ func doinit() { ...@@ -76,7 +76,7 @@ func doinit() {
"MockService": { "MockService": {
InterfaceName: "MockService", InterfaceName: "MockService",
Protocol: "mock", Protocol: "mock",
Registries: []ConfigRegistry{"shanghai_reg1", "shanghai_reg2", "hangzhou_reg1", "hangzhou_reg2"}, Registry: "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2",
Cluster: "failover", Cluster: "failover",
Loadbalance: "random", Loadbalance: "random",
Retries: 3, Retries: 3,
......
...@@ -19,15 +19,13 @@ application_config: ...@@ -19,15 +19,13 @@ application_config:
registries : registries :
"hangzhouzk": "hangzhouzk":
id: "hangzhouzk" protocol: "zookeeper"
type: "zookeeper"
timeout : "3s" timeout : "3s"
address: "127.0.0.1:2181" address: "127.0.0.1:2181"
username: "" username: ""
password: "" password: ""
"shanghaizk": "shanghaizk":
id: "shanghaizk" protocol: "zookeeper"
type: "zookeeper"
timeout : "3s" timeout : "3s"
address: "127.0.0.1:2182" address: "127.0.0.1:2182"
username: "" username: ""
...@@ -35,6 +33,7 @@ registries : ...@@ -35,6 +33,7 @@ registries :
references: references:
"UserProvider": "UserProvider":
registry: "hangzhouzk,shanghaizk"
filter: "" filter: ""
protocol : "dubbo" protocol : "dubbo"
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
......
...@@ -5,6 +5,7 @@ config_center: ...@@ -5,6 +5,7 @@ config_center:
address: "127.0.0.1" address: "127.0.0.1"
references: references:
"UserProvider": "UserProvider":
registry: "hangzhouzk,shanghaizk"
filter: "" filter: ""
protocol : "dubbo" protocol : "dubbo"
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
......
...@@ -12,13 +12,13 @@ application_config: ...@@ -12,13 +12,13 @@ application_config:
registries : registries :
"hangzhouzk": "hangzhouzk":
type: "zookeeper" protocol: "zookeeper"
timeout : "3s" timeout : "3s"
address: "127.0.0.1:2181" address: "127.0.0.1:2181"
username: "" username: ""
password: "" password: ""
"shanghaizk": "shanghaizk":
type: "zookeeper" protocol: "zookeeper"
timeout : "3s" timeout : "3s"
address: "127.0.0.1:2182" address: "127.0.0.1:2182"
username: "" username: ""
...@@ -27,6 +27,7 @@ registries : ...@@ -27,6 +27,7 @@ registries :
services: services:
"UserProvider": "UserProvider":
registry: "hangzhouzk,shanghaizk"
filter: "" filter: ""
protocol : "dubbo" protocol : "dubbo"
# equivalent to interface of dubbo.xml # equivalent to interface of dubbo.xml
......
...@@ -33,6 +33,8 @@ registries : ...@@ -33,6 +33,8 @@ registries :
references: references:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "dubbo" protocol : "dubbo"
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
cluster: "failover" cluster: "failover"
......
...@@ -32,6 +32,8 @@ registries : ...@@ -32,6 +32,8 @@ registries :
references: references:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "dubbo" protocol : "dubbo"
# version: "2.0" # version: "2.0"
# group: "as" # group: "as"
......
...@@ -32,6 +32,8 @@ registries : ...@@ -32,6 +32,8 @@ registries :
references: references:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "dubbo" protocol : "dubbo"
# version: "2.0" # version: "2.0"
# group: "as" # group: "as"
......
...@@ -29,6 +29,8 @@ registries : ...@@ -29,6 +29,8 @@ registries :
services: services:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "dubbo" protocol : "dubbo"
# 相当于dubbo.xml中的interface # 相当于dubbo.xml中的interface
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
......
...@@ -27,6 +27,8 @@ registries : ...@@ -27,6 +27,8 @@ registries :
services: services:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "dubbo" protocol : "dubbo"
# 相当于dubbo.xml中的interface # 相当于dubbo.xml中的interface
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
......
...@@ -27,6 +27,8 @@ registries : ...@@ -27,6 +27,8 @@ registries :
services: services:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "dubbo" protocol : "dubbo"
# 相当于dubbo.xml中的interface # 相当于dubbo.xml中的interface
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
......
...@@ -31,6 +31,8 @@ registries : ...@@ -31,6 +31,8 @@ registries :
references: references:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "jsonrpc" protocol : "jsonrpc"
# version : "2.0" # version : "2.0"
# group: "as" # group: "as"
......
...@@ -31,6 +31,8 @@ registries : ...@@ -31,6 +31,8 @@ registries :
references: references:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "jsonrpc" protocol : "jsonrpc"
# version : "2.0" # version : "2.0"
# group: "as" # group: "as"
......
...@@ -31,6 +31,8 @@ registries : ...@@ -31,6 +31,8 @@ registries :
references: references:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "jsonrpc" protocol : "jsonrpc"
# version : "2.0" # version : "2.0"
# group: "as" # group: "as"
......
...@@ -26,8 +26,10 @@ registries : ...@@ -26,8 +26,10 @@ registries :
services: services:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "jsonrpc" protocol : "jsonrpc"
# 相当于dubbo.xml中的interface # 相当于dubbo.xml中的interface
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
loadbalance: "random" loadbalance: "random"
warmup: "100" warmup: "100"
......
...@@ -26,8 +26,10 @@ registries : ...@@ -26,8 +26,10 @@ registries :
services: services:
"UserProvider": "UserProvider":
# 可以指定多个registry,使用逗号隔开;不指定默认向所有注册中心注册
registry: "hangzhouzk"
protocol : "jsonrpc" protocol : "jsonrpc"
# 相当于dubbo.xml中的interface # 相当于dubbo.xml中的interface
interface : "com.ikurento.user.UserProvider" interface : "com.ikurento.user.UserProvider"
loadbalance: "random" loadbalance: "random"
warmup: "100" warmup: "100"
...@@ -37,7 +39,6 @@ services: ...@@ -37,7 +39,6 @@ services:
retries: 1 retries: 1
loadbalance: "random" loadbalance: "random"
protocols: protocols:
#- name: "dubbo" #- name: "dubbo"
# ip : "127.0.0.1" # ip : "127.0.0.1"
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment