diff --git a/config/consumer_config.go b/config/consumer_config.go
index 5d3aec18e91ab3d9284c00fac3838d16414f2755..67d767896d7edab38e2868de632aa0b53d52a9bb 100644
--- a/config/consumer_config.go
+++ b/config/consumer_config.go
@@ -52,6 +52,7 @@ type ConsumerConfig struct {
Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"`
References map[string]*ReferenceConfig `yaml:"references" json:"references,omitempty" property:"references"`
ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf"`
+ FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" `
}
func (*ConsumerConfig) Prefix() string {
diff --git a/config/provider_config.go b/config/provider_config.go
index fc7a4d50d2ede6c3a64dade9c90914e3b5d51779..780f48f2352abc33ccf35103a4d1300880654eef 100644
--- a/config/provider_config.go
+++ b/config/provider_config.go
@@ -46,6 +46,7 @@ type ProviderConfig struct {
Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"`
Protocols map[string]*ProtocolConfig `yaml:"protocols" json:"protocols,omitempty" property:"protocols"`
ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf" `
+ FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" `
}
func (*ProviderConfig) Prefix() string {
diff --git a/filter/impl/hystrix_filter.go b/filter/impl/hystrix_filter.go
index ca7d77c21e34b73d8750badca85aad394069feee..0c4b909995c5517db48e7bd9ea762c83a14f1a87 100644
--- a/filter/impl/hystrix_filter.go
+++ b/filter/impl/hystrix_filter.go
@@ -5,13 +5,11 @@ import (
"github.com/afex/hystrix-go/hystrix"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
+ "github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/filter"
"github.com/apache/dubbo-go/protocol"
perrors "github.com/pkg/errors"
"gopkg.in/yaml.v2"
- "io/ioutil"
- "os"
- "path"
)
@@ -21,7 +19,6 @@ const (
MAXCONCURRENTREQUESTS_KEY="maxconcurrentrequests"
SLEEPWINDOW_KEY="sleepwindow"
ERRORPERCENTTHRESHOLD_KEY="errorpercentthreshold"
- CONF_HYSTRIXFILTER_FILE_PATH="CONF_HYSTRIXFILTER_FILE_PATH"
)
@@ -32,11 +29,9 @@ var (
//SleepWindow
//ErrorPercentThreshold
isConfigLoaded = false
-
+ conf = &HystrixFilterConfig{}
//
- methodLevelConfigMap = make(map[string]hystrix.CommandConfig)
- serviceLevelConfigMap = make(map[string]hystrix.CommandConfig)
- defaultConfig hystrix.CommandConfig
+
)
@@ -60,9 +55,7 @@ func (hf *HystrixFilter) Invoke(invoker protocol.Invoker, invocation protocol.In
// Do the configuration if the circuit breaker is created for the first time
if ifNew {
- hystrix.ConfigureCommand(cmdName,hystrix.CommandConfig{
-
- })
+ hystrix.ConfigureCommand(cmdName,getConfig(invoker.GetUrl().Service(),invocation.MethodName()))
}
logger.Infof("[Hystrix Filter]Using hystrix filter: %s",cmdName)
@@ -91,7 +84,6 @@ func GetHystrixFilter() filter.Filter{
if err:=initHystrixConfig();err!=nil{
logger.Warnf("[Hystrix Filter]Config load failed, error is: %v , will use default",err)
}
-
isConfigLoaded=true
}
@@ -99,12 +91,51 @@ func GetHystrixFilter() filter.Filter{
return &HystrixFilter{}
}
+func getConfig(service string, method string) hystrix.CommandConfig{
+ //Find method level config
+ getConf:=conf.Configs[conf.Services[service].Methods[method]]
+ if getConf!=nil{
+ logger.Infof("[Hystrix Filter]Found method-level config for %s - %s",service,method)
+ return *getConf
+ }
+ //Find service level config
+ getConf=conf.Configs[conf.Services[service].ServiceConfig]
+ if getConf!=nil{
+ logger.Infof("[Hystrix Filter]Found service-level config for %s - %s",service,method)
+ return *getConf
+ }
+ //Find default config
+ getConf=conf.Configs[conf.Default]
+ if getConf!=nil{
+ logger.Infof("[Hystrix Filter]Found global default config for %s - %s",service,method)
+ return *getConf
+ }
+ getConf=&hystrix.CommandConfig{}
+ logger.Infof("[Hystrix Filter]No config found for %s - %s, using default",service,method)
+ return *getConf
+}
+
+func initHystrixConfig() error{
+ filterConfig := config.GetConsumerConfig().FilterConf.(map[interface{}]interface{})[HYSTRIX]
+ if filterConfig ==nil{
+ return perrors.Errorf("no config for hystrix")
+ }
+ hystrixConfByte, err := yaml.Marshal(filterConfig)
+ if err != nil {
+ panic(err)
+ }
+ err = yaml.Unmarshal(hystrixConfByte, conf)
+ if err != nil {
+ panic(err)
+ }
+ return nil
+}
type HystrixFilterConfig struct {
- Configs map[string] hystrix.CommandConfig
+ Configs map[string] *hystrix.CommandConfig
Default string
Services map[string] ServiceHystrixConfig
}
@@ -112,22 +143,4 @@ type ServiceHystrixConfig struct{
ServiceConfig string `yaml:"service_config,omitempty"`
Methods map[string]string
}
-func initHystrixConfig() error{
- confHystrixFile := os.Getenv(CONF_HYSTRIXFILTER_FILE_PATH)
- if confHystrixFile==""{
- return perrors.Errorf("hystrix filter config file is nil")
- }
- if path.Ext(confHystrixFile) != ".yml"{
- return perrors.Errorf("hystrix filter config file suffix must be .yml")
- }
- confStream, err := ioutil.ReadFile(confHystrixFile)
- if err != nil {
- return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confHystrixFile, perrors.WithStack(err))
- }
- hystrixConfig:=&HystrixFilterConfig{}
- if err = yaml.Unmarshal(confStream,hystrixConfig);err!=nil{
- return perrors.Errorf("yaml.Unmarshal() = error:%v", perrors.WithStack(err))
- }
- return nil
-}