Skip to content

Commit

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

* style: format code

* feat: standarized use of hlog and cli.Do method

* fix: fix consul tag

* fix: fix consul tag in test.yml
  • Loading branch information
MulL1on authored Aug 20, 2023
1 parent f474f58 commit 5d40e80
Show file tree
Hide file tree
Showing 9 changed files with 617 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

services:
consul:
image: 'consul:latest'
image: 'hashicorp/consul:latest'
ports:
- '8500:8500'
polaris:
Expand Down
4 changes: 2 additions & 2 deletions consul/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
prepare:
docker pull consul:latest
docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 -p 8500:8500 consul:latest
docker pull hashicorp/consul:latest
docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 -p 8500:8500 hashicorp/consul:latest
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package main
import (
"context"

"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"
Expand All @@ -34,10 +36,19 @@ func main() {

cli.Use(sd.Discovery(r))
for i := 0; i < 10; i++ {
status, body, err := cli.Get(context.Background(), nil, "http://hertz.discovery.eureka/ping", config.WithSD(true))
// set request method、config、request uri
req := protocol.AcquireRequest()
req.SetOptions(config.WithSD(true))
req.SetMethod("GET")
req.SetRequestURI("http://hertz.discovery.eureka/ping")
// set content type
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", status, string(body))
hlog.Infof("code=%d,body=%s", resp.StatusCode(), string(resp.Body()))
}
}
File renamed without changes.
187 changes: 187 additions & 0 deletions eureka/example/custom_config_mutiple_server/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright 2022 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"encoding/json"
"net"
"time"

"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/client/discovery"
"github.com/cloudwego/hertz/pkg/app/client/loadbalance"
"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/cloudwego/hertz/pkg/protocol"
"github.com/hertz-contrib/registry/eureka"
"github.com/hudl/fargo"
)

var configPath = "paht/to/your/config/file.gcfg"

type Message struct {
Message string `json:"message"`
}

func main() {
// build a eureka resolver from custom config file
eurekaConfig, err := fargo.ReadConfig(configPath)
if err != nil {
hlog.Fatal(err)
}
r := eureka.NewEurekaResolverFromConfig(eurekaConfig)

discoveryWithSD(r)
discoveryWithTag(r)
discoveryWithCustomizedAddr(r)
discoveryWithLoadBalanceOptions(r)
discoveryThenUsePostMethod(r)
}

func discoveryWithSD(r discovery.Resolver) {
hlog.Info("simply discovery:")
cli, err := client.NewClient()
if err != nil {
panic(err)
}
cli.Use(sd.Discovery(r))
for i := 0; i < 10; i++ {
// set request method、config、request uri
req := protocol.AcquireRequest()
req.SetOptions(config.WithSD(true))
req.SetMethod("GET")
req.SetRequestURI("http://hertz.discovery.eureka/ping")
// set content type
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()))
}
}

func discoveryWithTag(r discovery.Resolver) {
hlog.Info("discovery with tag:")
cli, err := client.NewClient()
if err != nil {
panic(err)
}
cli.Use(sd.Discovery(r))

for i := 0; i < 10; i++ {
// set request method、config、request uri
req := protocol.AcquireRequest()
req.SetOptions(config.WithSD(true), config.WithTag("key1", "val1"))
req.SetMethod("GET")
req.SetRequestURI("http://hertz.discovery.eureka/ping")
// set content type
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()))
}
}

func discoveryWithCustomizedAddr(r discovery.Resolver) {
hlog.Info("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", "5001"))))
for i := 0; i < 10; i++ {
// set request method、config、request uri
req := protocol.AcquireRequest()
req.SetOptions(config.WithSD(true))
req.SetMethod("GET")
req.SetRequestURI("http://hertz.discovery.eureka/ping")
// set content type
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()))
}
}

func discoveryWithLoadBalanceOptions(r discovery.Resolver) {
hlog.Info("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++ {
// set request method、config、request uri
req := protocol.AcquireRequest()
req.SetOptions(config.WithSD(true))
req.SetMethod("GET")
req.SetRequestURI("http://hertz.discovery.eureka/ping")
// set content type
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()))
}
}

func discoveryThenUsePostMethod(r discovery.Resolver) {
hlog.Info("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.discovery.eureka/ping")
m := Message{Message: "hello"}
bytes, _ := json.Marshal(m)
// 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()))
}
}
5 changes: 5 additions & 0 deletions eureka/example/custom_config_mutiple_server/config.gcfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Eureka]
ServiceUrls = http://127.0.0.1:8761/eureka
ConnectTimeoutSeconds = 10
Retries = 3
PollIntervalSeconds = 30
112 changes: 112 additions & 0 deletions eureka/example/custom_config_mutiple_server/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2022 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/hertz-contrib/registry/eureka"
"github.com/hudl/fargo"
)

var (
wg sync.WaitGroup
configPath = "paht/to/your/config/file.gcfg"
)

type Message struct {
Message string `json:"message"`
}

func main() {
// custom config
eurekaConfig, err := fargo.ReadConfig(configPath)
if err != nil {
hlog.Fatal(err)
}
r := eureka.NewEurekaRegistryFromConfig(eurekaConfig, 40*time.Second)

wg.Add(2)
go func() {
defer wg.Done()
addr := net.JoinHostPort("127.0.0.1", "5001")

h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.discovery.eureka",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: map[string]string{
"key1": "val1",
},
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})

h.POST("ping", func(c context.Context, ctx *app.RequestContext) {
m := Message{}
if err := ctx.Bind(&m); err != nil {
ctx.String(consts.StatusBadRequest, err.Error())
return
}
ctx.JSON(consts.StatusOK, m)
})
h.Spin()
}()

go func() {
defer wg.Done()
addr := net.JoinHostPort("127.0.0.1", "5002")

h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.discovery.eureka",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
Tags: map[string]string{
"key1": "val1",
},
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})

h.POST("ping", func(c context.Context, ctx *app.RequestContext) {
m := Message{}
if err := ctx.Bind(&m); err != nil {
ctx.String(consts.StatusBadRequest, err.Error())
return
}
ctx.JSON(consts.StatusOK, m)
})
h.Spin()
}()

wg.Wait()
}
Loading

0 comments on commit 5d40e80

Please sign in to comment.