This is an automated email from the ASF dual-hosted git repository. luky116 pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-seata-go-samples.git
The following commit(s) were added to refs/heads/main by this push: new 598e6db feat:add at grpc sample (#58) 598e6db is described below commit 598e6dbaab25edc6cfe33c36bdac13af8428d761 Author: 1kasa <134709672+1k...@users.noreply.github.com> AuthorDate: Sun Mar 2 09:58:15 2025 +0800 feat:add at grpc sample (#58) * feat: at grpc sample * feat: at grpc sample --- at/grpc/cmd/client/main.go | 64 +++++++++++++++++ at/grpc/cmd/server/main.go | 50 +++++++++++++ at/grpc/pb/at_grpc.pb.go | 159 ++++++++++++++++++++++++++++++++++++++++++ at/grpc/pb/at_grpc.proto | 30 ++++++++ at/grpc/pb/at_grpc_grpc.pb.go | 106 ++++++++++++++++++++++++++++ at/grpc/service/service.go | 62 ++++++++++++++++ 6 files changed, 471 insertions(+) diff --git a/at/grpc/cmd/client/main.go b/at/grpc/cmd/client/main.go new file mode 100644 index 0000000..2e27269 --- /dev/null +++ b/at/grpc/cmd/client/main.go @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package main implements a client for Greeter service. +package main + +import ( + "context" + "flag" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + __ "seata.apache.org/seata-go-samples/at/grpc/pb" + "seata.apache.org/seata-go/pkg/client" + + grpc2 "seata.apache.org/seata-go/pkg/integration/grpc" + "seata.apache.org/seata-go/pkg/tm" + "seata.apache.org/seata-go/pkg/util/log" +) + +func main() { + flag.Parse() + // to set up grpc env + // set up a connection to the server. + conn, err := grpc.Dial("localhost:50051", + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithUnaryInterceptor(grpc2.ClientTransactionInterceptor)) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + businessClient := __.NewATServiceBusinessClient(conn) + + client.InitPath("../../../../conf/seatago.yml") + tm.WithGlobalTx( + context.Background(), + &tm.GtxConfig{ + Name: "XASampleLocalGlobalTx", + }, + func(ctx context.Context) (re error) { + r1, re := businessClient.UpdateDataSuccess(ctx, &__.Params{A: "1", B: "2"}) + if re != nil { + log.Fatalf("could not do TestXAServiceBusiness: %v", re) + return + } + log.Infof("TestXAServiceBusiness res: %s", r1) + + return + }) + <-make(chan struct{}) +} diff --git a/at/grpc/cmd/server/main.go b/at/grpc/cmd/server/main.go new file mode 100644 index 0000000..d67b597 --- /dev/null +++ b/at/grpc/cmd/server/main.go @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package main implements a business for Greeter service. +package main + +import ( + "fmt" + "net" + "seata.apache.org/seata-go-samples/at/grpc/pb" + "seata.apache.org/seata-go/pkg/client" + + "google.golang.org/grpc" + + "seata.apache.org/seata-go-samples/at/grpc/service" + grpc2 "seata.apache.org/seata-go/pkg/integration/grpc" + "seata.apache.org/seata-go/pkg/util/log" +) + +func main() { + client.InitPath("../../../../conf/seatago.yml") + service.InitService() + + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 50051)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + log.Infof("server register") + s := grpc.NewServer(grpc.UnaryInterceptor(grpc2.ServerTransactionInterceptor)) + + __.RegisterATServiceBusinessServer(s, &service.GrpcBusinessService{}) + log.Infof("business listening at %v", lis.Addr()) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/at/grpc/pb/at_grpc.pb.go b/at/grpc/pb/at_grpc.pb.go new file mode 100644 index 0000000..c063010 --- /dev/null +++ b/at/grpc/pb/at_grpc.pb.go @@ -0,0 +1,159 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.1 +// protoc v5.29.2 +// source: at_grpc.proto + +package __ + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" + 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 Params struct { + state protoimpl.MessageState `protogen:"open.v1"` + A string `protobuf:"bytes,1,opt,name=a,proto3" json:"a,omitempty"` + B string `protobuf:"bytes,2,opt,name=b,proto3" json:"b,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Params) Reset() { + *x = Params{} + mi := &file_at_grpc_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Params) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Params) ProtoMessage() {} + +func (x *Params) ProtoReflect() protoreflect.Message { + mi := &file_at_grpc_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Params.ProtoReflect.Descriptor instead. +func (*Params) Descriptor() ([]byte, []int) { + return file_at_grpc_proto_rawDescGZIP(), []int{0} +} + +func (x *Params) GetA() string { + if x != nil { + return x.A + } + return "" +} + +func (x *Params) GetB() string { + if x != nil { + return x.B + } + return "" +} + +var File_at_grpc_proto protoreflect.FileDescriptor + +var file_at_grpc_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x61, 0x74, 0x5f, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, + 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2f, 0x77, 0x72, 0x61, 0x70, 0x70, 0x65, 0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, + 0x24, 0x0a, 0x06, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x0c, 0x0a, 0x01, 0x61, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, 0x61, 0x12, 0x0c, 0x0a, 0x01, 0x62, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x01, 0x62, 0x32, 0x4f, 0x0a, 0x11, 0x41, 0x54, 0x53, 0x65, 0x72, 0x76, 0x69, + 0x63, 0x65, 0x42, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x12, 0x3a, 0x0a, 0x11, 0x55, 0x70, + 0x64, 0x61, 0x74, 0x65, 0x44, 0x61, 0x74, 0x61, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, + 0x07, 0x2e, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x1a, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x42, 0x6f, 0x6f, 0x6c, 0x56, + 0x61, 0x6c, 0x75, 0x65, 0x22, 0x00, 0x42, 0x04, 0x5a, 0x02, 0x2e, 0x2f, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_at_grpc_proto_rawDescOnce sync.Once + file_at_grpc_proto_rawDescData = file_at_grpc_proto_rawDesc +) + +func file_at_grpc_proto_rawDescGZIP() []byte { + file_at_grpc_proto_rawDescOnce.Do(func() { + file_at_grpc_proto_rawDescData = protoimpl.X.CompressGZIP(file_at_grpc_proto_rawDescData) + }) + return file_at_grpc_proto_rawDescData +} + +var file_at_grpc_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_at_grpc_proto_goTypes = []any{ + (*Params)(nil), // 0: Params + (*wrapperspb.BoolValue)(nil), // 1: google.protobuf.BoolValue +} +var file_at_grpc_proto_depIdxs = []int32{ + 0, // 0: ATServiceBusiness.UpdateDataSuccess:input_type -> Params + 1, // 1: ATServiceBusiness.UpdateDataSuccess:output_type -> google.protobuf.BoolValue + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_at_grpc_proto_init() } +func file_at_grpc_proto_init() { + if File_at_grpc_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_at_grpc_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_at_grpc_proto_goTypes, + DependencyIndexes: file_at_grpc_proto_depIdxs, + MessageInfos: file_at_grpc_proto_msgTypes, + }.Build() + File_at_grpc_proto = out.File + file_at_grpc_proto_rawDesc = nil + file_at_grpc_proto_goTypes = nil + file_at_grpc_proto_depIdxs = nil +} diff --git a/at/grpc/pb/at_grpc.proto b/at/grpc/pb/at_grpc.proto new file mode 100644 index 0000000..28cf6ed --- /dev/null +++ b/at/grpc/pb/at_grpc.proto @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +syntax = "proto3"; +option go_package="./"; +import "google/protobuf/wrappers.proto"; + +message Params { + string a = 1; + string b = 2; +} + +service ATServiceBusiness { + rpc UpdateDataSuccess (Params) returns (google.protobuf.BoolValue){ + } +} diff --git a/at/grpc/pb/at_grpc_grpc.pb.go b/at/grpc/pb/at_grpc_grpc.pb.go new file mode 100644 index 0000000..35c6173 --- /dev/null +++ b/at/grpc/pb/at_grpc_grpc.pb.go @@ -0,0 +1,106 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v5.29.2 +// source: at_grpc.proto + +package __ + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" +) + +// 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 + +// ATServiceBusinessClient is the client API for ATServiceBusiness 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 ATServiceBusinessClient interface { + UpdateDataSuccess(ctx context.Context, in *Params, opts ...grpc.CallOption) (*wrapperspb.BoolValue, error) +} + +type aTServiceBusinessClient struct { + cc grpc.ClientConnInterface +} + +func NewATServiceBusinessClient(cc grpc.ClientConnInterface) ATServiceBusinessClient { + return &aTServiceBusinessClient{cc} +} + +func (c *aTServiceBusinessClient) UpdateDataSuccess(ctx context.Context, in *Params, opts ...grpc.CallOption) (*wrapperspb.BoolValue, error) { + out := new(wrapperspb.BoolValue) + err := c.cc.Invoke(ctx, "/ATServiceBusiness/UpdateDataSuccess", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ATServiceBusinessServer is the server API for ATServiceBusiness service. +// All implementations must embed UnimplementedATServiceBusinessServer +// for forward compatibility +type ATServiceBusinessServer interface { + UpdateDataSuccess(context.Context, *Params) (*wrapperspb.BoolValue, error) + mustEmbedUnimplementedATServiceBusinessServer() +} + +// UnimplementedATServiceBusinessServer must be embedded to have forward compatible implementations. +type UnimplementedATServiceBusinessServer struct { +} + +func (UnimplementedATServiceBusinessServer) UpdateDataSuccess(context.Context, *Params) (*wrapperspb.BoolValue, error) { + return nil, status.Errorf(codes.Unimplemented, "method UpdateDataSuccess not implemented") +} +func (UnimplementedATServiceBusinessServer) mustEmbedUnimplementedATServiceBusinessServer() {} + +// UnsafeATServiceBusinessServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ATServiceBusinessServer will +// result in compilation errors. +type UnsafeATServiceBusinessServer interface { + mustEmbedUnimplementedATServiceBusinessServer() +} + +func RegisterATServiceBusinessServer(s grpc.ServiceRegistrar, srv ATServiceBusinessServer) { + s.RegisterService(&ATServiceBusiness_ServiceDesc, srv) +} + +func _ATServiceBusiness_UpdateDataSuccess_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Params) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ATServiceBusinessServer).UpdateDataSuccess(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/ATServiceBusiness/UpdateDataSuccess", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ATServiceBusinessServer).UpdateDataSuccess(ctx, req.(*Params)) + } + return interceptor(ctx, in, info, handler) +} + +// ATServiceBusiness_ServiceDesc is the grpc.ServiceDesc for ATServiceBusiness service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var ATServiceBusiness_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "ATServiceBusiness", + HandlerType: (*ATServiceBusinessServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "UpdateDataSuccess", + Handler: _ATServiceBusiness_UpdateDataSuccess_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "at_grpc.proto", +} diff --git a/at/grpc/service/service.go b/at/grpc/service/service.go new file mode 100644 index 0000000..39077bf --- /dev/null +++ b/at/grpc/service/service.go @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package service + +import ( + "context" + "database/sql" + "fmt" + "google.golang.org/protobuf/types/known/wrapperspb" + "seata.apache.org/seata-go-samples/at/grpc/pb" + "time" + + sql2 "seata.apache.org/seata-go/pkg/datasource/sql" +) + +var ( + db *sql.DB +) + +func InitService() { + var err error + db, err = sql.Open(sql2.SeataATMySQLDriver, "root:12345678@tcp(127.0.0.1:3306)/seata_client?multiStatements=true&interpolateParams=true") + if err != nil { + panic("init service error") + } +} + +type GrpcBusinessService struct { + __.UnimplementedATServiceBusinessServer +} + +func (service GrpcBusinessService) UpdateDataSuccess(ctx context.Context, params *__.Params) (*wrapperspb.BoolValue, error) { + sql := "update order_tbl set descs=? where id=?" + ret, err := db.ExecContext(ctx, sql, fmt.Sprintf("NewDescs1-%d", time.Now().UnixMilli()), 1) + if err != nil { + fmt.Printf("update failed, err:%v\n", err) + return wrapperspb.Bool(false), err + } + + rows, err := ret.RowsAffected() + if err != nil { + fmt.Printf("update failed, err:%v\n", err) + return wrapperspb.Bool(false), err + } + fmt.Printf("update success: %d.\n", rows) + return wrapperspb.Bool(true), nil +} --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org