From ed86f36476c9f093662206c07dd2f9dc7536adf0 Mon Sep 17 00:00:00 2001 From: haiyux <99347745@qq.com> Date: Fri, 31 Dec 2021 14:11:08 +0800 Subject: [PATCH] feat(examples/):add etcd registry cmux example (#1734) --- examples/go.mod | 2 + examples/go.sum | 4 ++ examples/registry/etcd/cmuxServer/main.go | 80 +++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 examples/registry/etcd/cmuxServer/main.go diff --git a/examples/go.mod b/examples/go.mod index 83e416117..3f2f13425 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -33,6 +33,7 @@ require ( github.com/prometheus/client_golang v1.11.0 github.com/segmentio/kafka-go v0.4.17 github.com/sirupsen/logrus v1.8.1 + github.com/soheilhy/cmux v0.1.4 github.com/stretchr/testify v1.7.0 go.etcd.io/etcd/client/v3 v3.5.0 go.opentelemetry.io/otel v1.0.0 @@ -43,6 +44,7 @@ require ( golang.org/x/text v0.3.7 google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2 google.golang.org/grpc v1.42.0 + google.golang.org/grpc/examples v0.0.0-20211228230712-fbaf7c558210 // indirect google.golang.org/protobuf v1.27.1 ) diff --git a/examples/go.sum b/examples/go.sum index e028f1f32..d86393d81 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -588,6 +588,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -1013,6 +1014,7 @@ google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEY google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced/go.mod h1:SzzZ/N+nwJDaO1kznhnlzqS8ocJICar6hYhVyhi++24= @@ -1045,6 +1047,8 @@ google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnD google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.42.0 h1:XT2/MFpuPFsEX2fWh3YQtHkZ+WYZFQRfaUgLZYj/p6A= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= +google.golang.org/grpc/examples v0.0.0-20211228230712-fbaf7c558210 h1:xTGItiLKOFqOpgkCD5DK1dWd8RzqFz+2+S2H785PnRg= +google.golang.org/grpc/examples v0.0.0-20211228230712-fbaf7c558210/go.mod h1:gID3PKrg7pWKntu9Ss6zTLJ0ttC0X9IHgREOCZwbCVU= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/examples/registry/etcd/cmuxServer/main.go b/examples/registry/etcd/cmuxServer/main.go new file mode 100644 index 000000000..7237c3aec --- /dev/null +++ b/examples/registry/etcd/cmuxServer/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "context" + "fmt" + "log" + "net" + "strings" + + "github.com/go-kratos/kratos/contrib/registry/etcd/v2" + pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" + "github.com/go-kratos/kratos/v2" + "github.com/go-kratos/kratos/v2/middleware/recovery" + "github.com/go-kratos/kratos/v2/transport/grpc" + "github.com/go-kratos/kratos/v2/transport/http" + "github.com/soheilhy/cmux" + etcdclient "go.etcd.io/etcd/client/v3" +) + +// server is used to implement helloworld.GreeterServer. +type server struct { + pb.UnimplementedGreeterServer +} + +// SayHello implements helloworld.GreeterServer +func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { + return &pb.HelloReply{Message: fmt.Sprintf("Welcome %+v!", in.Name)}, nil +} + +func main() { + l, err := net.Listen("tcp", ":9091") + if err != nil { + log.Panic(err) + } + m := cmux.New(l) + + client, err := etcdclient.New(etcdclient.Config{ + Endpoints: []string{"127.0.0.1:2379"}, + }) + if err != nil { + log.Fatal(err) + } + + grpcSrv := grpc.NewServer( + grpc.Listener(m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldPrefixSendSettings("content-type", "application/grpc"))), + grpc.Middleware( + recovery.Recovery(), + ), + ) + httpSrv := http.NewServer( + http.Listener(m.Match(cmux.Any())), + http.Middleware( + recovery.Recovery(), + ), + ) + + s := &server{} + pb.RegisterGreeterServer(grpcSrv, s) + pb.RegisterGreeterHTTPServer(httpSrv, s) + + r := etcd.New(client) + app := kratos.New( + kratos.Name("helloworld"), + kratos.Server( + httpSrv, + grpcSrv, + ), + kratos.Registrar(r), + ) + + go func() { + if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") { + panic(err) + } + }() + + if err := app.Run(); err != nil { + log.Fatal(err) + } +}