Registry
// fetch the service list of serviceName
Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
// subscribe to a list of serviceName
Watch(ctx context.Context, serviceName string) (Watcher, error)
}
Implementations:
Usage
Register a Service
If use etcd or any other implementations, you can create a Registrar with other client.
import (
"github.com/go-kratos/kratos/contrib/registry/etcd/v2"
clientv3 "go.etcd.io/etcd/client/v3"
)
// new etcd client
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
})
if err != nil {
panic(err)
}
// new reg with etcd client
app := kratos.New(
// service-name
kratos.Name(Name),
kratos.Version(Version),
kratos.Metadata(map[string]string{}),
kratos.Logger(logger),
kratos.Server(
hs,
gs,
),
// with registrar
)
Service Discovery (gRPC)
It is the same as Register, if use etcd or any other implementations, you can create a Discovery with other client.
import (
clientv3 "go.etcd.io/etcd/client/v3"
)
// new etcd client
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
})
if err != nil {
panic(err)
}
// new dis with etcd client
dis := etcd.New(client)
endpoint := "discovery://default/provider"
conn, err := grpc.Dial(context.Background(), grpc.WithEndpoint(endpoint), grpc.WithDiscovery(dis))
if err != nil {
panic(err)
}