基本介绍

GoFrame框架提供了服务注册发现组件,由gsvc组件管理,该组件主要定义了注册发现的接口,具体的实现由社区组件提供: https://github.com/gogf/gf/tree/master/contrib/registry 。目前社区组件提供了多种注册发现的实现,如 etcd, zookeeper, polaris等,开发者根据需要插拔使用,也可以根据gsvc组件的接口定义实现自己的注册发现组件。

组件启用

注册发现组件只有在引入具体的接口实现时才会启用。例如,使用etcd实现注册发现的使用方式:

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/registry/etcd/v2"
  4. "github.com/gogf/gf/v2/net/gsvc"
  5. )
  6. func main() {
  7. gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
  8. // ...
  9. }

常用组件

组件名称文档说明备注
filehttps://github.com/gogf/gf/tree/master/contrib/registry/file仅用于单机测试
etcdhttps://github.com/gogf/gf/tree/master/contrib/registry/etcd
polarishttps://github.com/gogf/gf/tree/master/contrib/registry/polaris
zookeeperhttps://github.com/gogf/gf/tree/master/contrib/registry/zookeeper

更多组件,请参考:https://github.com/gogf/gf/tree/master/contrib/registry

使用示例

HTTP

可以使用gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))来设置使用etcd的注册发现。其中的etcd.New表示通过社区组件创建一个gsvc.Registry的接口实现对象,并通过gsvc.SetRegistry方法设置全局默认的注册发现接口实现对象。

server.go

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/registry/etcd/v2"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. "github.com/gogf/gf/v2/net/gsvc"
  7. )
  8. func main() {
  9. gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
  10. s := g.Server(`hello.svc`)
  11. s.BindHandler("/", func(r *ghttp.Request) {
  12. g.Log().Info(r.Context(), `request received`)
  13. r.Response.Write(`Hello world`)
  14. })
  15. s.Run()
  16. }

client.go

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/registry/etcd/v2"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/gsvc"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. )
  8. func main() {
  9. gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
  10. ctx := gctx.New()
  11. res := g.Client().GetContent(ctx, `http://hello.svc/`)
  12. g.Log().Info(ctx, res)
  13. }

执行后,服务端输出:

  1. $ go run server.go
  2. 2023-03-15 20:55:56.256 [INFO] pid[3358]: http server started listening on [:60700]
  3. 2023-03-15 20:55:56.256 [INFO] openapi specification is disabled
  4. 2023-03-15 20:55:56.256 [DEBU] service register: &{Head: Deployment: Namespace: Name:hello.svc Version: Endpoints:10.35.12.81:60700 Metadata:map[insecure:true protocol:http]}
  5. 2023-03-15 20:55:56.297 [DEBU] etcd put success with key "/service/default/default/hello.svc/latest/10.35.12.81:60700", value "{"insecure":true,"protocol":"http"}", lease "7587869265945813002"
  6. SERVER | DOMAIN | ADDRESS | METHOD | ROUTE | HANDLER | MIDDLEWARE
  7. ------------|---------|---------|--------|-------|-----------------------------------------------------------------|--------------------
  8. hello.svc | default | :60700 | ALL | / | main.main.func1 |
  9. ------------|---------|---------|--------|-------|-----------------------------------------------------------------|--------------------
  10. hello.svc | default | :60700 | ALL | /* | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE
  11. ------------|---------|---------|--------|-------|-----------------------------------------------------------------|--------------------
  12. 2023-03-15 20:56:45.739 [INFO] {880eaa8104994c17ffb384495cd4c613} request received

客户端输出:

  1. $ go run client.go
  2. 2023-03-15 20:56:45.739 [INFO] {880eaa8104994c17ffb384495cd4c613} Hello world

GRPC

如果是GRPC协议,必须使用grpcx.Resolvergrpcx模块来设置服务注册发现组件。

server.go

代码中的etcd.New表示通过社区组件创建一个gsvc.Registry的接口实现对象,并通过grpcx.Resolver.Register设置全局的grpc注册发现接口实现对象。

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/registry/etcd/v2"
  4. "github.com/gogf/gf/contrib/rpc/grpcx/v2"
  5. "github.com/gogf/gf/example/registry/etcd/grpc/controller"
  6. )
  7. func main() {
  8. grpcx.Resolver.Register(etcd.New("127.0.0.1:2379"))
  9. s := grpcx.Server.New()
  10. controller.Register(s)
  11. s.Run()
  12. }

client.go

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/registry/etcd/v2"
  4. "github.com/gogf/gf/contrib/rpc/grpcx/v2"
  5. "github.com/gogf/gf/example/registry/etcd/grpc/protobuf"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. )
  9. func main() {
  10. grpcx.Resolver.Register(etcd.New("127.0.0.1:2379"))
  11. var (
  12. ctx = gctx.New()
  13. conn = grpcx.Client.MustNewGrpcClientConn("demo")
  14. client = protobuf.NewGreeterClient(conn)
  15. )
  16. res, err := client.SayHello(ctx, &protobuf.HelloRequest{Name: "World"})
  17. if err != nil {
  18. g.Log().Error(ctx, err)
  19. return
  20. }
  21. g.Log().Debug(ctx, "Response:", res.Message)
  22. }

执行后,服务端输出:

  1. $ go run server.go
  2. 2023-03-15 21:06:57.204 [DEBU] service register: &{Head: Deployment: Namespace: Name:demo Version: Endpoints:10.35.12.81:61978 Metadata:map[protocol:grpc]}
  3. 2023-03-15 21:06:57.257 [DEBU] etcd put success with key "/service/default/default/demo/latest/10.35.12.81:61978", value "{"protocol":"grpc"}", lease "7587869265945813015"
  4. 2023-03-15 21:06:57.257 [INFO] pid[5786]: grpc server started listening on [:61978]
  5. 2023-03-15 21:07:04.955 {08f0aead94994c1731591d2b653ddc18} /protobuf.Greeter/SayHello, 0.002ms, name:"World", message:"Hello World"

客户端输出:

  1. $ go run client.go
  2. 2023-03-15 21:07:04.950 [DEBU] Watch key "/service/default/default/demo/latest/"
  3. 2023-03-15 21:07:04.952 [DEBU] client conn updated with addresses [{"Addr":"10.35.12.81:61978","ServerName":"demo","Attributes":{},"BalancerAttributes":null,"Type":0,"Metadata":null}]
  4. 2023-03-15 21:07:04.953 [DEBU] client conn updated with addresses [{"Addr":"10.35.12.81:61978","ServerName":"demo","Attributes":{},"BalancerAttributes":null,"Type":0,"Metadata":null}]
  5. 2023-03-15 21:07:04.955 [DEBU] {08f0aead94994c1731591d2b653ddc18} Response: Hello World