From f486e0e6f4b3eaecb45e7140906de2038a08724b Mon Sep 17 00:00:00 2001 From: violapioggia <2604296771@qq.com> Date: Fri, 27 Oct 2023 11:22:41 +0800 Subject: [PATCH 1/2] feat:add more zookeeper examples --- zookeeper/example/standard/client/main.go | 105 +++++++++++++++++++++- zookeeper/example/standard/server/main.go | 81 +++++++++++++---- 2 files changed, 169 insertions(+), 17 deletions(-) diff --git a/zookeeper/example/standard/client/main.go b/zookeeper/example/standard/client/main.go index 0d84d78..2ce739c 100644 --- a/zookeeper/example/standard/client/main.go +++ b/zookeeper/example/standard/client/main.go @@ -16,6 +16,12 @@ package main import ( "context" + "encoding/json" + "fmt" + "github.com/cloudwego/hertz/pkg/app/client/discovery" + "github.com/cloudwego/hertz/pkg/app/client/loadbalance" + "github.com/cloudwego/hertz/pkg/protocol" + "net" "time" "github.com/cloudwego/hertz/pkg/app/client" @@ -25,21 +31,116 @@ import ( "github.com/hertz-contrib/registry/zookeeper" ) +type Example struct { + A int `json:"a"` + B int `json:"b"` +} + func main() { + r, err := zookeeper.NewZookeeperResolver([]string{"127.0.0.1:2181"}, 40*time.Second) + if err != nil { + panic(err) + } + discoveryWithSD(r) + discoveryWithTag(r) + discoveryWithCustomizedAddr(r) + discoveryWithLoadBalanceOptions(r) + discoveryThenUsePostMethod(r) +} + +func discoveryWithSD(r discovery.Resolver) { + fmt.Println("simply discovery:") cli, err := client.NewClient() if err != nil { panic(err) } - r, err := zookeeper.NewZookeeperResolver([]string{"127.0.0.1:2181"}, 40*time.Second) + cli.Use(sd.Discovery(r)) + for i := 0; i < 10; i++ { + status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo1/ping", config.WithSD(true)) + if err != nil { + hlog.Fatal(err) + } + hlog.Infof("code=%d,body=%s", status, string(body)) + } +} + +func discoveryWithTag(r discovery.Resolver) { + fmt.Println("discovery with tag:") + cli, err := client.NewClient() if err != nil { panic(err) } cli.Use(sd.Discovery(r)) for i := 0; i < 10; i++ { - status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true)) + status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo1/ping", config.WithSD(true), config.WithTag("key1", "val1")) if err != nil { hlog.Fatal(err) } hlog.Infof("code=%d,body=%s", status, string(body)) } } + +func discoveryWithCustomizedAddr(r discovery.Resolver) { + fmt.Println("discovery with customizedAddr:") + cli, err := client.NewClient() + if err != nil { + panic(err) + } + + cli.Use(sd.Discovery(r, sd.WithCustomizedAddrs(net.JoinHostPort("127.0.0.1", "8888")))) + for i := 0; i < 10; i++ { + status, body, err := cli.Get(context.Background(), nil, "http://custom-config-demo/ping", config.WithSD(true), config.WithTag("key1", "val1")) + if err != nil { + hlog.Fatal(err) + } + hlog.Infof("code=%d,body=%s", status, string(body)) + } +} + +func discoveryWithLoadBalanceOptions(r discovery.Resolver) { + fmt.Println("discovery with loadBalanceOptions:") + cli, err := client.NewClient() + if err != nil { + panic(err) + } + cli.Use(sd.Discovery(r, sd.WithLoadBalanceOptions(loadbalance.NewWeightedBalancer(), loadbalance.Options{ + RefreshInterval: 5 * time.Second, + ExpireInterval: 15 * time.Second, + }))) + for i := 0; i < 10; i++ { + status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo1/ping", config.WithSD(true)) + if err != nil { + hlog.Fatal(err) + } + hlog.Infof("code=%d,body=%s", status, string(body)) + } +} + +func discoveryThenUsePostMethod(r discovery.Resolver) { + fmt.Println("discovery and use post method to send request:") + cli, err := client.NewClient() + if err != nil { + panic(err) + } + cli.Use(sd.Discovery(r)) + + for i := 0; i < 10; i++ { + // set request config、method、request uri. + req := protocol.AcquireRequest() + req.SetOptions(config.WithSD(true)) + req.SetMethod("POST") + req.SetRequestURI("http://hertz.test.demo1/ping") + t := Example{A: i, B: i} + bytes, _ := json.Marshal(t) + // set body and content type + req.SetBody(bytes) + req.Header.SetContentTypeBytes([]byte("application/json")) + resp := protocol.AcquireResponse() + // send request + err := cli.Do(context.Background(), req, resp) + if err != nil { + hlog.Fatal(err) + } + hlog.Infof("code=%d,body=%s", resp.StatusCode(), string(resp.Body())) + } +} diff --git a/zookeeper/example/standard/server/main.go b/zookeeper/example/standard/server/main.go index a957ceb..502a3ce 100755 --- a/zookeeper/example/standard/server/main.go +++ b/zookeeper/example/standard/server/main.go @@ -16,32 +16,83 @@ package main import ( "context" - "time" - "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/app/server/registry" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/cloudwego/hertz/pkg/protocol/consts" "github.com/hertz-contrib/registry/zookeeper" + "net" + "sync" + "time" ) +var wg sync.WaitGroup + +type Example struct { + A int `json:"a"` + B int `json:"b"` +} + func main() { - addr := "127.0.0.1:8888" r, err := zookeeper.NewZookeeperRegistry([]string{"127.0.0.1:2181"}, 40*time.Second) if err != nil { panic(err) } - h := server.Default( - server.WithHostPorts(addr), - server.WithRegistry(r, ®istry.Info{ - ServiceName: "hertz.test.demo", - Addr: utils.NewNetAddr("tcp", addr), - Weight: 10, - Tags: nil, - })) - h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { - ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) - }) - h.Spin() + + wg.Add(2) + go func() { + defer wg.Done() + addr := net.JoinHostPort("127.0.0.1", "8888") + tags := map[string]string{"group": "blue", "idc": "hd1"} + h := server.Default( + server.WithHostPorts(addr), + server.WithRegistry(r, ®istry.Info{ + ServiceName: "hertz.test.demo1", + Addr: utils.NewNetAddr("tcp", addr), + Weight: 10, + Tags: tags, + })) + h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + ctx.JSON(consts.StatusOK, utils.H{"ping": "pong1"}) + }) + h.POST("/ping", func(c context.Context, ctx *app.RequestContext) { + e := Example{} + if err := ctx.Bind(&e); err != nil { + ctx.String(consts.StatusBadRequest, err.Error()) + return + } + ctx.JSON(consts.StatusOK, e) + }) + h.Spin() + + }() + go func() { + defer wg.Done() + addr := net.JoinHostPort("127.0.0.1", "8889") + tags := map[string]string{"group": "red", "idc": "hd2"} + h := server.Default( + server.WithHostPorts(addr), + server.WithRegistry(r, ®istry.Info{ + ServiceName: "hertz.test.demo2", + Addr: utils.NewNetAddr("tcp", addr), + Weight: 10, + Tags: tags, + })) + h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { + ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) + }) + h.POST("/ping", func(c context.Context, ctx *app.RequestContext) { + e := Example{} + if err := ctx.Bind(&e); err != nil { + ctx.String(consts.StatusBadRequest, err.Error()) + return + } + ctx.JSON(consts.StatusOK, e) + }) + h.Spin() + + }() + + wg.Wait() } From 09fe56206cc15648bc02209d64131a6f8bc44d88 Mon Sep 17 00:00:00 2001 From: violapioggia <2604296771@qq.com> Date: Fri, 27 Oct 2023 11:30:13 +0800 Subject: [PATCH 2/2] style:gofumpt --- zookeeper/example/standard/client/main.go | 5 +++-- zookeeper/example/standard/server/main.go | 9 ++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/zookeeper/example/standard/client/main.go b/zookeeper/example/standard/client/main.go index 2ce739c..971036a 100644 --- a/zookeeper/example/standard/client/main.go +++ b/zookeeper/example/standard/client/main.go @@ -18,11 +18,12 @@ import ( "context" "encoding/json" "fmt" + "net" + "time" + "github.com/cloudwego/hertz/pkg/app/client/discovery" "github.com/cloudwego/hertz/pkg/app/client/loadbalance" "github.com/cloudwego/hertz/pkg/protocol" - "net" - "time" "github.com/cloudwego/hertz/pkg/app/client" "github.com/cloudwego/hertz/pkg/app/middlewares/client/sd" diff --git a/zookeeper/example/standard/server/main.go b/zookeeper/example/standard/server/main.go index 502a3ce..a591f0d 100755 --- a/zookeeper/example/standard/server/main.go +++ b/zookeeper/example/standard/server/main.go @@ -16,15 +16,16 @@ package main import ( "context" + "net" + "sync" + "time" + "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/app/server/registry" "github.com/cloudwego/hertz/pkg/common/utils" "github.com/cloudwego/hertz/pkg/protocol/consts" "github.com/hertz-contrib/registry/zookeeper" - "net" - "sync" - "time" ) var wg sync.WaitGroup @@ -65,7 +66,6 @@ func main() { ctx.JSON(consts.StatusOK, e) }) h.Spin() - }() go func() { defer wg.Done() @@ -91,7 +91,6 @@ func main() { ctx.JSON(consts.StatusOK, e) }) h.Spin() - }() wg.Wait()