Skip to content

Commit

Permalink
add go test
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jan 4, 2024
1 parent 5726421 commit bcb1dd6
Show file tree
Hide file tree
Showing 7 changed files with 701 additions and 27 deletions.
2 changes: 1 addition & 1 deletion nacosv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[中文](../nacos/README_CN.md)

NacosV2 as service discovery for Hertz.
**Nacos V2 sdk** as service discovery for **Hertz**.

## How to use?

Expand Down
43 changes: 20 additions & 23 deletions nacosv2/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[English](../nacosv2/README.md)

使用 **nacosV2** 作为 **Hertz** 的注册中心
使用 **nacos v2 sdk** 作为 **Hertz** 的注册中心

## 这个项目应当如何使用?

Expand All @@ -12,24 +12,24 @@

```go
import (
"context"
"log"
"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/nacosv2"
"context"
"log"

"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/nacosv2"
)

func main() {
addr := "127.0.0.1:8888"
r, err := nacosRegistry.NewDefaultNacosV2Registry()
if err != nil {
log.Fatal(err)
return
}
r, err := nacosv2.NewDefaultNacosV2Registry()
if err != nil {
log.Fatal(err)
return
}
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
Expand All @@ -39,10 +39,11 @@ func main() {
Tags: nil,
}),
)
// ...
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})
h.Spin()
}

```

### 客户端
Expand All @@ -51,7 +52,6 @@ func main() {

```go
import (
"context"
"log"

"github.com/cloudwego/hertz/pkg/app/client"
Expand All @@ -66,7 +66,7 @@ func main() {
if err != nil {
panic(err)
}
r, err := nacos.NewDefaultNacosV2Resolver()
r, err := nacosv2.NewDefaultNacosV2Resolver()
if err != nil {
log.Fatal(err)
return
Expand Down Expand Up @@ -119,8 +119,6 @@ go run ./examples/standard/client/main.go

```go
import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/app/server/registry"
Expand Down Expand Up @@ -179,8 +177,7 @@ func main() {

```go
import (
"context"


"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
"github.com/cloudwego/hertz/pkg/common/config"
Expand Down
2 changes: 1 addition & 1 deletion nacosv2/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewDefaultNacosV2Config() (naming_client.INamingClient, error) {
serverConfig := []constant.ServerConfig{
*constant.NewServerConfig(NacosV2Addr(), uint64(NacosV2Port())),
}
var namingClient, err = clients.NewNamingClient(
namingClient, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &clientConfig,
ServerConfigs: serverConfig,
Expand Down
4 changes: 2 additions & 2 deletions nacosv2/exmaple/custom_config/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func main() {
LogLevel: "info",
}

nacosCli, err := clients.NewNamingClient(
nacosClient, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
})
if err != nil {
panic(err)
}
r := nacosv2.NewNacosV2Resolver(nacosCli)
r := nacosv2.NewNacosV2Resolver(nacosClient)
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))
Expand Down
46 changes: 46 additions & 0 deletions nacosv2/exmaple/stand_multiple_server/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2023 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"
"log"

"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/nacosv2"
)

func main() {
client, err := client.NewClient()
if err != nil {
panic(err)
}
r, err := nacosv2.NewDefaultNacosV2Resolver()
if err != nil {
log.Fatal(err)
return
}
client.Use(sd.Discovery(r))
for i := 0; i < 10; i++ {
status, body, err := client.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true))
if err != nil {
hlog.Fatal(err)
}
hlog.Infof("code=%d,body=%s\n", status, string(body))
}
}
78 changes: 78 additions & 0 deletions nacosv2/exmaple/stand_multiple_server/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2021 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"
"log"
"sync"

"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/nacosv2"
)

var (
wg sync.WaitGroup
server1IP = "127.0.0.1:8088"
server2IP = "127.0.0.1:8089"
)

func main() {
r, err := nacosv2.NewDefaultNacosV2Registry()
if err != nil {
log.Fatal(err)
return
}
wg.Add(2)
go func() {
defer wg.Done()
h := server.Default(
server.WithHostPorts(server1IP),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", server1IP),
Weight: 10,
Tags: nil,
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping1": "pong1"})
})
h.Spin()
}()

go func() {
defer wg.Done()
h := server.Default(
server.WithHostPorts(server2IP),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", server2IP),
Weight: 10,
Tags: nil,
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping2": "pong2"})
})
h.Spin()
}()

wg.Wait()
}
Loading

0 comments on commit bcb1dd6

Please sign in to comment.