Skip to content

Commit

Permalink
add README and example cases for nacos registry (#3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
joy999 committed Oct 23, 2023
1 parent aa625d2 commit db59f42
Show file tree
Hide file tree
Showing 11 changed files with 566 additions and 2 deletions.
81 changes: 81 additions & 0 deletions contrib/registry/nacos/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# GoFrame Nacos Registry


Use `nacos` as service registration and discovery management.


## Installation
```
go get -u -v github.com/gogf/gf/contrib/registry/nacos/v2
```
suggested using `go.mod`:
```
require github.com/gogf/gf/contrib/registry/nacos/v2 latest
```


## Example

### Reference example

[server](example/registry/nacos/server/main.go)
```go
package main

import (
"github.com/gogf/gf/contrib/registry/nacos/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/net/gsvc"
)

func main() {
gsvc.SetRegistry(nacos.New(`127.0.0.1:8848`).
SetClusterName("DEFAULT").
SetGroupName("DEFAULT_GROUP"))

s := g.Server(`hello.svc`)
s.BindHandler("/", func(r *ghttp.Request) {
g.Log().Info(r.Context(), `request received`)
r.Response.Write(`Hello world`)
})
s.Run()
}
```

[client](example/registry/nacos/client/main.go)
```go
package main

import (
"fmt"
"time"

"github.com/gogf/gf/contrib/registry/nacos/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gsel"
"github.com/gogf/gf/v2/net/gsvc"
"github.com/gogf/gf/v2/os/gctx"
)

func main() {
gsvc.SetRegistry(nacos.New(`127.0.0.1:8848`))
gsel.SetBuilder(gsel.NewBuilderRoundRobin())

client := g.Client()
for i := 0; i < 100; i++ {
res, err := client.Get(gctx.New(), `http://hello.svc/`)
if err != nil {
panic(err)
}
fmt.Println(res.ReadAllString())
res.Close()
time.Sleep(time.Second)
}
}
```

## License

`GoFrame Nacos` is licensed under the [MIT License](../../../LICENSE), 100% free and open-source, forever.

4 changes: 2 additions & 2 deletions contrib/registry/nacos/nacos_z_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestWatch(t *testing.T) {
t.AssertNil(err)
t.Assert(registered.GetName(), svc2.GetName())

time.Sleep(time.Second * 1)
time.Sleep(time.Second * 10)

// Watch and retrieve the service changes:
// svc1 and svc2 is the same service name, which has 2 endpoints.
Expand All @@ -154,7 +154,7 @@ func TestWatch(t *testing.T) {
err = registry2.Deregister(ctx, svc2)
t.AssertNil(err)

time.Sleep(time.Second * 1)
time.Sleep(time.Second * 10)
proceedResult, err = watcher.Proceed()
t.AssertNil(err)
t.Assert(len(proceedResult), 1)
Expand Down
31 changes: 31 additions & 0 deletions example/registry/nacos/grpc/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package main

import (
"github.com/gogf/gf/contrib/registry/nacos/v2"
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"github.com/gogf/gf/example/registry/etcd/grpc/protobuf"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)

func main() {
grpcx.Resolver.Register(nacos.New("127.0.0.1:8848"))

var (
ctx = gctx.New()
conn = grpcx.Client.MustNewGrpcClientConn("demo")
client = protobuf.NewGreeterClient(conn)
)
res, err := client.SayHello(ctx, &protobuf.HelloRequest{Name: "World"})
if err != nil {
g.Log().Error(ctx, err)
return
}
g.Log().Debug(ctx, "Response:", res.Message)
}
27 changes: 27 additions & 0 deletions example/registry/nacos/grpc/controller/helloworld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package controller

import (
"context"

"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"github.com/gogf/gf/example/registry/etcd/grpc/protobuf"
)

type Controller struct {
protobuf.UnimplementedGreeterServer
}

func Register(s *grpcx.GrpcServer) {
protobuf.RegisterGreeterServer(s.Server, &Controller{})
}

// SayHello implements helloworld.GreeterServer
func (s *Controller) SayHello(ctx context.Context, in *protobuf.HelloRequest) (*protobuf.HelloReply, error) {
return &protobuf.HelloReply{Message: "Hello " + in.GetName()}, nil
}
217 changes: 217 additions & 0 deletions example/registry/nacos/grpc/protobuf/helloworld.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db59f42

Please sign in to comment.