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() }