Registry

Interface

Registry has two interface, the Registrar is for services’ register and deregister, the Discovery is for fetching the list of services.

  1. type Registrar interface {
  2. // register the service
  3. Register(ctx context.Context, service *ServiceInstance) error
  4. // deregister the service
  5. Deregister(ctx context.Context, service *ServiceInstance) error
  6. }
  1. type Discovery interface {
  2. // fetch the service list of serviceName
  3. Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
  4. // subscribe to a list of serviceName
  5. Watch(ctx context.Context, serviceName string) (Watcher, error)
  6. }

Implementations:

Usage

Register a Service

Create a Registrar(e.g. consul) and inject it to Kratos applications. Then the framework will do register and deregister automatically.

  1. import (
  2. consul "github.com/go-kratos/consul/registry"
  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. )

If use etcd or any other implementations, you can create a Registrar with other client.

  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. )

Service Discovery (gRPC)

Create a Registrar(e.g. consul), create an Endpoint with url format as <schema>://[authority]/<service-name>, then use grc.WithDiscovery and grpc.WithEndpoint as the options of the Dial method to get the gRPC connection.

  1. import (
  2. "context"
  3. consul "github.com/go-kratos/consul/registry"
  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. }

It is the same as Register, if use etcd or any other implementations, you can create a Discovery with other client.

  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. }