-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add README and example cases for nacos registry (#3075)
- Loading branch information
Showing
11 changed files
with
566 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.