From 435b87f70ab1e3bbd724ad438e5e92b341e3804c Mon Sep 17 00:00:00 2001 From: liaochuntao Date: Fri, 19 Jul 2024 21:58:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Ddemo=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E7=A1=AE=E9=97=AE=E9=A2=98=20(#211)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../circuitbreaker/instance/consumer/go.sum | 1 + .../circuitbreaker/instance/consumer/main.go | 1 + .../circuitbreaker/instance/provider/go.sum | 1 + .../circuitbreaker/instance/provider/main.go | 30 ++++++++-- examples/quickstart/consumer/main.go | 22 ++++++- examples/quickstart/consumer/polaris.yaml | 5 +- examples/ratelimit/consumer/go.sum | 8 +-- examples/ratelimit/consumer/main.go | 26 ++++++++- examples/ratelimit/consumer/polaris.yaml | 5 +- examples/ratelimit/provider/go.sum | 8 +-- examples/ratelimit/provider/main.go | 2 +- examples/route/dynamic/consumer/go.sum | 8 +-- examples/route/dynamic/consumer/main.go | 57 +++++++++---------- examples/route/dynamic/provider/go.sum | 8 +-- examples/route/dynamic/provider/main.go | 21 ++++++- examples/route/nearby/README-zh.md | 12 ++-- examples/route/nearby/README.md | 12 ++-- examples/route/nearby/consumer/go.sum | 8 +-- examples/route/nearby/consumer/main.go | 2 - examples/route/nearby/consumer/polaris.yaml | 7 ++- examples/route/nearby/provider/go.sum | 8 +-- examples/route/nearby/provider/polaris.yaml | 4 +- examples/watch/service/longpull/polaris.yaml | 2 +- pkg/flow/data/object.go | 31 ++++++++-- pkg/flow/impl.go | 9 ++- pkg/flow/quota/assist.go | 6 +- pkg/network/impl.go | 34 ----------- plugin/location/local/local.go | 29 ++++++---- plugin/location/location.go | 6 ++ plugin/metrics/prometheus/reporter.go | 2 +- polaris.yaml | 12 ++-- 31 files changed, 212 insertions(+), 175 deletions(-) diff --git a/examples/circuitbreaker/instance/consumer/go.sum b/examples/circuitbreaker/instance/consumer/go.sum index 8180cb83..d93306f5 100644 --- a/examples/circuitbreaker/instance/consumer/go.sum +++ b/examples/circuitbreaker/instance/consumer/go.sum @@ -185,6 +185,7 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/examples/circuitbreaker/instance/consumer/main.go b/examples/circuitbreaker/instance/consumer/main.go index 9f6626c9..560e5865 100644 --- a/examples/circuitbreaker/instance/consumer/main.go +++ b/examples/circuitbreaker/instance/consumer/main.go @@ -76,6 +76,7 @@ func (svr *PolarisConsumer) discoverInstance() (string, model.Resource, error) { getOneRequest := &polaris.GetOneInstanceRequest{} getOneRequest.Namespace = namespace getOneRequest.Service = service + // 允许返回熔断半开的实例 getOneRequest.IncludeCircuitBreakInstances = true oneInstResp, err := svr.consumer.GetOneInstance(getOneRequest) if err != nil { diff --git a/examples/circuitbreaker/instance/provider/go.sum b/examples/circuitbreaker/instance/provider/go.sum index 8180cb83..d93306f5 100644 --- a/examples/circuitbreaker/instance/provider/go.sum +++ b/examples/circuitbreaker/instance/provider/go.sum @@ -185,6 +185,7 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/examples/circuitbreaker/instance/provider/main.go b/examples/circuitbreaker/instance/provider/main.go index 9d24dd8c..a348e2ce 100644 --- a/examples/circuitbreaker/instance/provider/main.go +++ b/examples/circuitbreaker/instance/provider/main.go @@ -48,6 +48,7 @@ func initArgs() { // PolarisProvider is a provider for polaris type PolarisProvider struct { + webSvr *http.Server provider polaris.ProviderAPI namespace string service string @@ -87,6 +88,7 @@ func (svr *PolarisProvider) runWebServer() { } else { atomic.StoreInt32(&svr.needErr, 0) } + log.Printf("switch success openError:" + val) rw.WriteHeader(http.StatusOK) }) @@ -99,8 +101,9 @@ func (svr *PolarisProvider) runWebServer() { go func() { log.Printf("[INFO] start http server, listen port is %v", svr.port) - if err := http.Serve(ln, nil); err != nil { - log.Fatalf("[ERROR]fail to run webServer, err is %v", err) + svr.webSvr = &http.Server{} + if err := svr.webSvr.Serve(ln); err != nil { + log.Printf("[ERROR]fail to run webServer, err is %v", err) } }() } @@ -113,14 +116,28 @@ func (svr *PolarisProvider) registerService() { registerRequest.Host = svr.host registerRequest.Port = svr.port registerRequest.ServiceToken = token - resp, err := svr.provider.Register(registerRequest) + resp, err := svr.provider.RegisterInstance(registerRequest) if err != nil { log.Fatalf("fail to register instance, err is %v", err) } log.Printf("register response: instanceId %s", resp.InstanceID) } -func runMainLoop() { +func (svr *PolarisProvider) deregisterService() { + log.Printf("start to invoke deregister operation") + registerRequest := &polaris.InstanceDeRegisterRequest{} + registerRequest.Service = service + registerRequest.Namespace = namespace + registerRequest.Host = svr.host + registerRequest.Port = svr.port + registerRequest.ServiceToken = token + if err := svr.provider.Deregister(registerRequest); err != nil { + log.Fatalf("fail to deregister instance, err is %v", err) + } + log.Printf("deregister finished") +} + +func (svr *PolarisProvider) runMainLoop() { ch := make(chan os.Signal, 1) signal.Notify(ch, []os.Signal{ syscall.SIGINT, syscall.SIGTERM, @@ -129,6 +146,8 @@ func runMainLoop() { for s := range ch { log.Printf("catch signal(%+v), stop servers", s) + _ = svr.webSvr.Close() + svr.deregisterService() return } } @@ -156,8 +175,7 @@ func main() { } svr.Run() - - runMainLoop() + svr.runMainLoop() } func getLocalHost(serverAddr string) (string, error) { diff --git a/examples/quickstart/consumer/main.go b/examples/quickstart/consumer/main.go index 03c1aee3..967fae47 100644 --- a/examples/quickstart/consumer/main.go +++ b/examples/quickstart/consumer/main.go @@ -24,6 +24,9 @@ import ( "log" "math/rand" "net/http" + "os" + "os/signal" + "syscall" "time" "github.com/polarismesh/polaris-go" @@ -47,11 +50,24 @@ type PolarisConsumer struct { consumer polaris.ConsumerAPI namespace string service string + webSvr *http.Server } // Run starts the consumer func (svr *PolarisConsumer) Run() { - svr.runWebServer() + go svr.runWebServer() + ch := make(chan os.Signal, 1) + signal.Notify(ch, []os.Signal{ + syscall.SIGINT, syscall.SIGTERM, + syscall.SIGSEGV, + }...) + + for s := range ch { + svr.consumer.Destroy() + log.Printf("catch signal(%+v), stop servers", s) + _ = svr.webSvr.Close() + return + } } func (svr *PolarisConsumer) runWebServer() { @@ -133,7 +149,9 @@ func (svr *PolarisConsumer) runWebServer() { log.Printf("start run web server, port : %d", port) - if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil); err != nil { + webSvr := &http.Server{Addr: fmt.Sprintf("0.0.0.0:%d", port), Handler: nil} + svr.webSvr = webSvr + if err := webSvr.ListenAndServe(); err != nil { log.Fatalf("[ERROR]fail to run webServer, err is %v", err) } } diff --git a/examples/quickstart/consumer/polaris.yaml b/examples/quickstart/consumer/polaris.yaml index 07e08516..12264fb3 100644 --- a/examples/quickstart/consumer/polaris.yaml +++ b/examples/quickstart/consumer/polaris.yaml @@ -8,5 +8,6 @@ global: - prometheus plugin: prometheus: - type: pull - metricPort: 0 + type: push + address: 127.0.0.1:9091 + interval: 10s diff --git a/examples/ratelimit/consumer/go.sum b/examples/ratelimit/consumer/go.sum index 48997cc2..d93306f5 100644 --- a/examples/ratelimit/consumer/go.sum +++ b/examples/ratelimit/consumer/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.3.2-alpha.2 h1:cMghyvCnRVM5ca2kYCGHOgIIxVnokiMvw0720q8a8RA= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -405,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/ratelimit/consumer/main.go b/examples/ratelimit/consumer/main.go index ceb470b9..ed31d402 100644 --- a/examples/ratelimit/consumer/main.go +++ b/examples/ratelimit/consumer/main.go @@ -24,6 +24,9 @@ import ( "log" "math/rand" "net/http" + "os" + "os/signal" + "syscall" "time" "github.com/polarismesh/polaris-go" @@ -47,11 +50,24 @@ type PolarisConsumer struct { consumer polaris.ConsumerAPI namespace string service string + webSvr *http.Server } // Run starts the consumer func (svr *PolarisConsumer) Run() { - svr.runWebServer() + go svr.runWebServer() + ch := make(chan os.Signal, 1) + signal.Notify(ch, []os.Signal{ + syscall.SIGINT, syscall.SIGTERM, + syscall.SIGSEGV, + }...) + + for s := range ch { + svr.consumer.Destroy() + log.Printf("catch signal(%+v), stop servers", s) + _ = svr.webSvr.Close() + return + } } func (svr *PolarisConsumer) runWebServer() { @@ -74,7 +90,9 @@ func (svr *PolarisConsumer) runWebServer() { } start := time.Now() - resp, err := http.Get(fmt.Sprintf("http://%s:%d/echo", instance.GetHost(), instance.GetPort())) + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d/echo", instance.GetHost(), instance.GetPort()), nil) + req.Header = r.Header + resp, err := http.DefaultClient.Do(req) if err != nil { log.Printf("[errot] send request to %s:%d fail : %s", instance.GetHost(), instance.GetPort(), err) rw.WriteHeader(http.StatusInternalServerError) @@ -133,7 +151,9 @@ func (svr *PolarisConsumer) runWebServer() { log.Printf("start run web server, port : %d", port) - if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil); err != nil { + webSvr := &http.Server{Addr: fmt.Sprintf("0.0.0.0:%d", port), Handler: nil} + svr.webSvr = webSvr + if err := webSvr.ListenAndServe(); err != nil { log.Fatalf("[ERROR]fail to run webServer, err is %v", err) } } diff --git a/examples/ratelimit/consumer/polaris.yaml b/examples/ratelimit/consumer/polaris.yaml index 07e08516..12264fb3 100644 --- a/examples/ratelimit/consumer/polaris.yaml +++ b/examples/ratelimit/consumer/polaris.yaml @@ -8,5 +8,6 @@ global: - prometheus plugin: prometheus: - type: pull - metricPort: 0 + type: push + address: 127.0.0.1:9091 + interval: 10s diff --git a/examples/ratelimit/provider/go.sum b/examples/ratelimit/provider/go.sum index 48997cc2..d93306f5 100644 --- a/examples/ratelimit/provider/go.sum +++ b/examples/ratelimit/provider/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.3.2-alpha.2 h1:cMghyvCnRVM5ca2kYCGHOgIIxVnokiMvw0720q8a8RA= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -405,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/ratelimit/provider/main.go b/examples/ratelimit/provider/main.go index 72ab62c7..5267a6ec 100644 --- a/examples/ratelimit/provider/main.go +++ b/examples/ratelimit/provider/main.go @@ -76,7 +76,7 @@ func (svr *PolarisProvider) runWebServer() { quotaReq.SetMethod("/echo") headers := convertHeaders(r.Header) for k, v := range headers { - quotaReq.AddArgument(model.BuildHeaderArgument(k, v)) + quotaReq.AddArgument(model.BuildHeaderArgument(strings.ToLower(k), v)) } quotaReq.SetNamespace(namespace) quotaReq.SetService(service) diff --git a/examples/route/dynamic/consumer/go.sum b/examples/route/dynamic/consumer/go.sum index 48997cc2..d93306f5 100644 --- a/examples/route/dynamic/consumer/go.sum +++ b/examples/route/dynamic/consumer/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.3.2-alpha.2 h1:cMghyvCnRVM5ca2kYCGHOgIIxVnokiMvw0720q8a8RA= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -405,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/route/dynamic/consumer/main.go b/examples/route/dynamic/consumer/main.go index 09f2adaf..83fe2daf 100644 --- a/examples/route/dynamic/consumer/main.go +++ b/examples/route/dynamic/consumer/main.go @@ -19,6 +19,7 @@ package main import ( "bytes" + "encoding/json" "flag" "fmt" "io/ioutil" @@ -62,28 +63,6 @@ type PolarisConsumer struct { // Run . func (svr *PolarisConsumer) Run() { - if selfService != "" && selfNamespace != "" { - tmpHost, err := getLocalHost(svr.provider.SDKContext().GetConfig().GetGlobal().GetServerConnector().GetAddresses()[0]) - if nil != err { - panic(fmt.Errorf("error occur while fetching localhost: %v", err)) - } - req := &polaris.InstanceRegisterRequest{} - req.Namespace = selfNamespace - req.Service = selfService - log.Printf("start to invoke register operation") - registerRequest := &polaris.InstanceRegisterRequest{} - registerRequest.Service = service - registerRequest.Namespace = namespace - registerRequest.Host = tmpHost - registerRequest.Port = int(port) - registerRequest.ServiceToken = token - resp, err := svr.provider.RegisterInstance(registerRequest) - if nil != err { - log.Fatalf("fail to register instance, err is %v", err) - } - log.Printf("register response: instanceId %s", resp.InstanceID) - } - svr.runWebServer() } @@ -105,7 +84,8 @@ func (svr *PolarisConsumer) runWebServer() { routerRequest.DstInstances = instancesResp routerRequest.SourceService.Service = selfService routerRequest.SourceService.Namespace = selfNamespace - routerRequest.AddArguments(convertQuery(r.URL.RawQuery)...) + routerRequest.AddArguments(convertRouteArguments(r)...) + log.Printf("route request %s", mustJson(routerRequest)) routerInstancesResp, err := svr.router.ProcessRouters(routerRequest) if nil != err { log.Printf("[error] fail to processRouters, err is %v", err) @@ -191,16 +171,26 @@ func main() { } -func convertQuery(rawQuery string) []model.Argument { +func convertRouteArguments(r *http.Request) []model.Argument { arguments := make([]model.Argument, 0, 4) - if len(rawQuery) == 0 { - return arguments + + headers := r.Header + if len(headers) != 0 { + for k, vs := range headers { + if len(vs) == 0 { + continue + } + arguments = append(arguments, model.BuildHeaderArgument(strings.ToLower(k), vs[0])) + } } - tokens := strings.Split(rawQuery, "&") - if len(tokens) > 0 { - for _, token := range tokens { - values := strings.Split(token, "=") - arguments = append(arguments, model.BuildQueryArgument(values[0], values[1])) + + query := r.URL.Query() + if len(query) != 0 { + for k, vs := range query { + if len(vs) == 0 { + continue + } + arguments = append(arguments, model.BuildQueryArgument(strings.ToLower(k), vs[0])) } } return arguments @@ -218,3 +208,8 @@ func getLocalHost(serverAddr string) (string, error) { } return localAddr, nil } + +func mustJson(v interface{}) string { + d, _ := json.Marshal(v) + return string(d) +} diff --git a/examples/route/dynamic/provider/go.sum b/examples/route/dynamic/provider/go.sum index 48997cc2..d93306f5 100644 --- a/examples/route/dynamic/provider/go.sum +++ b/examples/route/dynamic/provider/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.3.2-alpha.2 h1:cMghyvCnRVM5ca2kYCGHOgIIxVnokiMvw0720q8a8RA= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -405,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/route/dynamic/provider/main.go b/examples/route/dynamic/provider/main.go index c53b0256..b9c8ddf0 100644 --- a/examples/route/dynamic/provider/main.go +++ b/examples/route/dynamic/provider/main.go @@ -43,7 +43,7 @@ var ( func initArgs() { flag.StringVar(&namespace, "namespace", "default", "namespace") flag.StringVar(&service, "service", "RouteEchoServer", "service") - flag.IntVar(&port, "port", 8888, "service port") + flag.IntVar(&port, "port", 0, "service port") // 当北极星开启鉴权时,需要配置此参数完成相关的权限检查 flag.StringVar(&token, "token", "", "token") flag.StringVar(&metadata, "metadata", "", "key1=value1&key2=value2") @@ -86,7 +86,7 @@ func (svr *PolarisProvider) Run() { svr.host = tmpHost svr.runWebServer() svr.registerService() - runMainLoop() + svr.runMainLoop() } func (svr *PolarisProvider) registerService() { @@ -105,6 +105,20 @@ func (svr *PolarisProvider) registerService() { log.Printf("register response: instanceId %s", resp.InstanceID) } +func (svr *PolarisProvider) deregisterService() { + log.Printf("start to invoke deregister operation") + registerRequest := &polaris.InstanceDeRegisterRequest{} + registerRequest.Service = service + registerRequest.Namespace = namespace + registerRequest.Host = host + registerRequest.Port = svr.port + registerRequest.ServiceToken = token + if err := svr.provider.Deregister(registerRequest); nil != err { + log.Fatalf("fail to deregister instance, err is %v", err) + } + log.Printf("deregister successfully") +} + func (svr *PolarisProvider) runWebServer() { http.HandleFunc("/echo", func(rw http.ResponseWriter, r *http.Request) { rw.WriteHeader(http.StatusOK) @@ -128,7 +142,7 @@ func (svr *PolarisProvider) runWebServer() { } -func runMainLoop() { +func (svr *PolarisProvider) runMainLoop() { ch := make(chan os.Signal, 1) signal.Notify(ch, []os.Signal{ syscall.SIGINT, syscall.SIGTERM, @@ -137,6 +151,7 @@ func runMainLoop() { for s := range ch { log.Printf("catch signal(%+v), stop servers", s) + svr.deregisterService() return } } diff --git a/examples/route/nearby/README-zh.md b/examples/route/nearby/README-zh.md index bb0da963..a1e6fc1b 100644 --- a/examples/route/nearby/README-zh.md +++ b/examples/route/nearby/README-zh.md @@ -60,9 +60,9 @@ global: for ((i=1; i<=3; i ++)) do # 设置地域信息 - export POLARIS_INSTANCE_REGION=china - export POLARIS_INSTANCE_ZONE=ap-guangzhou - export POLARIS_INSTANCE_CAMPUS=ap-guangzhou-${i} + export REGION=china + export ZONE=ap-guangzhou + export CAMPUS=ap-guangzhou-${i} # linux/mac运行命令 ./provider > provider-20000.log 2>&1 & @@ -73,9 +73,9 @@ then ``` # 设置地域信息 -export POLARIS_INSTANCE_REGION=china -export POLARIS_INSTANCE_ZONE=ap-guangzhou -export POLARIS_INSTANCE_CAMPUS=ap-guangzhou-1 +export REGION=china +export ZONE=ap-guangzhou +export CAMPUS=ap-guangzhou-1 # linux/mac运行命令 ./consumer --selfNamespace={selfName} --selfService=EchoConsumer diff --git a/examples/route/nearby/README.md b/examples/route/nearby/README.md index da8137e8..c6feac9b 100644 --- a/examples/route/nearby/README.md +++ b/examples/route/nearby/README.md @@ -60,9 +60,9 @@ Run the built **provider** executable for ((i=1; i<=3; i++)) do # Set the locale information - export POLARIS_INSTANCE_REGION=china - export POLARIS_INSTANCE_ZONE=ap-guangzhou - export POLARIS_INSTANCE_CAMPUS=ap-guangzhou-${i} + export REGION=china + export ZONE=ap-guangzhou + export CAMPUS=ap-guangzhou-${i} # linux/mac run command ./provider > provider-20000.log 2>&1 & @@ -73,9 +73,9 @@ Run the built **consumer** executable ```` # Set the locale information -export POLARIS_INSTANCE_REGION=china -export POLARIS_INSTANCE_ZONE=ap-guangzhou -export POLARIS_INSTANCE_CAMPUS=ap-guangzhou-1 +export REGION=china +export ZONE=ap-guangzhou +export CAMPUS=ap-guangzhou-1 # linux/mac run command ./consumer --selfNamespace={selfName} --selfService=EchoConsumer diff --git a/examples/route/nearby/consumer/go.sum b/examples/route/nearby/consumer/go.sum index 48997cc2..d93306f5 100644 --- a/examples/route/nearby/consumer/go.sum +++ b/examples/route/nearby/consumer/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.3.2-alpha.2 h1:cMghyvCnRVM5ca2kYCGHOgIIxVnokiMvw0720q8a8RA= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -405,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/route/nearby/consumer/main.go b/examples/route/nearby/consumer/main.go index be43c476..390178f9 100644 --- a/examples/route/nearby/consumer/main.go +++ b/examples/route/nearby/consumer/main.go @@ -60,14 +60,12 @@ type PolarisConsumer struct { // Run . func (svr *PolarisConsumer) Run() { - tmpHost, err := getLocalHost(svr.consumer.SDKContext().GetConfig().GetGlobal().GetServerConnector().GetAddresses()[0]) if nil != err { panic(fmt.Errorf("error occur while fetching localhost: %v", err)) } svr.host = tmpHost - svr.runWebServer() } diff --git a/examples/route/nearby/consumer/polaris.yaml b/examples/route/nearby/consumer/polaris.yaml index 5ee581ef..e3fc19d4 100644 --- a/examples/route/nearby/consumer/polaris.yaml +++ b/examples/route/nearby/consumer/polaris.yaml @@ -6,9 +6,10 @@ global: location: providers: - type: local - region: ${REGION} - zone: ${ZONE} - campus: ${CAMPUS} + options: + region: ${REGION} + zone: ${ZONE} + campus: ${CAMPUS} consumer: serviceRouter: # 服务路由链 diff --git a/examples/route/nearby/provider/go.sum b/examples/route/nearby/provider/go.sum index 2a18abaf..d93306f5 100644 --- a/examples/route/nearby/provider/go.sum +++ b/examples/route/nearby/provider/go.sum @@ -171,7 +171,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -321,7 +320,6 @@ github.com/googleapis/gax-go/v2 v2.3.0/go.mod h1:b8LNqSzNabLiUpXKkY7HAR5jr6bIT99 github.com/googleapis/gax-go/v2 v2.4.0/go.mod h1:XOTVJ59hdnfJLIP/dh8n5CGryZR2LxK9wbMD5+iXC6c= github.com/googleapis/gax-go/v2 v2.5.1/go.mod h1:h6B0KMMFNtI2ddbGJn3T3ZbwkeT6yqEF02fYlzkUCyo= github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -340,7 +338,6 @@ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= @@ -375,8 +372,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/specification v1.4.0 h1:fm7sUtFZC2g9+lLmRCtjGrUow47CY5JDFoZXwwCQGGY= -github.com/polarismesh/specification v1.4.0/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= +github.com/polarismesh/specification v1.4.1 h1:lTZqeyUhhWuKyr6NDKBwmUrNfcUDvKLxWT/uOq71T5A= github.com/polarismesh/specification v1.4.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -405,9 +401,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= -github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= diff --git a/examples/route/nearby/provider/polaris.yaml b/examples/route/nearby/provider/polaris.yaml index 614bd51b..028ebf80 100644 --- a/examples/route/nearby/provider/polaris.yaml +++ b/examples/route/nearby/provider/polaris.yaml @@ -1,7 +1,7 @@ global: serverConnector: addresses: - - 119.91.66.223:8091 + - 127.0.0.1:8091 # 地址提供插件,用于获取当前SDK所在的地域信息 location: providers: @@ -10,7 +10,7 @@ global: region: ${REGION} zone: ${ZONE} campus: ${CAMPUS} -#描述:主调端配置 + #描述:主调端配置 statReporter: enable: true chain: diff --git a/examples/watch/service/longpull/polaris.yaml b/examples/watch/service/longpull/polaris.yaml index 5524cd96..eae1cffd 100644 --- a/examples/watch/service/longpull/polaris.yaml +++ b/examples/watch/service/longpull/polaris.yaml @@ -1,6 +1,6 @@ global: serverConnector: addresses: - - 119.91.66.223:8091 + - 127.0.0.1:8091 statReporter: enable: false diff --git a/pkg/flow/data/object.go b/pkg/flow/data/object.go index 281ef713..2089863c 100644 --- a/pkg/flow/data/object.go +++ b/pkg/flow/data/object.go @@ -21,6 +21,8 @@ import ( "sync" "time" + apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" + "github.com/polarismesh/polaris-go/pkg/config" "github.com/polarismesh/polaris-go/pkg/model" "github.com/polarismesh/polaris-go/pkg/plugin" @@ -551,7 +553,7 @@ type CommonRateLimitRequest struct { DstService model.ServiceKey Token uint32 Method string - Arguments map[int]map[string]string + Arguments map[apitraffic.MatchArgument_Type]map[string]string RateLimitRule model.ServiceRule Trigger model.NotifyTrigger ControlParam model.ControlParam @@ -567,16 +569,35 @@ func (cl *CommonRateLimitRequest) clearValues() { cl.Arguments = nil } -func parseArguments(arguments []model.Argument) map[int]map[string]string { - argumentMap := make(map[int]map[string]string, 0) +func toSpecArgument(i int) apitraffic.MatchArgument_Type { + switch i { + case model.ArgumentTypeCallerIP: + return apitraffic.MatchArgument_CALLER_IP + case model.ArgumentTypeCallerService: + return apitraffic.MatchArgument_CALLER_SERVICE + case model.ArgumentTypeCustom: + return apitraffic.MatchArgument_CUSTOM + case model.ArgumentTypeHeader: + return apitraffic.MatchArgument_HEADER + case model.ArgumentTypeMethod: + return apitraffic.MatchArgument_METHOD + case model.ArgumentTypeQuery: + return apitraffic.MatchArgument_QUERY + default: + return apitraffic.MatchArgument_CUSTOM + } +} + +func parseArguments(arguments []model.Argument) map[apitraffic.MatchArgument_Type]map[string]string { + argumentMap := make(map[apitraffic.MatchArgument_Type]map[string]string, 0) if len(arguments) == 0 { return argumentMap } for _, argument := range arguments { - stringMatchArgumentMap := argumentMap[argument.ArgumentType()] + stringMatchArgumentMap := argumentMap[toSpecArgument(argument.ArgumentType())] if nil == stringMatchArgumentMap { stringMatchArgumentMap = make(map[string]string) - argumentMap[argument.ArgumentType()] = stringMatchArgumentMap + argumentMap[toSpecArgument(argument.ArgumentType())] = stringMatchArgumentMap } stringMatchArgumentMap[argument.Key()] = argument.Value() } diff --git a/pkg/flow/impl.go b/pkg/flow/impl.go index f0aeef5d..79ef6045 100644 --- a/pkg/flow/impl.go +++ b/pkg/flow/impl.go @@ -355,7 +355,9 @@ func (e *Engine) SyncReportStat(typ model.MetricType, stat model.InstanceGauge) // reportAPIStat 上报api数据 func (e *Engine) reportAPIStat(result *model.APICallResult) error { - return e.SyncReportStat(model.SDKAPIStat, result) + // TODO: SDK 本身和北极星 server 的服务调用监控数据不能和用户的监控数据混合在一起,这里可以打印在本地日志中 + // return e.SyncReportStat(model.SDKAPIStat, result) + return nil } // reportSvcStat 上报服务数据 @@ -365,8 +367,8 @@ func (e *Engine) reportSvcStat(result *model.ServiceCallResult) error { // loadLocation 上报服务数据 func (e *Engine) loadLocation() { - providerName := e.configuration.GetGlobal().GetLocation().GetProviders() - if len(providerName) == 0 { + providers := e.configuration.GetGlobal().GetLocation().GetProviders() + if len(providers) == 0 { return } locationProvider, err := e.plugins.GetPlugin(common.TypeLocationProvider, location.ProviderName) @@ -380,6 +382,7 @@ func (e *Engine) loadLocation() { return } + log.GetBaseLogger().Infof("location provider get location result: %v", loc) e.globalCtx.SetCurrentLocation(&model.Location{ Region: loc.Region, Zone: loc.Zone, diff --git a/pkg/flow/quota/assist.go b/pkg/flow/quota/assist.go index 7c4be4d7..895fe7a2 100644 --- a/pkg/flow/quota/assist.go +++ b/pkg/flow/quota/assist.go @@ -358,7 +358,7 @@ func matchStringValue(matchString *apimodel.MatchString, value string, ruleCache } // lookupRule 寻址规则 -func lookupRules(svcRule model.ServiceRule, method string, arguments map[int]map[string]string) []*apitraffic.Rule { +func lookupRules(svcRule model.ServiceRule, method string, arguments map[apitraffic.MatchArgument_Type]map[string]string) []*apitraffic.Rule { if reflect2.IsNil(svcRule) || reflect2.IsNil(svcRule.GetValue()) { // 规则集为空 return nil @@ -393,7 +393,7 @@ func lookupRules(svcRule model.ServiceRule, method string, arguments map[int]map matched := true if len(argumentMatchers) > 0 { for _, argumentMatcher := range argumentMatchers { - stringStringMap := arguments[int(argumentMatcher.Type)] + stringStringMap := arguments[argumentMatcher.Type] if len(stringStringMap) == 0 { matched = false break @@ -440,7 +440,7 @@ func FormatLabelToStr(request *data.CommonRateLimitRequest, rule *apitraffic.Rul if regexCombine && valueMatcher.GetType() != apimodel.MatchString_EXACT { labelValue = valueMatcher.GetValue().GetValue() } else { - stringStringMap := request.Arguments[int(argumentMatcher.GetType())] + stringStringMap := request.Arguments[argumentMatcher.GetType()] labelValue, _ = getLabelValue(argumentMatcher, stringStringMap) if valueMatcher.GetType() != apimodel.MatchString_EXACT { regexSpread = true diff --git a/pkg/network/impl.go b/pkg/network/impl.go index d4aad074..4c7836c2 100644 --- a/pkg/network/impl.go +++ b/pkg/network/impl.go @@ -407,45 +407,11 @@ func (c *connectionManager) ConnectByAddr(clusterType config.ClusterType, addr s // ReportSuccess 上报服务成功 func (c *connectionManager) ReportSuccess(connID ConnID, retCode int32, timeout time.Duration) { log.GetNetworkLogger().Debugf("service %s: reported success", connID.Service) - var err error - if !reflect2.IsNil(connID.instance) { - engineValue, ok := c.valueCtx.GetValue(model.ContextKeyEngine) - if ok { - engine := engineValue.(model.Engine) - result := &model.ServiceCallResult{ - CalledInstance: connID.instance, - RetStatus: model.RetSuccess} - result.SetDelay(timeout) - result.SetRetCode(retCode) - err = engine.SyncUpdateServiceCallResult(result) - } - } - if err != nil { - log.GetNetworkLogger().Errorf( - "error to update success call result for connection %s, %s", connID.String(), err) - } } // ReportFail 上报服务失败 func (c *connectionManager) ReportFail(connID ConnID, retCode int32, timeout time.Duration) { log.GetNetworkLogger().Warnf("connection %s: reported fail", connID) - var err error - if !reflect2.IsNil(connID.instance) && connID.Service.ClusterType != config.BuiltinCluster { - engineValue, ok := c.valueCtx.GetValue(model.ContextKeyEngine) - if ok { - engine := engineValue.(model.Engine) - result := &model.ServiceCallResult{ - CalledInstance: connID.instance, - RetStatus: model.RetFail} - result.SetDelay(timeout) - result.SetRetCode(retCode) - err = engine.SyncUpdateServiceCallResult(result) - } - } - if err != nil { - log.GetNetworkLogger().Errorf( - "error to update fail call result for connection %s, %s", connID.String(), err) - } } // ReportConnectionDown 报告连接故障 diff --git a/plugin/location/local/local.go b/plugin/location/local/local.go index 736e7ea4..9108e88c 100644 --- a/plugin/location/local/local.go +++ b/plugin/location/local/local.go @@ -29,6 +29,7 @@ const ( // LocationProviderImpl 从环境变量获取地域信息 type LocationProviderImpl struct { + ctx *plugin.InitContext locCache *model.Location } @@ -39,9 +40,24 @@ func New(ctx *plugin.InitContext) (*LocationProviderImpl, error) { // Init 初始化插件 func (p *LocationProviderImpl) Init(ctx *plugin.InitContext) error { + p.ctx = ctx + return nil +} + +// Name 插件名称 +func (p *LocationProviderImpl) Name() string { + return locationProviderLocal +} + +// GetLocation 获取地理位置信息 +func (p *LocationProviderImpl) GetLocation() (*model.Location, error) { + if p.locCache != nil { + return p.locCache, nil + } + log.GetBaseLogger().Infof("start use env location provider") - provider := ctx.Config.GetGlobal().GetLocation().GetProvider(locationProviderLocal) + provider := p.ctx.Config.GetGlobal().GetLocation().GetProvider(locationProviderLocal) options := provider.GetOptions() region, _ := options["region"].(string) @@ -53,16 +69,5 @@ func (p *LocationProviderImpl) Init(ctx *plugin.InitContext) error { Zone: zone, Campus: campus, } - - return nil -} - -// Name 插件名称 -func (p *LocationProviderImpl) Name() string { - return locationProviderLocal -} - -// GetLocation 获取地理位置信息 -func (p *LocationProviderImpl) GetLocation() (*model.Location, error) { return p.locCache, nil } diff --git a/plugin/location/location.go b/plugin/location/location.go index d52a0d58..7e25e0a9 100644 --- a/plugin/location/location.go +++ b/plugin/location/location.go @@ -120,6 +120,12 @@ func (p *Provider) Init(ctx *plugin.InitContext) error { sort.Slice(p.pluginChains, func(i, j int) bool { return priority[p.pluginChains[i].Name()] < priority[p.pluginChains[j].Name()] }) + + activeProviders := []string{} + for i := range p.pluginChains { + activeProviders = append(activeProviders, p.pluginChains[i].Name()) + } + log.GetBaseLogger().Infof("active location provider: %+v", activeProviders) return nil } diff --git a/plugin/metrics/prometheus/reporter.go b/plugin/metrics/prometheus/reporter.go index fb047ff1..723f8ed4 100644 --- a/plugin/metrics/prometheus/reporter.go +++ b/plugin/metrics/prometheus/reporter.go @@ -370,6 +370,7 @@ func (pa *PushAction) Init(initCtx *plugin.InitContext, reporter *PrometheusRepo pa.cfg = cfgValue.(*Config) pa.pusher = push. New(pa.cfg.Address, _defaultJobName). + Gatherer(pa.reporter.registry). Grouping(_defaultJobInstance, pa.initCtx.SDKContextID) } @@ -399,7 +400,6 @@ func (pa *PushAction) Run(ctx context.Context) { pa.reporter.rateLimitCollector.GetCurrentRevision()) if err := pa.pusher. - Gatherer(pa.reporter.registry). Push(); err != nil { log.GetBaseLogger().Errorf("push metrics to pushgateway fail: %s", err.Error()) return diff --git a/polaris.yaml b/polaris.yaml index aeefe24b..1d6cb406 100644 --- a/polaris.yaml +++ b/polaris.yaml @@ -50,7 +50,7 @@ global: #默认值:1s retryInterval: 1s #描述:客户端绑定的网卡地址 - bindIf: + bindIf: #描述:对接polaris server的相关配置 serverConnector: #描述:访问server的连接协议,SDK会根据协议名称会加载对应的插件 @@ -118,7 +118,7 @@ global: #类型:string #默认值: ${global.api.bindIP} #默认使用SDK的绑定IP - metricHost: + metricHost: #描述: 设置 prometheus http-server 的监听端口, 仅 type == pull 时生效 #类型:int #默认值: 28080 @@ -287,12 +287,14 @@ config: serverSwitchInterval: 10m #描述:重连间隔时间 reconnectInterval: 500ms + #描述: 开启客户端鉴权后,需要填写用户/用户组的访问凭据 + token: "" #描述:连接器插件配置 plugin: polaris: - #描述:GRPC客户端单次最大链路接收报文 - #类型:int - #范围:(0:524288000] + #描述:GRPC客户端单次最大链路接收报文 + #类型:int + #范围:(0:524288000] maxCallRecvMsgSize: 52428800 # 配置过滤器 configFilter: