dubbo.apache.org/dubbo-go/v3@v3.1.1/config/router_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 "github.com/creasty/defaults" 22 ) 23 24 import ( 25 _ "dubbo.apache.org/dubbo-go/v3/cluster/router/chain" 26 "dubbo.apache.org/dubbo-go/v3/common" 27 "dubbo.apache.org/dubbo-go/v3/common/constant" 28 _ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus" 29 ) 30 31 // RouterConfig is the configuration of the router. 32 type RouterConfig struct { 33 Scope string `validate:"required" yaml:"scope" json:"scope,omitempty" property:"scope"` // must be chosen from `service` and `application`. 34 Key string `validate:"required" yaml:"key" json:"key,omitempty" property:"key"` // specifies which service or application the rule body acts on. 35 Force *bool `default:"false" yaml:"force" json:"force,omitempty" property:"force"` 36 Runtime *bool `default:"false" yaml:"runtime" json:"runtime,omitempty" property:"runtime"` 37 Enabled *bool `default:"true" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` 38 Valid *bool `default:"true" yaml:"valid" json:"valid,omitempty" property:"valid"` 39 Priority int `default:"0" yaml:"priority" json:"priority,omitempty" property:"priority"` 40 Conditions []string `yaml:"conditions" json:"conditions,omitempty" property:"conditions"` 41 Tags []Tag `yaml:"tags" json:"tags,omitempty" property:"tags"` 42 } 43 44 type Tag struct { 45 Name string `yaml:"name" json:"name,omitempty" property:"name"` 46 Match []*common.ParamMatch `yaml:"match" json:"match,omitempty" property:"match"` 47 Addresses []string `yaml:"addresses" json:"addresses,omitempty" property:"addresses"` 48 } 49 50 // Prefix dubbo.router 51 func (RouterConfig) Prefix() string { 52 return constant.RouterConfigPrefix 53 } 54 55 func (c *RouterConfig) Init() error { 56 if err := defaults.Set(c); err != nil { 57 return err 58 } 59 return verify(c) 60 } 61 62 func initRouterConfig(rc *RootConfig) error { 63 routers := rc.Router 64 if len(routers) > 0 { 65 for _, r := range routers { 66 if err := r.Init(); err != nil { 67 return err 68 } 69 } 70 rc.Router = routers 71 } 72 73 //chain.SetVSAndDRConfigByte(vsBytes, drBytes) 74 return nil 75 } 76 77 type RouterConfigBuilder struct { 78 routerConfig *RouterConfig 79 } 80 81 func NewRouterConfigBuilder() *RouterConfigBuilder { 82 return &RouterConfigBuilder{routerConfig: &RouterConfig{}} 83 } 84 85 func (rcb *RouterConfigBuilder) SetScope(scope string) *RouterConfigBuilder { 86 rcb.routerConfig.Scope = scope 87 return rcb 88 } 89 90 func (rcb *RouterConfigBuilder) SetKey(key string) *RouterConfigBuilder { 91 rcb.routerConfig.Key = key 92 return rcb 93 } 94 95 func (rcb *RouterConfigBuilder) SetForce(force bool) *RouterConfigBuilder { 96 rcb.routerConfig.Force = &force 97 return rcb 98 } 99 100 func (rcb *RouterConfigBuilder) SetRuntime(runtime bool) *RouterConfigBuilder { 101 rcb.routerConfig.Runtime = &runtime 102 return rcb 103 } 104 105 func (rcb *RouterConfigBuilder) SetEnabled(enabled bool) *RouterConfigBuilder { 106 rcb.routerConfig.Enabled = &enabled 107 return rcb 108 } 109 110 func (rcb *RouterConfigBuilder) SetValid(valid bool) *RouterConfigBuilder { 111 rcb.routerConfig.Valid = &valid 112 return rcb 113 } 114 115 func (rcb *RouterConfigBuilder) SetPriority(priority int) *RouterConfigBuilder { 116 rcb.routerConfig.Priority = priority 117 return rcb 118 } 119 120 func (rcb *RouterConfigBuilder) SetConditions(conditions []string) *RouterConfigBuilder { 121 rcb.routerConfig.Conditions = conditions 122 return rcb 123 } 124 125 func (rcb *RouterConfigBuilder) AddCondition(condition string) *RouterConfigBuilder { 126 if rcb.routerConfig.Conditions == nil { 127 rcb.routerConfig.Conditions = make([]string, 0) 128 } 129 rcb.routerConfig.Conditions = append(rcb.routerConfig.Conditions, condition) 130 return rcb 131 } 132 133 func (rcb *RouterConfigBuilder) SetTags(tags []Tag) *RouterConfigBuilder { 134 rcb.routerConfig.Tags = tags 135 return rcb 136 } 137 138 func (rcb *RouterConfigBuilder) AddTag(tag Tag) *RouterConfigBuilder { 139 if rcb.routerConfig.Tags == nil { 140 rcb.routerConfig.Tags = make([]Tag, 0) 141 } 142 rcb.routerConfig.Tags = append(rcb.routerConfig.Tags, tag) 143 return rcb 144 } 145 146 func (rcb *RouterConfigBuilder) Build() *RouterConfig { 147 if err := rcb.routerConfig.Init(); err != nil { 148 panic(err) 149 } 150 return rcb.routerConfig 151 }