Ace
Using etcd discovery with go-grpc
B 站视频: gRPC + etcd 服务注册与发现
Getting Started
addsvc.go
func main() {
svc := ace.NewService(
ace.Name("hello"),
ace.EtcdUrls([]string{"localhost:2379"}),
ace.RegisterServiceFn(func(s *server.Server) {
pb.RegisterAddServer(s, &service.AddSvc{})
pb.RegisterHelloWorldServiceServer(s, &service.HelloSvc{})
}),
)
svc.Run()
}
addcli.go
func main() {
ctx := context.Background()
conn, err := ace.Dial(ctx, "hello", []string{"localhost:2379"})
if err != nil {
panic(err)
}
defer conn.Close()
var a, b int64 = 2, 3
addClient := pb.NewAddClient(conn)
sumReply, err := addClient.Sum(ctx, &pb.SumRequest{A: a, B: b})
if err != nil {
fmt.Println("err", err)
return
}
fmt.Printf("%d + %d = %d\n", a, b, sumReply.V)
}
etcd 与 gRPC 版本兼容问题
go.etcd.io/etcd/client/v3@v3.5.9/naming/resolver/resolver.go:22:11:
cannot use target.Endpoint (value of type func() string) as type string in struct literal
replace google.golang.org/grpc => google.golang.org/grpc v1.52.3
references
- gRPC naming and discovery
- go-kit
- go-micro