From ec9b0f11b35fa27e710df07d30c71fa5513bd01c Mon Sep 17 00:00:00 2001 From: zhangmj Date: Sat, 22 Apr 2023 00:15:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=AC=AC=E4=B8=89=E8=8A=82=20=E5=85=A8?= =?UTF-8?q?=E9=93=BE=E8=B7=AF=E6=97=A5=E5=BF=97=E5=AE=9E=E7=8E=B0=EF=BC=8C?= =?UTF-8?q?=E8=AF=B7=E6=8C=89=E7=85=A7=20tag=20=E6=A3=80=E5=87=BA=20v0.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + cmd/cart/main.go | 33 +++ cmd/good/main.go | 33 +++ cmd/order/main.go | 67 +++++ e2etest/orderclient.go | 28 ++ go.mod | 24 ++ go.sum | 93 ++++++ pb/cart.pb.go | 430 ++++++++++++++++++++++++++++ pb/cart.proto | 31 ++ pb/cart_grpc.pb.go | 141 +++++++++ pb/good.pb.go | 502 ++++++++++++++++++++++++++++++++ pb/good.proto | 37 +++ pb/good_grpc.pb.go | 141 +++++++++ pb/order.pb.go | 509 +++++++++++++++++++++++++++++++++ pb/order.proto | 44 +++ pb/order_grpc.pb.go | 105 +++++++ pkg/event/order.go | 21 ++ pkg/logger/format.go | 26 ++ pkg/logger/log.go | 17 ++ pkg/requestid/context.go | 22 ++ pkg/requestid/middleware.go | 18 ++ pkg/sleep/random.go | 11 + pkg/tracing/middleware.go | 18 ++ pkg/tracing/tracing.go | 34 +++ server/cart/cart_service.go | 46 +++ server/good/good_service.go | 49 ++++ server/order/order_servicec.go | 117 ++++++++ 27 files changed, 2598 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/cart/main.go create mode 100644 cmd/good/main.go create mode 100644 cmd/order/main.go create mode 100644 e2etest/orderclient.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pb/cart.pb.go create mode 100644 pb/cart.proto create mode 100644 pb/cart_grpc.pb.go create mode 100644 pb/good.pb.go create mode 100644 pb/good.proto create mode 100644 pb/good_grpc.pb.go create mode 100644 pb/order.pb.go create mode 100644 pb/order.proto create mode 100644 pb/order_grpc.pb.go create mode 100644 pkg/event/order.go create mode 100644 pkg/logger/format.go create mode 100644 pkg/logger/log.go create mode 100644 pkg/requestid/context.go create mode 100644 pkg/requestid/middleware.go create mode 100644 pkg/sleep/random.go create mode 100644 pkg/tracing/middleware.go create mode 100644 pkg/tracing/tracing.go create mode 100644 server/cart/cart_service.go create mode 100644 server/good/good_service.go create mode 100644 server/order/order_servicec.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/cmd/cart/main.go b/cmd/cart/main.go new file mode 100644 index 0000000..534f1de --- /dev/null +++ b/cmd/cart/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "context" + "go-opentracing-example/pb" + "go-opentracing-example/pkg/logger" + "go-opentracing-example/pkg/requestid" + "go-opentracing-example/pkg/tracing" + "go-opentracing-example/server/cart" + "google.golang.org/grpc" + "net" +) + +func main() { + s := grpc.NewServer( + grpc.ChainUnaryInterceptor( + requestid.WithRequestID(), + tracing.Middleware(), + ), + ) + + service := cart.New() + pb.RegisterCartServiceServer(s, service) + + ln, err := net.Listen("tcp", "127.0.0.1:5002") + if err != nil { + panic(err) + } + + logger.WithContext(context.Background(), "main").Infof("cart server start at: 5001") + + s.Serve(ln) +} diff --git a/cmd/good/main.go b/cmd/good/main.go new file mode 100644 index 0000000..5a3c72d --- /dev/null +++ b/cmd/good/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "context" + "go-opentracing-example/pb" + "go-opentracing-example/pkg/logger" + "go-opentracing-example/pkg/requestid" + "go-opentracing-example/pkg/tracing" + "go-opentracing-example/server/good" + "google.golang.org/grpc" + "net" +) + +func main() { + s := grpc.NewServer( + grpc.ChainUnaryInterceptor( + requestid.WithRequestID(), + tracing.Middleware(), + ), + ) + + service := good.New() + pb.RegisterGoodServiceServer(s, service) + + ln, err := net.Listen("tcp", "127.0.0.1:5001") + if err != nil { + panic(err) + } + + logger.WithContext(context.Background(), "main").Infof("good server start at: 5001") + + s.Serve(ln) +} diff --git a/cmd/order/main.go b/cmd/order/main.go new file mode 100644 index 0000000..f21099f --- /dev/null +++ b/cmd/order/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "github.com/opentracing/opentracing-go" + "go-opentracing-example/pb" + "go-opentracing-example/pkg/event" + "go-opentracing-example/pkg/logger" + "go-opentracing-example/pkg/requestid" + "go-opentracing-example/pkg/tracing" + "go-opentracing-example/server/order" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "log" + "net" +) + +func main() { + + tracer, closer := tracing.Init("orderService") + defer closer.Close() + opentracing.SetGlobalTracer(tracer) + + s := grpc.NewServer( + grpc.ChainUnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { + ctx = requestid.NewWithContext(ctx, "") + resp, err = handler(ctx, req) + + return resp, err + }, + tracing.Middleware(), + ), + ) + + goodsCC, err := grpc.Dial( + "localhost:5001", + grpc.WithInsecure(), + grpc.WithChainUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + id := requestid.RequestID(ctx) + ctx = metadata.AppendToOutgoingContext(ctx, "x-request-id", id) + return invoker(ctx, method, req, reply, cc, opts...) + }), + ) + if err != nil { + log.Fatal(err) + } + + goodClient := pb.NewGoodServiceClient(goodsCC) + + cartCC, err := grpc.Dial("localhost:5002", grpc.WithInsecure()) + if err != nil { + log.Fatal(err) + } + cartClient := pb.NewCartServiceClient(cartCC) + + orderServer := order.NewOrderService(cartClient, goodClient, event.New()) + pb.RegisterOrderServiceServer(s, orderServer) + + ln, err := net.Listen("tcp", "127.0.0.1:5000") + if err != nil { + panic(err) + } + + logger.WithContext(context.Background(), "main").Infof("order server start at: 5000") + + s.Serve(ln) +} diff --git a/e2etest/orderclient.go b/e2etest/orderclient.go new file mode 100644 index 0000000..851ec47 --- /dev/null +++ b/e2etest/orderclient.go @@ -0,0 +1,28 @@ +package main + +import ( + "context" + "fmt" + "go-opentracing-example/pb" + "google.golang.org/grpc" +) + +func main() { + + cc, err := grpc.Dial("localhost:5000", grpc.WithInsecure()) + if err != nil { + panic(err) + } + + client := pb.NewOrderServiceClient(cc) + + resp, err := client.CreateOrder(context.Background(), &pb.CreateOrderRequest{ + CartIds: []int64{1, 2, 3}, + }) + + if err != nil { + panic(err) + } + + fmt.Println(resp.OrderId) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a556424 --- /dev/null +++ b/go.mod @@ -0,0 +1,24 @@ +module go-opentracing-example + +go 1.19 + +require ( + github.com/google/uuid v1.3.0 + github.com/opentracing/opentracing-go v1.2.0 + github.com/sirupsen/logrus v1.9.0 + github.com/uber/jaeger-client-go v2.30.0+incompatible + google.golang.org/grpc v1.54.0 + google.golang.org/protobuf v1.30.0 +) + +require ( + github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/uber/jaeger-lib v2.4.1+incompatible // indirect + go.uber.org/atomic v1.10.0 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/text v0.8.0 // indirect + google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..323cc48 --- /dev/null +++ b/go.sum @@ -0,0 +1,93 @@ +dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM= +github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= +github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= +github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= +github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o= +github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= +github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg= +github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U= +go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= +go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= +golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo= +gonum.org/v1/gonum v0.8.2/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0= +gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw= +gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w= +google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM= +google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag= +google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/pb/cart.pb.go b/pb/cart.pb.go new file mode 100644 index 0000000..3de71c2 --- /dev/null +++ b/pb/cart.pb.go @@ -0,0 +1,430 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.30.0 +// protoc v4.22.3 +// source: cart.proto + +package pb + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Cart struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + GoodId int64 `protobuf:"varint,2,opt,name=goodId,proto3" json:"goodId,omitempty"` + Selected bool `protobuf:"varint,3,opt,name=selected,proto3" json:"selected,omitempty"` +} + +func (x *Cart) Reset() { + *x = Cart{} + if protoimpl.UnsafeEnabled { + mi := &file_cart_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Cart) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Cart) ProtoMessage() {} + +func (x *Cart) ProtoReflect() protoreflect.Message { + mi := &file_cart_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Cart.ProtoReflect.Descriptor instead. +func (*Cart) Descriptor() ([]byte, []int) { + return file_cart_proto_rawDescGZIP(), []int{0} +} + +func (x *Cart) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Cart) GetGoodId() int64 { + if x != nil { + return x.GoodId + } + return 0 +} + +func (x *Cart) GetSelected() bool { + if x != nil { + return x.Selected + } + return false +} + +type GetCardDetailByIDsRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ids []int64 `protobuf:"varint,1,rep,packed,name=ids,proto3" json:"ids,omitempty"` +} + +func (x *GetCardDetailByIDsRequest) Reset() { + *x = GetCardDetailByIDsRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_cart_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetCardDetailByIDsRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetCardDetailByIDsRequest) ProtoMessage() {} + +func (x *GetCardDetailByIDsRequest) ProtoReflect() protoreflect.Message { + mi := &file_cart_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetCardDetailByIDsRequest.ProtoReflect.Descriptor instead. +func (*GetCardDetailByIDsRequest) Descriptor() ([]byte, []int) { + return file_cart_proto_rawDescGZIP(), []int{1} +} + +func (x *GetCardDetailByIDsRequest) GetIds() []int64 { + if x != nil { + return x.Ids + } + return nil +} + +type GetCardDetailByIDsResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Carts []*Cart `protobuf:"bytes,1,rep,name=carts,proto3" json:"carts,omitempty"` +} + +func (x *GetCardDetailByIDsResponse) Reset() { + *x = GetCardDetailByIDsResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_cart_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetCardDetailByIDsResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetCardDetailByIDsResponse) ProtoMessage() {} + +func (x *GetCardDetailByIDsResponse) ProtoReflect() protoreflect.Message { + mi := &file_cart_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetCardDetailByIDsResponse.ProtoReflect.Descriptor instead. +func (*GetCardDetailByIDsResponse) Descriptor() ([]byte, []int) { + return file_cart_proto_rawDescGZIP(), []int{2} +} + +func (x *GetCardDetailByIDsResponse) GetCarts() []*Cart { + if x != nil { + return x.Carts + } + return nil +} + +type UpdateCartByIDsRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ids []int64 `protobuf:"varint,1,rep,packed,name=ids,proto3" json:"ids,omitempty"` + Selected bool `protobuf:"varint,2,opt,name=selected,proto3" json:"selected,omitempty"` +} + +func (x *UpdateCartByIDsRequest) Reset() { + *x = UpdateCartByIDsRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_cart_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UpdateCartByIDsRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UpdateCartByIDsRequest) ProtoMessage() {} + +func (x *UpdateCartByIDsRequest) ProtoReflect() protoreflect.Message { + mi := &file_cart_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UpdateCartByIDsRequest.ProtoReflect.Descriptor instead. +func (*UpdateCartByIDsRequest) Descriptor() ([]byte, []int) { + return file_cart_proto_rawDescGZIP(), []int{3} +} + +func (x *UpdateCartByIDsRequest) GetIds() []int64 { + if x != nil { + return x.Ids + } + return nil +} + +func (x *UpdateCartByIDsRequest) GetSelected() bool { + if x != nil { + return x.Selected + } + return false +} + +type UpdateCartByIDsResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *UpdateCartByIDsResponse) Reset() { + *x = UpdateCartByIDsResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_cart_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UpdateCartByIDsResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UpdateCartByIDsResponse) ProtoMessage() {} + +func (x *UpdateCartByIDsResponse) ProtoReflect() protoreflect.Message { + mi := &file_cart_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UpdateCartByIDsResponse.ProtoReflect.Descriptor instead. +func (*UpdateCartByIDsResponse) Descriptor() ([]byte, []int) { + return file_cart_proto_rawDescGZIP(), []int{4} +} + +var File_cart_proto protoreflect.FileDescriptor + +var file_cart_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x63, 0x61, 0x72, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x4a, 0x0a, 0x04, 0x43, 0x61, 0x72, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x67, + 0x6f, 0x6f, 0x64, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x67, 0x6f, 0x6f, + 0x64, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x22, + 0x2d, 0x0a, 0x19, 0x47, 0x65, 0x74, 0x43, 0x61, 0x72, 0x64, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, + 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, + 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x03, 0x52, 0x03, 0x69, 0x64, 0x73, 0x22, 0x3f, + 0x0a, 0x1a, 0x47, 0x65, 0x74, 0x43, 0x61, 0x72, 0x64, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x42, + 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, 0x0a, 0x05, + 0x63, 0x61, 0x72, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x61, 0x72, 0x74, 0x52, 0x05, 0x63, 0x61, 0x72, 0x74, 0x73, 0x22, + 0x46, 0x0a, 0x16, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x43, 0x61, 0x72, 0x74, 0x42, 0x79, 0x49, + 0x44, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x69, 0x64, 0x73, + 0x18, 0x01, 0x20, 0x03, 0x28, 0x03, 0x52, 0x03, 0x69, 0x64, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x73, + 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x73, + 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x22, 0x19, 0x0a, 0x17, 0x55, 0x70, 0x64, 0x61, 0x74, + 0x65, 0x43, 0x61, 0x72, 0x74, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x32, 0xba, 0x01, 0x0a, 0x0b, 0x43, 0x61, 0x72, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x12, 0x59, 0x0a, 0x12, 0x47, 0x65, 0x74, 0x43, 0x61, 0x72, 0x64, 0x44, 0x65, 0x74, + 0x61, 0x69, 0x6c, 0x42, 0x79, 0x49, 0x44, 0x73, 0x12, 0x20, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2e, 0x47, 0x65, 0x74, 0x43, 0x61, 0x72, 0x64, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x42, 0x79, + 0x49, 0x44, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x21, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, 0x43, 0x61, 0x72, 0x64, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, + 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x50, 0x0a, + 0x0f, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x43, 0x61, 0x72, 0x74, 0x42, 0x79, 0x49, 0x44, 0x73, + 0x12, 0x1d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x43, + 0x61, 0x72, 0x74, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x1e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x43, 0x61, + 0x72, 0x74, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, + 0x1b, 0x5a, 0x19, 0x67, 0x6f, 0x2d, 0x6f, 0x70, 0x65, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x69, 0x6e, + 0x67, 0x2d, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_cart_proto_rawDescOnce sync.Once + file_cart_proto_rawDescData = file_cart_proto_rawDesc +) + +func file_cart_proto_rawDescGZIP() []byte { + file_cart_proto_rawDescOnce.Do(func() { + file_cart_proto_rawDescData = protoimpl.X.CompressGZIP(file_cart_proto_rawDescData) + }) + return file_cart_proto_rawDescData +} + +var file_cart_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_cart_proto_goTypes = []interface{}{ + (*Cart)(nil), // 0: proto.Cart + (*GetCardDetailByIDsRequest)(nil), // 1: proto.GetCardDetailByIDsRequest + (*GetCardDetailByIDsResponse)(nil), // 2: proto.GetCardDetailByIDsResponse + (*UpdateCartByIDsRequest)(nil), // 3: proto.UpdateCartByIDsRequest + (*UpdateCartByIDsResponse)(nil), // 4: proto.UpdateCartByIDsResponse +} +var file_cart_proto_depIdxs = []int32{ + 0, // 0: proto.GetCardDetailByIDsResponse.carts:type_name -> proto.Cart + 1, // 1: proto.CartService.GetCardDetailByIDs:input_type -> proto.GetCardDetailByIDsRequest + 3, // 2: proto.CartService.UpdateCartByIDs:input_type -> proto.UpdateCartByIDsRequest + 2, // 3: proto.CartService.GetCardDetailByIDs:output_type -> proto.GetCardDetailByIDsResponse + 4, // 4: proto.CartService.UpdateCartByIDs:output_type -> proto.UpdateCartByIDsResponse + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_cart_proto_init() } +func file_cart_proto_init() { + if File_cart_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_cart_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Cart); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cart_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetCardDetailByIDsRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cart_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetCardDetailByIDsResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cart_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UpdateCartByIDsRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cart_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UpdateCartByIDsResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_cart_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_cart_proto_goTypes, + DependencyIndexes: file_cart_proto_depIdxs, + MessageInfos: file_cart_proto_msgTypes, + }.Build() + File_cart_proto = out.File + file_cart_proto_rawDesc = nil + file_cart_proto_goTypes = nil + file_cart_proto_depIdxs = nil +} diff --git a/pb/cart.proto b/pb/cart.proto new file mode 100644 index 0000000..a49e76b --- /dev/null +++ b/pb/cart.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; +package proto; + +option go_package = "go-opentracing-example/pb"; + +service CartService { + rpc GetCardDetailByIDs(GetCardDetailByIDsRequest) returns (GetCardDetailByIDsResponse) ; // 获取购物车详情,通过购物车ids + rpc UpdateCartByIDs(UpdateCartByIDsRequest) returns (UpdateCartByIDsResponse) ; // 更新购物车数据, 将选中的移除购物车. +} + +message Cart { + int64 id = 1; + int64 goodId = 2; + bool selected = 3; +} + +message GetCardDetailByIDsRequest { + repeated int64 ids = 1; +} + +message GetCardDetailByIDsResponse { + repeated Cart carts = 1; +} + +message UpdateCartByIDsRequest { + repeated int64 ids = 1; + bool selected = 2; +} + +message UpdateCartByIDsResponse { +} \ No newline at end of file diff --git a/pb/cart_grpc.pb.go b/pb/cart_grpc.pb.go new file mode 100644 index 0000000..761cef1 --- /dev/null +++ b/pb/cart_grpc.pb.go @@ -0,0 +1,141 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v4.22.3 +// source: cart.proto + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// CartServiceClient is the client API for CartService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type CartServiceClient interface { + GetCardDetailByIDs(ctx context.Context, in *GetCardDetailByIDsRequest, opts ...grpc.CallOption) (*GetCardDetailByIDsResponse, error) + UpdateCartByIDs(ctx context.Context, in *UpdateCartByIDsRequest, opts ...grpc.CallOption) (*UpdateCartByIDsResponse, error) +} + +type cartServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewCartServiceClient(cc grpc.ClientConnInterface) CartServiceClient { + return &cartServiceClient{cc} +} + +func (c *cartServiceClient) GetCardDetailByIDs(ctx context.Context, in *GetCardDetailByIDsRequest, opts ...grpc.CallOption) (*GetCardDetailByIDsResponse, error) { + out := new(GetCardDetailByIDsResponse) + err := c.cc.Invoke(ctx, "/proto.CartService/GetCardDetailByIDs", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *cartServiceClient) UpdateCartByIDs(ctx context.Context, in *UpdateCartByIDsRequest, opts ...grpc.CallOption) (*UpdateCartByIDsResponse, error) { + out := new(UpdateCartByIDsResponse) + err := c.cc.Invoke(ctx, "/proto.CartService/UpdateCartByIDs", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// CartServiceServer is the server API for CartService service. +// All implementations must embed UnimplementedCartServiceServer +// for forward compatibility +type CartServiceServer interface { + GetCardDetailByIDs(context.Context, *GetCardDetailByIDsRequest) (*GetCardDetailByIDsResponse, error) + UpdateCartByIDs(context.Context, *UpdateCartByIDsRequest) (*UpdateCartByIDsResponse, error) + mustEmbedUnimplementedCartServiceServer() +} + +// UnimplementedCartServiceServer must be embedded to have forward compatible implementations. +type UnimplementedCartServiceServer struct { +} + +func (UnimplementedCartServiceServer) GetCardDetailByIDs(context.Context, *GetCardDetailByIDsRequest) (*GetCardDetailByIDsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetCardDetailByIDs not implemented") +} +func (UnimplementedCartServiceServer) UpdateCartByIDs(context.Context, *UpdateCartByIDsRequest) (*UpdateCartByIDsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpdateCartByIDs not implemented") +} +func (UnimplementedCartServiceServer) mustEmbedUnimplementedCartServiceServer() {} + +// UnsafeCartServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to CartServiceServer will +// result in compilation errors. +type UnsafeCartServiceServer interface { + mustEmbedUnimplementedCartServiceServer() +} + +func RegisterCartServiceServer(s grpc.ServiceRegistrar, srv CartServiceServer) { + s.RegisterService(&CartService_ServiceDesc, srv) +} + +func _CartService_GetCardDetailByIDs_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetCardDetailByIDsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CartServiceServer).GetCardDetailByIDs(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.CartService/GetCardDetailByIDs", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CartServiceServer).GetCardDetailByIDs(ctx, req.(*GetCardDetailByIDsRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _CartService_UpdateCartByIDs_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(UpdateCartByIDsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CartServiceServer).UpdateCartByIDs(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.CartService/UpdateCartByIDs", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CartServiceServer).UpdateCartByIDs(ctx, req.(*UpdateCartByIDsRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// CartService_ServiceDesc is the grpc.ServiceDesc for CartService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var CartService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.CartService", + HandlerType: (*CartServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetCardDetailByIDs", + Handler: _CartService_GetCardDetailByIDs_Handler, + }, + { + MethodName: "UpdateCartByIDs", + Handler: _CartService_UpdateCartByIDs_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "cart.proto", +} diff --git a/pb/good.pb.go b/pb/good.pb.go new file mode 100644 index 0000000..198aa59 --- /dev/null +++ b/pb/good.pb.go @@ -0,0 +1,502 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.30.0 +// protoc v4.22.3 +// source: good.proto + +package pb + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Good struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + Stoke int64 `protobuf:"varint,3,opt,name=stoke,proto3" json:"stoke,omitempty"` // 库存. + Price float64 `protobuf:"fixed64,4,opt,name=price,proto3" json:"price,omitempty"` +} + +func (x *Good) Reset() { + *x = Good{} + if protoimpl.UnsafeEnabled { + mi := &file_good_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Good) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Good) ProtoMessage() {} + +func (x *Good) ProtoReflect() protoreflect.Message { + mi := &file_good_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Good.ProtoReflect.Descriptor instead. +func (*Good) Descriptor() ([]byte, []int) { + return file_good_proto_rawDescGZIP(), []int{0} +} + +func (x *Good) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Good) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Good) GetStoke() int64 { + if x != nil { + return x.Stoke + } + return 0 +} + +func (x *Good) GetPrice() float64 { + if x != nil { + return x.Price + } + return 0 +} + +type GetGoodsByIDsRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ids []int64 `protobuf:"varint,1,rep,packed,name=ids,proto3" json:"ids,omitempty"` +} + +func (x *GetGoodsByIDsRequest) Reset() { + *x = GetGoodsByIDsRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_good_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetGoodsByIDsRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetGoodsByIDsRequest) ProtoMessage() {} + +func (x *GetGoodsByIDsRequest) ProtoReflect() protoreflect.Message { + mi := &file_good_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetGoodsByIDsRequest.ProtoReflect.Descriptor instead. +func (*GetGoodsByIDsRequest) Descriptor() ([]byte, []int) { + return file_good_proto_rawDescGZIP(), []int{1} +} + +func (x *GetGoodsByIDsRequest) GetIds() []int64 { + if x != nil { + return x.Ids + } + return nil +} + +type GetGoodsByIDsResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Goods []*Good `protobuf:"bytes,1,rep,name=goods,proto3" json:"goods,omitempty"` +} + +func (x *GetGoodsByIDsResponse) Reset() { + *x = GetGoodsByIDsResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_good_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetGoodsByIDsResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetGoodsByIDsResponse) ProtoMessage() {} + +func (x *GetGoodsByIDsResponse) ProtoReflect() protoreflect.Message { + mi := &file_good_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetGoodsByIDsResponse.ProtoReflect.Descriptor instead. +func (*GetGoodsByIDsResponse) Descriptor() ([]byte, []int) { + return file_good_proto_rawDescGZIP(), []int{2} +} + +func (x *GetGoodsByIDsResponse) GetGoods() []*Good { + if x != nil { + return x.Goods + } + return nil +} + +type StokeRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + GoodId int64 `protobuf:"varint,1,opt,name=goodId,proto3" json:"goodId,omitempty"` // 商品id + Stoke int64 `protobuf:"varint,2,opt,name=stoke,proto3" json:"stoke,omitempty"` // 库存数量 +} + +func (x *StokeRequest) Reset() { + *x = StokeRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_good_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *StokeRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*StokeRequest) ProtoMessage() {} + +func (x *StokeRequest) ProtoReflect() protoreflect.Message { + mi := &file_good_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use StokeRequest.ProtoReflect.Descriptor instead. +func (*StokeRequest) Descriptor() ([]byte, []int) { + return file_good_proto_rawDescGZIP(), []int{3} +} + +func (x *StokeRequest) GetGoodId() int64 { + if x != nil { + return x.GoodId + } + return 0 +} + +func (x *StokeRequest) GetStoke() int64 { + if x != nil { + return x.Stoke + } + return 0 +} + +type UpdateGoodsStokeRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Requests []*StokeRequest `protobuf:"bytes,1,rep,name=requests,proto3" json:"requests,omitempty"` +} + +func (x *UpdateGoodsStokeRequest) Reset() { + *x = UpdateGoodsStokeRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_good_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UpdateGoodsStokeRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UpdateGoodsStokeRequest) ProtoMessage() {} + +func (x *UpdateGoodsStokeRequest) ProtoReflect() protoreflect.Message { + mi := &file_good_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UpdateGoodsStokeRequest.ProtoReflect.Descriptor instead. +func (*UpdateGoodsStokeRequest) Descriptor() ([]byte, []int) { + return file_good_proto_rawDescGZIP(), []int{4} +} + +func (x *UpdateGoodsStokeRequest) GetRequests() []*StokeRequest { + if x != nil { + return x.Requests + } + return nil +} + +type UpdateGoodsStokeResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *UpdateGoodsStokeResponse) Reset() { + *x = UpdateGoodsStokeResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_good_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UpdateGoodsStokeResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UpdateGoodsStokeResponse) ProtoMessage() {} + +func (x *UpdateGoodsStokeResponse) ProtoReflect() protoreflect.Message { + mi := &file_good_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UpdateGoodsStokeResponse.ProtoReflect.Descriptor instead. +func (*UpdateGoodsStokeResponse) Descriptor() ([]byte, []int) { + return file_good_proto_rawDescGZIP(), []int{5} +} + +var File_good_proto protoreflect.FileDescriptor + +var file_good_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x67, 0x6f, 0x6f, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x56, 0x0a, 0x04, 0x47, 0x6f, 0x6f, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, + 0x14, 0x0a, 0x05, 0x73, 0x74, 0x6f, 0x6b, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, + 0x73, 0x74, 0x6f, 0x6b, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x22, 0x28, 0x0a, 0x14, 0x47, + 0x65, 0x74, 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x69, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x03, + 0x52, 0x03, 0x69, 0x64, 0x73, 0x22, 0x3a, 0x0a, 0x15, 0x47, 0x65, 0x74, 0x47, 0x6f, 0x6f, 0x64, + 0x73, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x21, + 0x0a, 0x05, 0x67, 0x6f, 0x6f, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x6f, 0x6f, 0x64, 0x52, 0x05, 0x67, 0x6f, 0x6f, 0x64, + 0x73, 0x22, 0x3c, 0x0a, 0x0c, 0x73, 0x74, 0x6f, 0x6b, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x16, 0x0a, 0x06, 0x67, 0x6f, 0x6f, 0x64, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x06, 0x67, 0x6f, 0x6f, 0x64, 0x49, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x6f, + 0x6b, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x73, 0x74, 0x6f, 0x6b, 0x65, 0x22, + 0x4a, 0x0a, 0x17, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x53, 0x74, + 0x6f, 0x6b, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2f, 0x0a, 0x08, 0x72, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x73, 0x74, 0x6f, 0x6b, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x52, 0x08, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x73, 0x22, 0x1a, 0x0a, 0x18, 0x55, + 0x70, 0x64, 0x61, 0x74, 0x65, 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x53, 0x74, 0x6f, 0x6b, 0x65, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0xad, 0x01, 0x0a, 0x0b, 0x47, 0x6f, 0x6f, 0x64, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x49, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x47, 0x6f, + 0x6f, 0x64, 0x73, 0x42, 0x79, 0x49, 0x44, 0x12, 0x1b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, + 0x47, 0x65, 0x74, 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x47, 0x65, 0x74, + 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x42, 0x79, 0x49, 0x44, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x53, 0x0a, 0x10, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x47, 0x6f, 0x6f, 0x64, + 0x73, 0x53, 0x74, 0x6f, 0x6b, 0x65, 0x12, 0x1e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55, + 0x70, 0x64, 0x61, 0x74, 0x65, 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x53, 0x74, 0x6f, 0x6b, 0x65, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x55, + 0x70, 0x64, 0x61, 0x74, 0x65, 0x47, 0x6f, 0x6f, 0x64, 0x73, 0x53, 0x74, 0x6f, 0x6b, 0x65, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x1b, 0x5a, 0x19, 0x67, 0x6f, 0x2d, 0x6f, 0x70, + 0x65, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2d, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_good_proto_rawDescOnce sync.Once + file_good_proto_rawDescData = file_good_proto_rawDesc +) + +func file_good_proto_rawDescGZIP() []byte { + file_good_proto_rawDescOnce.Do(func() { + file_good_proto_rawDescData = protoimpl.X.CompressGZIP(file_good_proto_rawDescData) + }) + return file_good_proto_rawDescData +} + +var file_good_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_good_proto_goTypes = []interface{}{ + (*Good)(nil), // 0: proto.Good + (*GetGoodsByIDsRequest)(nil), // 1: proto.GetGoodsByIDsRequest + (*GetGoodsByIDsResponse)(nil), // 2: proto.GetGoodsByIDsResponse + (*StokeRequest)(nil), // 3: proto.stokeRequest + (*UpdateGoodsStokeRequest)(nil), // 4: proto.UpdateGoodsStokeRequest + (*UpdateGoodsStokeResponse)(nil), // 5: proto.UpdateGoodsStokeResponse +} +var file_good_proto_depIdxs = []int32{ + 0, // 0: proto.GetGoodsByIDsResponse.goods:type_name -> proto.Good + 3, // 1: proto.UpdateGoodsStokeRequest.requests:type_name -> proto.stokeRequest + 1, // 2: proto.GoodService.GetGoodsByID:input_type -> proto.GetGoodsByIDsRequest + 4, // 3: proto.GoodService.UpdateGoodsStoke:input_type -> proto.UpdateGoodsStokeRequest + 2, // 4: proto.GoodService.GetGoodsByID:output_type -> proto.GetGoodsByIDsResponse + 5, // 5: proto.GoodService.UpdateGoodsStoke:output_type -> proto.UpdateGoodsStokeResponse + 4, // [4:6] is the sub-list for method output_type + 2, // [2:4] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name +} + +func init() { file_good_proto_init() } +func file_good_proto_init() { + if File_good_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_good_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Good); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_good_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetGoodsByIDsRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_good_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetGoodsByIDsResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_good_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*StokeRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_good_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UpdateGoodsStokeRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_good_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UpdateGoodsStokeResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_good_proto_rawDesc, + NumEnums: 0, + NumMessages: 6, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_good_proto_goTypes, + DependencyIndexes: file_good_proto_depIdxs, + MessageInfos: file_good_proto_msgTypes, + }.Build() + File_good_proto = out.File + file_good_proto_rawDesc = nil + file_good_proto_goTypes = nil + file_good_proto_depIdxs = nil +} diff --git a/pb/good.proto b/pb/good.proto new file mode 100644 index 0000000..4357f10 --- /dev/null +++ b/pb/good.proto @@ -0,0 +1,37 @@ +syntax = "proto3"; +package proto; + +option go_package = "go-opentracing-example/pb"; + +service GoodService { + rpc GetGoodsByID(GetGoodsByIDsRequest) returns (GetGoodsByIDsResponse) ; + rpc UpdateGoodsStoke(UpdateGoodsStokeRequest) returns (UpdateGoodsStokeResponse) ; +} + +message Good { + int64 id = 1; + string name = 2; + int64 stoke = 3; // 库存. + double price = 4; +} + +message GetGoodsByIDsRequest { + repeated int64 ids = 1; +} + +message GetGoodsByIDsResponse { + repeated Good goods = 1; +} + +message stokeRequest { + int64 goodId = 1; // 商品id + int64 stoke = 2; // 库存数量 +} + +message UpdateGoodsStokeRequest{ + repeated stokeRequest requests = 1; +} + +message UpdateGoodsStokeResponse { + +} \ No newline at end of file diff --git a/pb/good_grpc.pb.go b/pb/good_grpc.pb.go new file mode 100644 index 0000000..29a2dc0 --- /dev/null +++ b/pb/good_grpc.pb.go @@ -0,0 +1,141 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v4.22.3 +// source: good.proto + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// GoodServiceClient is the client API for GoodService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type GoodServiceClient interface { + GetGoodsByID(ctx context.Context, in *GetGoodsByIDsRequest, opts ...grpc.CallOption) (*GetGoodsByIDsResponse, error) + UpdateGoodsStoke(ctx context.Context, in *UpdateGoodsStokeRequest, opts ...grpc.CallOption) (*UpdateGoodsStokeResponse, error) +} + +type goodServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewGoodServiceClient(cc grpc.ClientConnInterface) GoodServiceClient { + return &goodServiceClient{cc} +} + +func (c *goodServiceClient) GetGoodsByID(ctx context.Context, in *GetGoodsByIDsRequest, opts ...grpc.CallOption) (*GetGoodsByIDsResponse, error) { + out := new(GetGoodsByIDsResponse) + err := c.cc.Invoke(ctx, "/proto.GoodService/GetGoodsByID", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *goodServiceClient) UpdateGoodsStoke(ctx context.Context, in *UpdateGoodsStokeRequest, opts ...grpc.CallOption) (*UpdateGoodsStokeResponse, error) { + out := new(UpdateGoodsStokeResponse) + err := c.cc.Invoke(ctx, "/proto.GoodService/UpdateGoodsStoke", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GoodServiceServer is the server API for GoodService service. +// All implementations must embed UnimplementedGoodServiceServer +// for forward compatibility +type GoodServiceServer interface { + GetGoodsByID(context.Context, *GetGoodsByIDsRequest) (*GetGoodsByIDsResponse, error) + UpdateGoodsStoke(context.Context, *UpdateGoodsStokeRequest) (*UpdateGoodsStokeResponse, error) + mustEmbedUnimplementedGoodServiceServer() +} + +// UnimplementedGoodServiceServer must be embedded to have forward compatible implementations. +type UnimplementedGoodServiceServer struct { +} + +func (UnimplementedGoodServiceServer) GetGoodsByID(context.Context, *GetGoodsByIDsRequest) (*GetGoodsByIDsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetGoodsByID not implemented") +} +func (UnimplementedGoodServiceServer) UpdateGoodsStoke(context.Context, *UpdateGoodsStokeRequest) (*UpdateGoodsStokeResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpdateGoodsStoke not implemented") +} +func (UnimplementedGoodServiceServer) mustEmbedUnimplementedGoodServiceServer() {} + +// UnsafeGoodServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to GoodServiceServer will +// result in compilation errors. +type UnsafeGoodServiceServer interface { + mustEmbedUnimplementedGoodServiceServer() +} + +func RegisterGoodServiceServer(s grpc.ServiceRegistrar, srv GoodServiceServer) { + s.RegisterService(&GoodService_ServiceDesc, srv) +} + +func _GoodService_GetGoodsByID_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetGoodsByIDsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GoodServiceServer).GetGoodsByID(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.GoodService/GetGoodsByID", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GoodServiceServer).GetGoodsByID(ctx, req.(*GetGoodsByIDsRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _GoodService_UpdateGoodsStoke_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(UpdateGoodsStokeRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GoodServiceServer).UpdateGoodsStoke(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.GoodService/UpdateGoodsStoke", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GoodServiceServer).UpdateGoodsStoke(ctx, req.(*UpdateGoodsStokeRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// GoodService_ServiceDesc is the grpc.ServiceDesc for GoodService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var GoodService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.GoodService", + HandlerType: (*GoodServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetGoodsByID", + Handler: _GoodService_GetGoodsByID_Handler, + }, + { + MethodName: "UpdateGoodsStoke", + Handler: _GoodService_UpdateGoodsStoke_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "good.proto", +} diff --git a/pb/order.pb.go b/pb/order.pb.go new file mode 100644 index 0000000..05c7140 --- /dev/null +++ b/pb/order.pb.go @@ -0,0 +1,509 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.30.0 +// protoc v4.22.3 +// source: order.proto + +package pb + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Order_Status int32 + +const ( + Order_UNKNOWN Order_Status = 0 + Order_Created Order_Status = 1 // 已创建 + Order_Paying Order_Status = 2 // 正在支付 + Order_Payed Order_Status = 3 // 支付完成 + Order_Cancel Order_Status = 4 // 已取消 +) + +// Enum value maps for Order_Status. +var ( + Order_Status_name = map[int32]string{ + 0: "UNKNOWN", + 1: "Created", + 2: "Paying", + 3: "Payed", + 4: "Cancel", + } + Order_Status_value = map[string]int32{ + "UNKNOWN": 0, + "Created": 1, + "Paying": 2, + "Payed": 3, + "Cancel": 4, + } +) + +func (x Order_Status) Enum() *Order_Status { + p := new(Order_Status) + *p = x + return p +} + +func (x Order_Status) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Order_Status) Descriptor() protoreflect.EnumDescriptor { + return file_order_proto_enumTypes[0].Descriptor() +} + +func (Order_Status) Type() protoreflect.EnumType { + return &file_order_proto_enumTypes[0] +} + +func (x Order_Status) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Order_Status.Descriptor instead. +func (Order_Status) EnumDescriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{0, 0} +} + +type Order struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Uid int64 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"` + Price float64 `protobuf:"fixed64,3,opt,name=price,proto3" json:"price,omitempty"` + DiscountAmount float64 `protobuf:"fixed64,4,opt,name=discountAmount,proto3" json:"discountAmount,omitempty"` + Status Order_Status `protobuf:"varint,5,opt,name=status,proto3,enum=proto.Order_Status" json:"status,omitempty"` // 状态 + CreatedAt int64 `protobuf:"varint,6,opt,name=createdAt,proto3" json:"createdAt,omitempty"` // 创建时间. +} + +func (x *Order) Reset() { + *x = Order{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Order) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Order) ProtoMessage() {} + +func (x *Order) ProtoReflect() protoreflect.Message { + mi := &file_order_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Order.ProtoReflect.Descriptor instead. +func (*Order) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{0} +} + +func (x *Order) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Order) GetUid() int64 { + if x != nil { + return x.Uid + } + return 0 +} + +func (x *Order) GetPrice() float64 { + if x != nil { + return x.Price + } + return 0 +} + +func (x *Order) GetDiscountAmount() float64 { + if x != nil { + return x.DiscountAmount + } + return 0 +} + +func (x *Order) GetStatus() Order_Status { + if x != nil { + return x.Status + } + return Order_UNKNOWN +} + +func (x *Order) GetCreatedAt() int64 { + if x != nil { + return x.CreatedAt + } + return 0 +} + +type OrderGood struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + GoodId int64 `protobuf:"varint,2,opt,name=goodId,proto3" json:"goodId,omitempty"` + OrderId int64 `protobuf:"varint,3,opt,name=orderId,proto3" json:"orderId,omitempty"` + GoodName int64 `protobuf:"varint,4,opt,name=goodName,proto3" json:"goodName,omitempty"` + // .. + Price float64 `protobuf:"fixed64,5,opt,name=price,proto3" json:"price,omitempty"` // 价格 + Number int64 `protobuf:"varint,6,opt,name=number,proto3" json:"number,omitempty"` +} + +func (x *OrderGood) Reset() { + *x = OrderGood{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *OrderGood) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*OrderGood) ProtoMessage() {} + +func (x *OrderGood) ProtoReflect() protoreflect.Message { + mi := &file_order_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use OrderGood.ProtoReflect.Descriptor instead. +func (*OrderGood) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{1} +} + +func (x *OrderGood) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *OrderGood) GetGoodId() int64 { + if x != nil { + return x.GoodId + } + return 0 +} + +func (x *OrderGood) GetOrderId() int64 { + if x != nil { + return x.OrderId + } + return 0 +} + +func (x *OrderGood) GetGoodName() int64 { + if x != nil { + return x.GoodName + } + return 0 +} + +func (x *OrderGood) GetPrice() float64 { + if x != nil { + return x.Price + } + return 0 +} + +func (x *OrderGood) GetNumber() int64 { + if x != nil { + return x.Number + } + return 0 +} + +type CreateOrderRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + CartIds []int64 `protobuf:"varint,1,rep,packed,name=cartIds,proto3" json:"cartIds,omitempty"` // 购物车 id + CouponId int64 `protobuf:"varint,2,opt,name=couponId,proto3" json:"couponId,omitempty"` // 优惠券id +} + +func (x *CreateOrderRequest) Reset() { + *x = CreateOrderRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CreateOrderRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateOrderRequest) ProtoMessage() {} + +func (x *CreateOrderRequest) ProtoReflect() protoreflect.Message { + mi := &file_order_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CreateOrderRequest.ProtoReflect.Descriptor instead. +func (*CreateOrderRequest) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{2} +} + +func (x *CreateOrderRequest) GetCartIds() []int64 { + if x != nil { + return x.CartIds + } + return nil +} + +func (x *CreateOrderRequest) GetCouponId() int64 { + if x != nil { + return x.CouponId + } + return 0 +} + +type CreateOrderResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + OrderId int64 `protobuf:"varint,1,opt,name=orderId,proto3" json:"orderId,omitempty"` +} + +func (x *CreateOrderResponse) Reset() { + *x = CreateOrderResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CreateOrderResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateOrderResponse) ProtoMessage() {} + +func (x *CreateOrderResponse) ProtoReflect() protoreflect.Message { + mi := &file_order_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CreateOrderResponse.ProtoReflect.Descriptor instead. +func (*CreateOrderResponse) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{3} +} + +func (x *CreateOrderResponse) GetOrderId() int64 { + if x != nil { + return x.OrderId + } + return 0 +} + +var File_order_proto protoreflect.FileDescriptor + +var file_order_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xf9, 0x01, 0x0a, 0x05, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x0e, + 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x10, + 0x0a, 0x03, 0x75, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x75, 0x69, 0x64, + 0x12, 0x14, 0x0a, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, + 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x12, 0x26, 0x0a, 0x0e, 0x64, 0x69, 0x73, 0x63, 0x6f, 0x75, + 0x6e, 0x74, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x01, 0x52, 0x0e, + 0x64, 0x69, 0x73, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x2b, + 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x13, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x2e, 0x53, 0x74, 0x61, + 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x63, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, + 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x22, 0x45, 0x0a, 0x06, 0x53, 0x74, 0x61, + 0x74, 0x75, 0x73, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, + 0x12, 0x0b, 0x0a, 0x07, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x10, 0x01, 0x12, 0x0a, 0x0a, + 0x06, 0x50, 0x61, 0x79, 0x69, 0x6e, 0x67, 0x10, 0x02, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x61, 0x79, + 0x65, 0x64, 0x10, 0x03, 0x12, 0x0a, 0x0a, 0x06, 0x43, 0x61, 0x6e, 0x63, 0x65, 0x6c, 0x10, 0x04, + 0x22, 0x97, 0x01, 0x0a, 0x09, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x47, 0x6f, 0x6f, 0x64, 0x12, 0x0e, + 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x16, + 0x0a, 0x06, 0x67, 0x6f, 0x6f, 0x64, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, + 0x67, 0x6f, 0x6f, 0x64, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, + 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, 0x64, + 0x12, 0x1a, 0x0a, 0x08, 0x67, 0x6f, 0x6f, 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x08, 0x67, 0x6f, 0x6f, 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, + 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x70, 0x72, 0x69, + 0x63, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x06, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x4a, 0x0a, 0x12, 0x43, 0x72, + 0x65, 0x61, 0x74, 0x65, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x12, 0x18, 0x0a, 0x07, 0x63, 0x61, 0x72, 0x74, 0x49, 0x64, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, + 0x03, 0x52, 0x07, 0x63, 0x61, 0x72, 0x74, 0x49, 0x64, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x6f, + 0x75, 0x70, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x63, 0x6f, + 0x75, 0x70, 0x6f, 0x6e, 0x49, 0x64, 0x22, 0x2f, 0x0a, 0x13, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, + 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, + 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, + 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, 0x64, 0x32, 0x54, 0x0a, 0x0c, 0x4f, 0x72, 0x64, 0x65, 0x72, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x44, 0x0a, 0x0b, 0x43, 0x72, 0x65, 0x61, 0x74, + 0x65, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x19, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x1a, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, + 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x1b, 0x5a, + 0x19, 0x67, 0x6f, 0x2d, 0x6f, 0x70, 0x65, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x69, 0x6e, 0x67, 0x2d, + 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_order_proto_rawDescOnce sync.Once + file_order_proto_rawDescData = file_order_proto_rawDesc +) + +func file_order_proto_rawDescGZIP() []byte { + file_order_proto_rawDescOnce.Do(func() { + file_order_proto_rawDescData = protoimpl.X.CompressGZIP(file_order_proto_rawDescData) + }) + return file_order_proto_rawDescData +} + +var file_order_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_order_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_order_proto_goTypes = []interface{}{ + (Order_Status)(0), // 0: proto.Order.Status + (*Order)(nil), // 1: proto.Order + (*OrderGood)(nil), // 2: proto.OrderGood + (*CreateOrderRequest)(nil), // 3: proto.CreateOrderRequest + (*CreateOrderResponse)(nil), // 4: proto.CreateOrderResponse +} +var file_order_proto_depIdxs = []int32{ + 0, // 0: proto.Order.status:type_name -> proto.Order.Status + 3, // 1: proto.OrderService.CreateOrder:input_type -> proto.CreateOrderRequest + 4, // 2: proto.OrderService.CreateOrder:output_type -> proto.CreateOrderResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_order_proto_init() } +func file_order_proto_init() { + if File_order_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_order_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Order); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_order_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*OrderGood); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_order_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CreateOrderRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_order_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CreateOrderResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_order_proto_rawDesc, + NumEnums: 1, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_order_proto_goTypes, + DependencyIndexes: file_order_proto_depIdxs, + EnumInfos: file_order_proto_enumTypes, + MessageInfos: file_order_proto_msgTypes, + }.Build() + File_order_proto = out.File + file_order_proto_rawDesc = nil + file_order_proto_goTypes = nil + file_order_proto_depIdxs = nil +} diff --git a/pb/order.proto b/pb/order.proto new file mode 100644 index 0000000..ed54b9a --- /dev/null +++ b/pb/order.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; +package proto; + +option go_package = "go-opentracing-example/pb"; + +service OrderService { + rpc CreateOrder(CreateOrderRequest) returns (CreateOrderResponse) ; +} + +message Order { + int64 id = 1; + enum Status { + UNKNOWN = 0 ; + Created = 1; // 已创建 + Paying = 2; // 正在支付 + Payed = 3; // 支付完成 + Cancel = 4; // 已取消 + // ... todo + } + int64 uid = 2; + double price = 3; + double discountAmount = 4; + Status status = 5; // 状态 + int64 createdAt = 6; // 创建时间. +} + +message OrderGood { + int64 id = 1; + int64 goodId = 2; + int64 orderId = 3; + int64 goodName = 4; + //.. + double price = 5; // 价格 + int64 number = 6; +} + +message CreateOrderRequest { + repeated int64 cartIds = 1; // 购物车 id + int64 couponId = 2; // 优惠券id +} + +message CreateOrderResponse { + int64 orderId = 1; +} \ No newline at end of file diff --git a/pb/order_grpc.pb.go b/pb/order_grpc.pb.go new file mode 100644 index 0000000..e9c40e1 --- /dev/null +++ b/pb/order_grpc.pb.go @@ -0,0 +1,105 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v4.22.3 +// source: order.proto + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// OrderServiceClient is the client API for OrderService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type OrderServiceClient interface { + CreateOrder(ctx context.Context, in *CreateOrderRequest, opts ...grpc.CallOption) (*CreateOrderResponse, error) +} + +type orderServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewOrderServiceClient(cc grpc.ClientConnInterface) OrderServiceClient { + return &orderServiceClient{cc} +} + +func (c *orderServiceClient) CreateOrder(ctx context.Context, in *CreateOrderRequest, opts ...grpc.CallOption) (*CreateOrderResponse, error) { + out := new(CreateOrderResponse) + err := c.cc.Invoke(ctx, "/proto.OrderService/CreateOrder", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// OrderServiceServer is the server API for OrderService service. +// All implementations must embed UnimplementedOrderServiceServer +// for forward compatibility +type OrderServiceServer interface { + CreateOrder(context.Context, *CreateOrderRequest) (*CreateOrderResponse, error) + mustEmbedUnimplementedOrderServiceServer() +} + +// UnimplementedOrderServiceServer must be embedded to have forward compatible implementations. +type UnimplementedOrderServiceServer struct { +} + +func (UnimplementedOrderServiceServer) CreateOrder(context.Context, *CreateOrderRequest) (*CreateOrderResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method CreateOrder not implemented") +} +func (UnimplementedOrderServiceServer) mustEmbedUnimplementedOrderServiceServer() {} + +// UnsafeOrderServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to OrderServiceServer will +// result in compilation errors. +type UnsafeOrderServiceServer interface { + mustEmbedUnimplementedOrderServiceServer() +} + +func RegisterOrderServiceServer(s grpc.ServiceRegistrar, srv OrderServiceServer) { + s.RegisterService(&OrderService_ServiceDesc, srv) +} + +func _OrderService_CreateOrder_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CreateOrderRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(OrderServiceServer).CreateOrder(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/proto.OrderService/CreateOrder", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(OrderServiceServer).CreateOrder(ctx, req.(*CreateOrderRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// OrderService_ServiceDesc is the grpc.ServiceDesc for OrderService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var OrderService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.OrderService", + HandlerType: (*OrderServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "CreateOrder", + Handler: _OrderService_CreateOrder_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "order.proto", +} diff --git a/pkg/event/order.go b/pkg/event/order.go new file mode 100644 index 0000000..fb5bdc9 --- /dev/null +++ b/pkg/event/order.go @@ -0,0 +1,21 @@ +package event + +import ( + "go-opentracing-example/pb" + "log" +) + +type CreateOrderPublisher interface { + Notify(order *pb.Order) +} + +func New() CreateOrderPublisher { + return &createOrderPublisher{} +} + +type createOrderPublisher struct { +} + +func (c createOrderPublisher) Notify(order *pb.Order) { + log.Printf("推送创建订单事件:%v \n", order) +} diff --git a/pkg/logger/format.go b/pkg/logger/format.go new file mode 100644 index 0000000..fb65664 --- /dev/null +++ b/pkg/logger/format.go @@ -0,0 +1,26 @@ +package logger + +import ( + "github.com/sirupsen/logrus" + "go-opentracing-example/pkg/requestid" +) + +func init() { + logrus.SetFormatter(&logFormatter{ + JSONFormatter: &logrus.JSONFormatter{}, + }) +} + +type logFormatter struct { + *logrus.JSONFormatter +} + +func (f *logFormatter) Format(entry *logrus.Entry) ([]byte, error) { + requestId := requestid.RequestID(entry.Context) + + if len(requestId) > 0 { + entry.Data["x-request-id"] = requestId + } + + return f.JSONFormatter.Format(entry) +} diff --git a/pkg/logger/log.go b/pkg/logger/log.go new file mode 100644 index 0000000..fbd9b83 --- /dev/null +++ b/pkg/logger/log.go @@ -0,0 +1,17 @@ +package logger + +import ( + "context" + "github.com/sirupsen/logrus" +) + +type Logger interface { + logrus.FieldLogger +} + +func WithContext(ctx context.Context, module ...string) Logger { + if len(module) > 0 { + return logrus.StandardLogger().WithContext(ctx).WithField("module", module[0]) + } + return logrus.StandardLogger().WithContext(ctx) +} diff --git a/pkg/requestid/context.go b/pkg/requestid/context.go new file mode 100644 index 0000000..afedcb3 --- /dev/null +++ b/pkg/requestid/context.go @@ -0,0 +1,22 @@ +package requestid + +import ( + "context" + "github.com/google/uuid" +) + +const ( + key = "x-request-id" +) + +func NewWithContext(ctx context.Context, id string) context.Context { + if id == "" { + id = uuid.New().String() + } + return context.WithValue(ctx, key, id) +} + +func RequestID(ctx context.Context) string { + reqid, _ := ctx.Value(key).(string) + return reqid +} diff --git a/pkg/requestid/middleware.go b/pkg/requestid/middleware.go new file mode 100644 index 0000000..bcccb24 --- /dev/null +++ b/pkg/requestid/middleware.go @@ -0,0 +1,18 @@ +package requestid + +import ( + "context" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func WithRequestID() grpc.UnaryServerInterceptor { + return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { + md, _ := metadata.FromIncomingContext(ctx) + reqid := md.Get("x-request-id") + if len(reqid) > 0 { + ctx = NewWithContext(ctx, reqid[0]) + } + return handler(ctx, req) + } +} diff --git a/pkg/sleep/random.go b/pkg/sleep/random.go new file mode 100644 index 0000000..eaad8a2 --- /dev/null +++ b/pkg/sleep/random.go @@ -0,0 +1,11 @@ +package sleep + +import ( + "math/rand" + "time" +) + +func SleepRandom() { + n := rand.Intn(1000) + time.Sleep(time.Duration(n) * time.Millisecond) +} diff --git a/pkg/tracing/middleware.go b/pkg/tracing/middleware.go new file mode 100644 index 0000000..17cfd23 --- /dev/null +++ b/pkg/tracing/middleware.go @@ -0,0 +1,18 @@ +package tracing + +import ( + "context" + "github.com/opentracing/opentracing-go" + "google.golang.org/grpc" +) + +func Middleware() grpc.UnaryServerInterceptor { + return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { + sp := opentracing.SpanFromContext(ctx) + if sp == nil { + sp, ctx = opentracing.StartSpanFromContext(ctx, info.FullMethod) + } + + return handler(ctx, req) + } +} diff --git a/pkg/tracing/tracing.go b/pkg/tracing/tracing.go new file mode 100644 index 0000000..8681f11 --- /dev/null +++ b/pkg/tracing/tracing.go @@ -0,0 +1,34 @@ +package tracing + +import ( + "fmt" + "io" + "os" + + opentracing "github.com/opentracing/opentracing-go" + jaeger "github.com/uber/jaeger-client-go" + config "github.com/uber/jaeger-client-go/config" +) + +func init() { + os.Setenv("JAEGER_AGENT_HOST", "http://192.168.1.101:5778") +} + +// Init returns an instance of Jaeger Tracer that samples 100% of traces and logs all spans to stdout. +func Init(service string) (opentracing.Tracer, io.Closer) { + cfg := &config.Configuration{ + ServiceName: service, + Sampler: &config.SamplerConfig{ + Type: "const", + Param: 1, + }, + Reporter: &config.ReporterConfig{ + LogSpans: true, + }, + } + tracer, closer, err := cfg.NewTracer(config.Logger(jaeger.StdLogger)) + if err != nil { + panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err)) + } + return tracer, closer +} diff --git a/server/cart/cart_service.go b/server/cart/cart_service.go new file mode 100644 index 0000000..f967aed --- /dev/null +++ b/server/cart/cart_service.go @@ -0,0 +1,46 @@ +package cart + +import ( + context "context" + "go-opentracing-example/pb" + "go-opentracing-example/pkg/logger" + "go-opentracing-example/pkg/sleep" +) + +type CartService struct { + pb.UnimplementedCartServiceServer +} + +func New() *CartService { + return &CartService{} +} + +func (c *CartService) GetCardDetailByIDs(ctx context.Context, request *pb.GetCardDetailByIDsRequest) (*pb.GetCardDetailByIDsResponse, error) { + + logger.WithContext(ctx, "CartService.GetCardDetailByIDs").Infof("获取cart: %v", request.Ids) + + sleep.SleepRandom() + + return &pb.GetCardDetailByIDsResponse{ + Carts: []*pb.Cart{ + { + Id: 1, + GoodId: 1, + Selected: true, + }, + { + Id: 2, + GoodId: 2, + Selected: true, + }, + }, + }, nil +} + +func (c *CartService) UpdateCartByIDs(ctx context.Context, request *pb.UpdateCartByIDsRequest) (*pb.UpdateCartByIDsResponse, error) { + logger.WithContext(ctx, "CartService.UpdateCartByIDs").Infof("更新 cart: %v, ids=%v", request.Ids, request.Selected) + + sleep.SleepRandom() + + return &pb.UpdateCartByIDsResponse{}, nil +} diff --git a/server/good/good_service.go b/server/good/good_service.go new file mode 100644 index 0000000..4bc4a02 --- /dev/null +++ b/server/good/good_service.go @@ -0,0 +1,49 @@ +package good + +import ( + "context" + "go-opentracing-example/pb" + "go-opentracing-example/pkg/logger" + "go-opentracing-example/pkg/sleep" +) + +type GoodService struct { + pb.UnimplementedGoodServiceServer +} + +func New() *GoodService { + return &GoodService{} +} + +func (s *GoodService) GetGoodsByID(ctx context.Context, req *pb.GetGoodsByIDsRequest) (*pb.GetGoodsByIDsResponse, error) { + logger.WithContext(ctx, "GoodService.GetGoodsByID").Infof("获取商品详情: %v", req.Ids) + sleep.SleepRandom() + return &pb.GetGoodsByIDsResponse{ + Goods: []*pb.Good{ + { + Id: 1, + Name: "商品1", + Stoke: 999, + Price: 80, + }, + { + Id: 2, + Name: "商品2", + Stoke: 999, + Price: 100, + }, + { + Id: 3, + Name: "商品3", + Stoke: 999, + Price: 80, + }, + }, + }, nil +} + +func (s *GoodService) UpdateGoodsStoke(ctx context.Context, req *pb.UpdateGoodsStokeRequest) (*pb.UpdateGoodsStokeResponse, error) { + logger.WithContext(ctx, "GoodService.UpdateGoodsStoke").Infof("更新商品详情: %v", req.Requests) + sleep.SleepRandom() + return &pb.UpdateGoodsStokeResponse{}, nil +} diff --git a/server/order/order_servicec.go b/server/order/order_servicec.go new file mode 100644 index 0000000..c3af637 --- /dev/null +++ b/server/order/order_servicec.go @@ -0,0 +1,117 @@ +package order + +import ( + context "context" + "go-opentracing-example/pb" + "go-opentracing-example/pkg/event" + "go-opentracing-example/pkg/logger" + "time" +) + +type OrderService struct { + cartClient pb.CartServiceClient + goodClient pb.GoodServiceClient + publisher event.CreateOrderPublisher + + pb.UnimplementedOrderServiceServer +} + +func NewOrderService( + cartClient pb.CartServiceClient, + goodClient pb.GoodServiceClient, + publisher event.CreateOrderPublisher, +) *OrderService { + return &OrderService{ + cartClient: cartClient, + goodClient: goodClient, + publisher: publisher, + } +} + +func (s *OrderService) CreateOrder(ctx context.Context, request *pb.CreateOrderRequest) (*pb.CreateOrderResponse, error) { + + log := logger.WithContext(ctx, "CartService.CreateOrder") + + log.Infof("创建订单: %v", request.CartIds) + + // 1. 获取购物车详情. + cartDetail, err := s.cartClient.GetCardDetailByIDs(ctx, &pb.GetCardDetailByIDsRequest{ + Ids: request.CartIds, + }) + + if err != nil { + log.WithError(err).Errorf("获取购物车详情失败:%v", request.CartIds) + return nil, err + } + + // 2. 获取商品详情 + var ( + goodIds []int64 + cartIds []int64 + updateStokeRequest []*pb.StokeRequest + ) + for _, c := range cartDetail.Carts { + goodIds = append(goodIds, c.GoodId) + cartIds = append(cartIds, c.Id) + updateStokeRequest = append(updateStokeRequest, &pb.StokeRequest{ + GoodId: c.GoodId, + Stoke: 1, // 忘了写库存字段了 + }) + } + + // 2. 获取商品详情 + goodsResp, err := s.goodClient.GetGoodsByID(ctx, &pb.GetGoodsByIDsRequest{ + Ids: goodIds, + }) + if err != nil { + log.WithError(err).Errorf("获取商品详情失败:%v", goodIds) + return nil, err + } + + // 创建订单. + realOrder := s.createRealOrder(ctx, goodsResp.Goods) + + log.Infof("创建订单: %v", realOrder) + + // 更新库存 + _, err = s.goodClient.UpdateGoodsStoke(ctx, &pb.UpdateGoodsStokeRequest{ + Requests: updateStokeRequest, + }) + if err != nil { + log.WithError(err).Errorf("更新商品库存失败:%v", goodIds) + return nil, err + } + + // 更新购物车 + _, err = s.cartClient.UpdateCartByIDs(ctx, &pb.UpdateCartByIDsRequest{ + Ids: cartIds, + }) + + if err != nil { + log.WithError(err).Errorf("更新商品库存失败:%v", goodIds) + return nil, err + } + + // 发送下单事件 + s.publisher.Notify(realOrder) + + // 返回 + return &pb.CreateOrderResponse{ + OrderId: realOrder.Id, + }, nil +} + +func (s *OrderService) createRealOrder(ctx context.Context, goods []*pb.Good) *pb.Order { + + logger.WithContext(ctx, "OrderService.createRealOrder").Infof("商品是: %+v", goods) + + // 创建订单 + + return &pb.Order{ + Id: 1, + Uid: 1, + Price: 310, + Status: pb.Order_Created, + CreatedAt: time.Now().Unix(), + } +}