@@ -19,7 +19,6 @@ package rulebase
1919
2020import (
2121 "encoding/json"
22- "fmt"
2322 "sync"
2423
2524 "github.com/golang/protobuf/jsonpb"
@@ -28,7 +27,6 @@ import (
2827
2928 "github.com/polarismesh/polaris-go/pkg/algorithm/rand"
3029 "github.com/polarismesh/polaris-go/pkg/config"
31- "github.com/polarismesh/polaris-go/pkg/log"
3230 "github.com/polarismesh/polaris-go/pkg/model"
3331 "github.com/polarismesh/polaris-go/pkg/plugin"
3432 "github.com/polarismesh/polaris-go/pkg/plugin/common"
@@ -44,6 +42,7 @@ type RuleBasedInstancesFilter struct {
4442 recoverAll bool
4543 prioritySubsetPool * sync.Pool
4644 systemCfg config.SystemConfig
45+ routerConf * RuleRouterConfig
4746}
4847
4948// Type 插件类型
@@ -66,6 +65,10 @@ func (g *RuleBasedInstancesFilter) Init(ctx *plugin.InitContext) error {
6665 g .valueCtx = ctx .ValueCtx
6766 g .prioritySubsetPool = & sync.Pool {}
6867 g .systemCfg = ctx .Config .GetGlobal ().GetSystem ()
68+ routerConf := ctx .Config .GetConsumer ().GetServiceRouter ().GetPluginConfig (g .Name ())
69+ if routerConf != nil {
70+ g .routerConf = routerConf .(* RuleRouterConfig )
71+ }
6972 return nil
7073}
7174
@@ -144,27 +147,20 @@ finally:
144147 case dstRuleSuccess :
145148 targetCluster = dstFilteredInstances
146149 default :
147- checkRule := routeInfo .DestService .GetNamespace () + ":" + routeInfo .DestService .GetService ()
148- if ruleStatus == sourceRuleFail {
149- checkRule = routeInfo .SourceService .GetNamespace () + ":" + routeInfo .SourceService .GetService ()
150+ failoverType := routeInfo .FailOverType
151+ if failoverType == nil {
152+ failoverType = & g .routerConf .failoverType
153+ }
154+ if * failoverType == servicerouter .FailOverNone {
155+ emptyCluster := model .NewServiceClusters (model .NewDefaultServiceInstancesWithRegistryValue (model.ServiceInfo {
156+ Service : withinCluster .GetClusters ().GetServiceInstances ().GetService (),
157+ Namespace : withinCluster .GetClusters ().GetServiceInstances ().GetNamespace (),
158+ Metadata : withinCluster .GetClusters ().GetServiceInstances ().GetMetadata (),
159+ }, withinCluster .GetClusters ().GetServiceInstances (), []model.Instance {}))
160+ targetCluster = model .NewCluster (emptyCluster , withinCluster )
161+ } else {
162+ targetCluster = model .NewCluster (clusters , withinCluster )
150163 }
151- // 如果规则匹配失败, 返回错误
152- notMatchedSrcText := getSourcesText (summary .notMatchedSources )
153- matchedSrcText := getSourcesText (summary .matchedSource )
154- invalidRegexSourceText := getSourcesText (summary .invalidRegexSources )
155- notMatchedDstText := getNotMatchedDestinationText (summary .notMatchedDestinations )
156- invalidRegexDstText := getNotMatchedDestinationText (summary .invalidRegexDestinations )
157- weightZeroDstText := getNotMatchedDestinationText (summary .weightZeroDestinations )
158- regexCompileErrText := getErrorRegexText (summary .errorRegexes )
159- errorText := fmt .Sprintf ("route rule not match, rule status: %s, sourceService %s, used variables %v," +
160- " dstService %s, notMatchedSource is %s, invalidRegexSource is %s, matchedSource is %s," +
161- " notMatchedDestination is %s, invalidRegexDestination is %s, zeroWeightDestination is %s," +
162- " regexCompileErrors is %s, please check your route rule of service %s" ,
163- ruleStatus .String (), model .ToStringService (routeInfo .SourceService , true ), routeInfo .EnvironmentVariables ,
164- model .ToStringService (routeInfo .DestService , false ), notMatchedSrcText , invalidRegexSourceText ,
165- matchedSrcText , notMatchedDstText , invalidRegexDstText , weightZeroDstText , regexCompileErrText , checkRule )
166- log .GetBaseLogger ().Errorf (errorText )
167- return nil , model .NewSDKError (model .ErrCodeRouteRuleNotMatch , nil , errorText )
168164 }
169165 result := servicerouter .PoolGetRouteResult (g .valueCtx )
170166 result .OutputCluster = targetCluster
@@ -237,3 +233,25 @@ func checkRouteRule(routeRule model.ServiceRule) error {
237233func init () {
238234 plugin .RegisterPlugin (& RuleBasedInstancesFilter {})
239235}
236+
237+ func init () {
238+ plugin .RegisterConfigurablePlugin (& RuleBasedInstancesFilter {}, & RuleRouterConfig {})
239+ }
240+
241+ type RuleRouterConfig struct {
242+ failoverType servicerouter.FailOverType
243+ FailoverType string `yaml:"failoverType"`
244+ }
245+
246+ // Verify 校验配置是否OK
247+ func (rc * RuleRouterConfig ) Verify () error {
248+ return nil
249+ }
250+
251+ // SetDefault 对关键值设置默认值
252+ func (rc * RuleRouterConfig ) SetDefault () {
253+ rc .failoverType = servicerouter .FailOverAll
254+ if rc .FailoverType == "none" {
255+ rc .failoverType = servicerouter .FailOverNone
256+ }
257+ }
0 commit comments