Skip to content

Commit

Permalink
feat:add more zookeeper examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ViolaPioggia committed Oct 27, 2023
1 parent eb24a62 commit f486e0e
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 17 deletions.
105 changes: 103 additions & 2 deletions zookeeper/example/standard/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ package main

import (
"context"
"encoding/json"
"fmt"

Check failure on line 20 in zookeeper/example/standard/client/main.go

View workflow job for this annotation

GitHub Actions / lint module (./zookeeper)

File is not `gofumpt`-ed (gofumpt)
"github.com/cloudwego/hertz/pkg/app/client/discovery"
"github.com/cloudwego/hertz/pkg/app/client/loadbalance"
"github.com/cloudwego/hertz/pkg/protocol"
"net"

Check failure on line 24 in zookeeper/example/standard/client/main.go

View workflow job for this annotation

GitHub Actions / lint module (./zookeeper)

File is not `gofumpt`-ed (gofumpt)
"time"

"github.com/cloudwego/hertz/pkg/app/client"
Expand All @@ -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()))
}
}
81 changes: 66 additions & 15 deletions zookeeper/example/standard/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,83 @@ package main

import (
"context"

Check failure on line 18 in zookeeper/example/standard/server/main.go

View workflow job for this annotation

GitHub Actions / lint module (./zookeeper)

File is not `gofumpt`-ed (gofumpt)
"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, &registry.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, &registry.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, &registry.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()
}

0 comments on commit f486e0e

Please sign in to comment.