服务注册与发现

接口实现

Registry 接口分为两个,Registrar 为实例注册和反注册,Discovery 为服务实例列表获取

  1. type Registrar interface {
  2. // 注册实例
  3. Register(ctx context.Context, service *ServiceInstance) error
  4. // 反注册实例
  5. Deregister(ctx context.Context, service *ServiceInstance) error
  6. }
  1. type Discovery interface {
  2. // 根据 serviceName 直接拉取实例列表
  3. GetService(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
  4. // 根据 serviceName 阻塞式订阅一个服务的实例列表信息
  5. Watch(ctx context.Context, serviceName string) (Watcher, error)
  6. }

已支持的实现:

使用方式

注册服务实例

创建一个 Registrar(以 consul 为例),将 Registrar 注入进 Kratos 应用实例中,Kratos 会自动完成实例注册和反注册

  1. import (
  2. consul "github.com/go-kratos/kratos/contrib/registry/consul/v2"
  3. "github.com/hashicorp/consul/api"
  4. )
  5. // new consul client
  6. client, err := api.NewClient(api.DefaultConfig())
  7. if err != nil {
  8. panic(err)
  9. }
  10. // new reg with consul client
  11. reg := consul.New(client)
  12. app := kratos.New(
  13. // service-name
  14. kratos.Name(Name),
  15. kratos.Version(Version),
  16. kratos.Metadata(map[string]string{}),
  17. kratos.Logger(logger),
  18. kratos.Server(
  19. hs,
  20. gs,
  21. ),
  22. // with registrar
  23. kratos.Registrar(reg),
  24. )

如果使用 etcd 或是其它实现,只需要根据不同的实现来创建 Registry 后传入

  1. import (
  2. "github.com/go-kratos/kratos/contrib/registry/etcd/v2"
  3. clientv3 "go.etcd.io/etcd/client/v3"
  4. )
  5. // new etcd client
  6. client, err := clientv3.New(clientv3.Config{
  7. Endpoints: []string{"127.0.0.1:2379"},
  8. })
  9. if err != nil {
  10. panic(err)
  11. }
  12. // new reg with etcd client
  13. reg := etcd.New(client)
  14. app := kratos.New(
  15. // service-name
  16. kratos.Name(Name),
  17. kratos.Version(Version),
  18. kratos.Metadata(map[string]string{}),
  19. kratos.Logger(logger),
  20. kratos.Server(
  21. hs,
  22. gs,
  23. ),
  24. // with registrar
  25. kratos.Registrar(reg),
  26. )

服务发现(gRPC)

创建一个 Discoverer(以 consul 为例),根据 Dial url 格式 <schema>://[authority]/<service-name> 创建一个 Endpoint,通过 grpc.WithDiscoverer,grpc.WithEndpoint 创建一个 grpc connection

  1. import (
  2. "context"
  3. consul "github.com/go-kratos/kratos/contrib/registry/consul/v2"
  4. "github.com/go-kratos/kratos/v2/transport/grpc"
  5. "github.com/hashicorp/consul/api"
  6. )
  7. // new consul client
  8. client, err := api.NewClient(api.DefaultConfig())
  9. if err != nil {
  10. panic(err)
  11. }
  12. // new dis with consul client
  13. dis := consul.New(client)
  14. endpoint := "discovery:///provider"
  15. conn, err := grpc.Dial(context.Background(), grpc.WithEndpoint(endpoint), grpc.WithDiscovery(dis))
  16. if err != nil {
  17. panic(err)
  18. }

与服务注册相同,如果使用 etcd 或是其它实现,只需要根据不同的实现来创建 Discovery 后传入

  1. import (
  2. "github.com/go-kratos/kratos/contrib/registry/etcd/v2"
  3. clientv3 "go.etcd.io/etcd/client/v3"
  4. )
  5. // new etcd client
  6. client, err := clientv3.New(clientv3.Config{
  7. Endpoints: []string{"127.0.0.1:2379"},
  8. })
  9. if err != nil {
  10. panic(err)
  11. }
  12. // new dis with etcd client
  13. dis := etcd.New(client)
  14. endpoint := "discovery:///provider"
  15. conn, err := grpc.Dial(context.Background(), grpc.WithEndpoint(endpoint), grpc.WithDiscovery(dis))
  16. if err != nil {
  17. panic(err)
  18. }