From 7480913cb0bcbab7ad4f591846c589f4c7bc1c47 Mon Sep 17 00:00:00 2001 From: zhaokaiwei <1186713932@qq.com> Date: Fri, 17 Sep 2021 21:10:10 +0800 Subject: [PATCH 1/3] simplify config for pixiu --- go.mod | 1 + go.sum | 2 + pkg/config/config_load.go | 4 + pkg/model/base.go | 6 +- pkg/model/bootstrap.go | 6 +- pkg/model/cluster.go | 12 +- samples/dubbogo/simple/body/pixiu/conf.yaml | 154 ++++++++++++-------- 7 files changed, 113 insertions(+), 72 deletions(-) diff --git a/go.mod b/go.mod index 51a875f70..a4ce56c38 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/alibaba/sentinel-golang v1.0.2 github.com/apache/dubbo-go v1.5.7-rc2 github.com/apache/dubbo-go-hessian2 v1.9.3 + github.com/creasty/defaults v1.5.2 github.com/dubbogo/dubbo-go-pixiu-filter v0.1.4 github.com/dubbogo/go-zookeeper v1.0.3 github.com/dubbogo/gost v1.11.14 diff --git a/go.sum b/go.sum index 2220eed34..8d0b19a57 100644 --- a/go.sum +++ b/go.sum @@ -188,6 +188,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creasty/defaults v1.5.1 h1:j8WexcS3d/t4ZmllX4GEkl4wIB/trOr035ajcLHCISM= github.com/creasty/defaults v1.5.1/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY= +github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls= +github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY= github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/pkg/config/config_load.go b/pkg/config/config_load.go index ce28745de..dd5d4e780 100644 --- a/pkg/config/config_load.go +++ b/pkg/config/config_load.go @@ -18,6 +18,7 @@ package config import ( + "github.com/creasty/defaults" "io/ioutil" "log" "path/filepath" @@ -87,6 +88,9 @@ func LoadYAMLConfig(path string) *model.Bootstrap { if err != nil { log.Fatalln("[config] [yaml load] convert YAML to JSON failed, ", err) } + if err = defaults.Set(cfg); err != nil { + log.Fatalln("[config] [yaml load] initialize structs with default value failed, ", err) + } err = Adapter(cfg) if err != nil { log.Fatalln("[config] [yaml load] yaml unmarshal config failed, ", err) diff --git a/pkg/model/base.go b/pkg/model/base.go index 26776a7af..766a5e035 100644 --- a/pkg/model/base.go +++ b/pkg/model/base.go @@ -117,9 +117,9 @@ type ( // management servers SocketAddress struct { ProtocolStr string `yaml:"protocol_type" json:"protocol_type" mapstructure:"protocol_type"` - Protocol ProtocolType `yaml:"omitempty" json:"omitempty"` - Address string `yaml:"address" json:"address" mapstructure:"address"` - Port int `yaml:"port" json:"port" mapstructure:"port"` + Protocol ProtocolType `default:"http" yaml:"omitempty" json:"omitempty"` + Address string `default:"0.0.0.0" yaml:"address" json:"address" mapstructure:"address"` + Port int `default:"8881" yaml:"port" json:"port" mapstructure:"port"` ResolverName string `yaml:"resolver_name" json:"resolver_name" mapstructure:"resolver_name"` } diff --git a/pkg/model/bootstrap.go b/pkg/model/bootstrap.go index 52b415b67..e54746f77 100644 --- a/pkg/model/bootstrap.go +++ b/pkg/model/bootstrap.go @@ -69,7 +69,7 @@ type DynamicResources struct{} type ShutdownConfig struct { Timeout string `default:"60s" yaml:"timeout" json:"timeout,omitempty"` StepTimeout string `default:"10s" yaml:"step_timeout" json:"step_timeout,omitempty"` - RejectPolicy string `yaml:"reject_policy" json:"reject_policy,omitempty"` + RejectPolicy string `default:"immediacy" yaml:"reject_policy" json:"reject_policy,omitempty"` } // APIMetaConfig how to find api config, file or etcd etc. @@ -80,6 +80,6 @@ type APIMetaConfig struct { // TimeoutConfig the config of ConnectTimeout and RequestTimeout type TimeoutConfig struct { - ConnectTimeoutStr string `yaml:"connect_timeout" json:"connect_timeout,omitempty"` // ConnectTimeout timeout for connect to cluster node - RequestTimeoutStr string `yaml:"request_timeout" json:"request_timeout,omitempty"` + ConnectTimeoutStr string `default:"5s" yaml:"connect_timeout" json:"connect_timeout,omitempty"` // ConnectTimeout timeout for connect to cluster node + RequestTimeoutStr string `default:"10s" yaml:"request_timeout" json:"request_timeout,omitempty"` } diff --git a/pkg/model/cluster.go b/pkg/model/cluster.go index ae01e9f8b..5704e2ef3 100644 --- a/pkg/model/cluster.go +++ b/pkg/model/cluster.go @@ -50,12 +50,12 @@ var ( type ( // Cluster a single upstream cluster Cluster struct { - Name string `yaml:"name" json:"name"` // Name the cluster unique name - TypeStr string `yaml:"type" json:"type"` // Type the cluster discovery type string value - Type DiscoveryType `yaml:",omitempty" json:",omitempty"` // Type the cluster discovery type + Name string `yaml:"name" json:"name" default:"test_dubbo"` // Name the cluster unique name + TypeStr string `yaml:"type" json:"type"` // Type the cluster discovery type string value + Type DiscoveryType `yaml:",omitempty" json:",omitempty"` // Type the cluster discovery type EdsClusterConfig EdsClusterConfig `yaml:"eds_cluster_config" json:"eds_cluster_config" mapstructure:"eds_cluster_config"` - LbStr string `yaml:"lb_policy" json:"lb_policy"` // Lb the cluster select node used loadBalance policy - Lb LbPolicy `yaml:",omitempty" json:",omitempty"` // Lb the cluster select node used loadBalance policy + LbStr string `yaml:"lb_policy" json:"lb_policy" default:"RoundRobin"` // Lb the cluster select node used loadBalance policy + Lb LbPolicy `yaml:",omitempty" json:",omitempty"` // Lb the cluster select node used loadBalance policy HealthChecks []HealthCheck `yaml:"health_checks" json:"health_checks"` Endpoints []*Endpoint `yaml:"endpoints" json:"endpoints"` } @@ -68,7 +68,7 @@ type ( // Registry remote registry where dubbo apis are registered. Registry struct { - Protocol string `yaml:"protocol" json:"protocol" default:"zookeeper"` + Protocol string `yaml:"protocol" json:"protocol"` Timeout string `yaml:"timeout" json:"timeout"` Address string `yaml:"address" json:"address"` Username string `yaml:"username" json:"username"` diff --git a/samples/dubbogo/simple/body/pixiu/conf.yaml b/samples/dubbogo/simple/body/pixiu/conf.yaml index 0dec7c157..1b6f11f2d 100644 --- a/samples/dubbogo/simple/body/pixiu/conf.yaml +++ b/samples/dubbogo/simple/body/pixiu/conf.yaml @@ -20,64 +20,98 @@ static_resources: listeners: - name: "net/http" - address: - socket_address: - protocol_type: "HTTP" - address: "0.0.0.0" - port: 8881 filter_chains: - - filter_chain_match: - domains: - - api.dubbo.com - - api.pixiu.com - filters: - - name: dgp.filter.httpconnectionmanager - config: - route_config: - routes: - - match: - prefix: "/api/v1" - route: - cluster: "test-dubbo" - cluster_not_found_response_code: 505 - http_filters: - - name: dgp.filter.http.apiconfig - config: - path: $PROJECT_DIR/pixiu/api_config.yaml - - name: dgp.filter.http.dubboproxy - config: - dubboProxyConfig: - registries: - "zookeeper": - protocol: "zookeeper" - timeout: "3s" - address: "127.0.0.1:2181" - username: "" - password: "" - timeout_config: - connect_timeout: 5s - request_timeout: 5s - - name: dgp.filter.http.response - config: - server_name: "test_http_dubbo" - generate_request_id: false - config: - idle_timeout: 5s - read_timeout: 5s - write_timeout: 5s - clusters: - - name: "test_dubbo" - lb_policy: "RoundRobin" - registries: - "zookeeper": - timeout: "3s" - address: "127.0.0.1:2181" - username: "" - password: "" - timeout_config: - connect_timeout: "5s" - request_timeout: "10s" - shutdown_config: - timeout: "60s" - step_timeout: "10s" - reject_policy: "immediacy" \ No newline at end of file + - filters: + - name: dgp.filter.httpconnectionmanager + config: + route_config: + routes: + - match: + prefix: "/api/v1" + http_filters: + - name: dgp.filter.http.apiconfig + config: +# path: samples\dubbogo\simple\body\pixiu\api_config.yaml + path: $PROJECT_DIR/pixiu/api_config.yaml + - name: dgp.filter.http.dubboproxy + config: + dubboProxyConfig: + registries: + "zookeeper": + protocol: "zookeeper" + timeout: "3s" + address: "127.0.0.1:2181" + username: "" + password: "" + timeout_config: + connect_timeout: 5s + request_timeout: 5s + - name: dgp.filter.http.response + config: + server_name: "test_http_dubbo" + generate_request_id: false + +#static_resources: +# listeners: +# - name: "net/http" +# address: +# socket_address: +# protocol_type: "HTTP" +# address: "0.0.0.0" +# port: 8881 +# filter_chains: +# - filter_chain_match: +# domains: +# - api.dubbo.com +# - api.pixiu.com +# filters: +# - name: dgp.filter.httpconnectionmanager +# config: +# route_config: +# routes: +# - match: +# prefix: "/api/v1" +# route: +# cluster: "test-dubbo" +# cluster_not_found_response_code: 505 +# http_filters: +# - name: dgp.filter.http.apiconfig +# config: +# path: samples\dubbogo\simple\body\pixiu\api_config.yaml +# - name: dgp.filter.http.dubboproxy +# config: +# dubboProxyConfig: +# registries: +# "zookeeper": +# protocol: "zookeeper" +# timeout: "3s" +# address: "127.0.0.1:2181" +# username: "" +# password: "" +# timeout_config: +# connect_timeout: 5s +# request_timeout: 5s +# - name: dgp.filter.http.response +# config: +# server_name: "test_http_dubbo" +# generate_request_id: false +# config: +# idle_timeout: 5s +# read_timeout: 5s +# write_timeout: 5s +# clusters: +# - name: "test_dubbo" +# lb_policy: "RoundRobin" +# registries: +# "zookeeper": +# timeout: "3s" +# address: "127.0.0.1:2181" +# username: "" +# password: "" +# timeout_config: +# connect_timeout: "5s" +# request_timeout: "10s" +# shutdown_config: +# timeout: "60s" +# step_timeout: "10s" +# reject_policy: "immediacy" \ No newline at end of file From 6c47868f97669c9cb4792d7ebf51d0e542b7d6a1 Mon Sep 17 00:00:00 2001 From: zhaokaiwei <1186713932@qq.com> Date: Sun, 19 Sep 2021 11:40:32 +0800 Subject: [PATCH 2/3] [simplify config for pixiu] fix: evise code specification --- pkg/config/config_load.go | 3 +-- pkg/model/cluster.go | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/config/config_load.go b/pkg/config/config_load.go index dd5d4e780..2d5b2bc4f 100644 --- a/pkg/config/config_load.go +++ b/pkg/config/config_load.go @@ -18,15 +18,14 @@ package config import ( - "github.com/creasty/defaults" "io/ioutil" "log" "path/filepath" ) import ( + "github.com/creasty/defaults" "github.com/ghodss/yaml" - "github.com/goinggo/mapstructure" ) diff --git a/pkg/model/cluster.go b/pkg/model/cluster.go index 5704e2ef3..24ab34964 100644 --- a/pkg/model/cluster.go +++ b/pkg/model/cluster.go @@ -50,12 +50,12 @@ var ( type ( // Cluster a single upstream cluster Cluster struct { - Name string `yaml:"name" json:"name" default:"test_dubbo"` // Name the cluster unique name - TypeStr string `yaml:"type" json:"type"` // Type the cluster discovery type string value - Type DiscoveryType `yaml:",omitempty" json:",omitempty"` // Type the cluster discovery type + Name string `yaml:"name" json:"name"` // Name the cluster unique name + TypeStr string `yaml:"type" json:"type"` // Type the cluster discovery type string value + Type DiscoveryType `yaml:",omitempty" json:",omitempty"` // Type the cluster discovery type EdsClusterConfig EdsClusterConfig `yaml:"eds_cluster_config" json:"eds_cluster_config" mapstructure:"eds_cluster_config"` - LbStr string `yaml:"lb_policy" json:"lb_policy" default:"RoundRobin"` // Lb the cluster select node used loadBalance policy - Lb LbPolicy `yaml:",omitempty" json:",omitempty"` // Lb the cluster select node used loadBalance policy + LbStr string `yaml:"lb_policy" json:"lb_policy"` // Lb the cluster select node used loadBalance policy + Lb LbPolicy `yaml:",omitempty" json:",omitempty"` // Lb the cluster select node used loadBalance policy HealthChecks []HealthCheck `yaml:"health_checks" json:"health_checks"` Endpoints []*Endpoint `yaml:"endpoints" json:"endpoints"` } @@ -68,7 +68,7 @@ type ( // Registry remote registry where dubbo apis are registered. Registry struct { - Protocol string `yaml:"protocol" json:"protocol"` + Protocol string `default:"zookeeper" yaml:"protocol" json:"protocol"` Timeout string `yaml:"timeout" json:"timeout"` Address string `yaml:"address" json:"address"` Username string `yaml:"username" json:"username"` From ff6537f43317bc25ae8bb4dcfaf3359641144bf4 Mon Sep 17 00:00:00 2001 From: zhaokaiwei <1186713932@qq.com> Date: Thu, 23 Sep 2021 17:22:54 +0800 Subject: [PATCH 3/3] [simplify config for pixiu] fix: delete unused comment --- samples/dubbogo/simple/body/pixiu/conf.yaml | 66 --------------------- 1 file changed, 66 deletions(-) diff --git a/samples/dubbogo/simple/body/pixiu/conf.yaml b/samples/dubbogo/simple/body/pixiu/conf.yaml index 1b6f11f2d..3273b6436 100644 --- a/samples/dubbogo/simple/body/pixiu/conf.yaml +++ b/samples/dubbogo/simple/body/pixiu/conf.yaml @@ -31,7 +31,6 @@ static_resources: http_filters: - name: dgp.filter.http.apiconfig config: -# path: samples\dubbogo\simple\body\pixiu\api_config.yaml path: $PROJECT_DIR/pixiu/api_config.yaml - name: dgp.filter.http.dubboproxy config: @@ -50,68 +49,3 @@ static_resources: config: server_name: "test_http_dubbo" generate_request_id: false - -#static_resources: -# listeners: -# - name: "net/http" -# address: -# socket_address: -# protocol_type: "HTTP" -# address: "0.0.0.0" -# port: 8881 -# filter_chains: -# - filter_chain_match: -# domains: -# - api.dubbo.com -# - api.pixiu.com -# filters: -# - name: dgp.filter.httpconnectionmanager -# config: -# route_config: -# routes: -# - match: -# prefix: "/api/v1" -# route: -# cluster: "test-dubbo" -# cluster_not_found_response_code: 505 -# http_filters: -# - name: dgp.filter.http.apiconfig -# config: -# path: samples\dubbogo\simple\body\pixiu\api_config.yaml -# - name: dgp.filter.http.dubboproxy -# config: -# dubboProxyConfig: -# registries: -# "zookeeper": -# protocol: "zookeeper" -# timeout: "3s" -# address: "127.0.0.1:2181" -# username: "" -# password: "" -# timeout_config: -# connect_timeout: 5s -# request_timeout: 5s -# - name: dgp.filter.http.response -# config: -# server_name: "test_http_dubbo" -# generate_request_id: false -# config: -# idle_timeout: 5s -# read_timeout: 5s -# write_timeout: 5s -# clusters: -# - name: "test_dubbo" -# lb_policy: "RoundRobin" -# registries: -# "zookeeper": -# timeout: "3s" -# address: "127.0.0.1:2181" -# username: "" -# password: "" -# timeout_config: -# connect_timeout: "5s" -# request_timeout: "10s" -# shutdown_config: -# timeout: "60s" -# step_timeout: "10s" -# reject_policy: "immediacy" \ No newline at end of file