Skip to content

Commit

Permalink
feat:add more zookeeper examples (#92)
Browse files Browse the repository at this point in the history
* feat:add more zookeeper examples

* style:gofumpt
  • Loading branch information
ViolaPioggia authored Oct 27, 2023
1 parent eb24a62 commit 133a1ea
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 15 deletions.
106 changes: 104 additions & 2 deletions zookeeper/example/standard/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,132 @@ package main

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"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/hlog"
"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()))
}
}
76 changes: 63 additions & 13 deletions zookeeper/example/standard/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package main

import (
"context"
"net"
"sync"
"time"

"github.com/cloudwego/hertz/pkg/app"
Expand All @@ -26,22 +28,70 @@ import (
"github.com/hertz-contrib/registry/zookeeper"
)

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 133a1ea

Please sign in to comment.