Registry

    1. // fetch the service list of serviceName
    2. Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
    3. // subscribe to a list of serviceName
    4. Watch(ctx context.Context, serviceName string) (Watcher, error)
    5. }

    Implementations:

    Usage

    Register a Service

    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. app := kratos.New(
    14. // service-name
    15. kratos.Name(Name),
    16. kratos.Version(Version),
    17. kratos.Metadata(map[string]string{}),
    18. kratos.Logger(logger),
    19. kratos.Server(
    20. hs,
    21. gs,
    22. ),
    23. // with registrar
    24. )

    Service Discovery (gRPC)

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

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