添加protoc.sh

This commit is contained in:
eson 2020-03-05 05:12:38 +08:00
parent f27ac05f9a
commit 9a6a99bfc7
8 changed files with 46 additions and 220 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ edb
main
*.log
log
goproto

10
api.go
View File

@ -2,12 +2,18 @@ package main
import (
context "context"
proto "edb/proto"
)
// server is used to implement
type server struct{}
// SayHello implements
func (s *server) CreateTable(ctx context.Context, in *TableRequest) (*TableReply, error) {
return &TableReply{Msg: "Hello " + in.Name}, nil
func (s *server) SayHello(ctx context.Context, in *proto.TableRequest) (*proto.TableReply, error) {
return &proto.TableReply{Msg: "Hello " + in.Name}, nil
}
// SayHello implements
func (s *server) CreateTable(ctx context.Context, in *proto.TableRequest) (*proto.TableReply, error) {
return &proto.TableReply{Msg: "Hello " + in.Name}, nil
}

209
api.pb.go
View File

@ -1,209 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: api.proto
package main
import (
context "context"
fmt "fmt"
proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// 包含用户名的请求信息
type TableRequest struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *TableRequest) Reset() { *m = TableRequest{} }
func (m *TableRequest) String() string { return proto.CompactTextString(m) }
func (*TableRequest) ProtoMessage() {}
func (*TableRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_00212fb1f9d3bf1c, []int{0}
}
func (m *TableRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_TableRequest.Unmarshal(m, b)
}
func (m *TableRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_TableRequest.Marshal(b, m, deterministic)
}
func (m *TableRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_TableRequest.Merge(m, src)
}
func (m *TableRequest) XXX_Size() int {
return xxx_messageInfo_TableRequest.Size(m)
}
func (m *TableRequest) XXX_DiscardUnknown() {
xxx_messageInfo_TableRequest.DiscardUnknown(m)
}
var xxx_messageInfo_TableRequest proto.InternalMessageInfo
func (m *TableRequest) GetName() string {
if m != nil {
return m.Name
}
return ""
}
// 服务端响应信息
type TableReply struct {
Msg string `protobuf:"bytes,1,opt,name=msg,proto3" json:"msg,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *TableReply) Reset() { *m = TableReply{} }
func (m *TableReply) String() string { return proto.CompactTextString(m) }
func (*TableReply) ProtoMessage() {}
func (*TableReply) Descriptor() ([]byte, []int) {
return fileDescriptor_00212fb1f9d3bf1c, []int{1}
}
func (m *TableReply) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_TableReply.Unmarshal(m, b)
}
func (m *TableReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_TableReply.Marshal(b, m, deterministic)
}
func (m *TableReply) XXX_Merge(src proto.Message) {
xxx_messageInfo_TableReply.Merge(m, src)
}
func (m *TableReply) XXX_Size() int {
return xxx_messageInfo_TableReply.Size(m)
}
func (m *TableReply) XXX_DiscardUnknown() {
xxx_messageInfo_TableReply.DiscardUnknown(m)
}
var xxx_messageInfo_TableReply proto.InternalMessageInfo
func (m *TableReply) GetMsg() string {
if m != nil {
return m.Msg
}
return ""
}
func init() {
proto.RegisterType((*TableRequest)(nil), "main.TableRequest")
proto.RegisterType((*TableReply)(nil), "main.TableReply")
}
func init() {
proto.RegisterFile("api.proto", fileDescriptor_00212fb1f9d3bf1c)
}
var fileDescriptor_00212fb1f9d3bf1c = []byte{
// 140 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4c, 0x2c, 0xc8, 0xd4,
0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x52, 0xe2, 0xe2, 0x09,
0x49, 0x4c, 0xca, 0x49, 0x0d, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x11, 0x12, 0xe2, 0x62, 0xc9,
0x4b, 0xcc, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x95, 0xe4, 0xb8, 0xb8,
0xa0, 0x6a, 0x0a, 0x72, 0x2a, 0x85, 0x04, 0xb8, 0x98, 0x73, 0x8b, 0xd3, 0xa1, 0x0a, 0x40, 0x4c,
0x23, 0x47, 0x2e, 0x0e, 0xd7, 0xc4, 0xe2, 0x4a, 0x97, 0xc4, 0x92, 0x44, 0x21, 0x53, 0x2e, 0x6e,
0xe7, 0xa2, 0xd4, 0xc4, 0x92, 0x54, 0xb0, 0x0e, 0x21, 0x21, 0x3d, 0x90, 0x2d, 0x7a, 0xc8, 0x56,
0x48, 0x09, 0xa0, 0x88, 0x15, 0xe4, 0x54, 0x2a, 0x31, 0x24, 0xb1, 0x81, 0xdd, 0x64, 0x0c, 0x08,
0x00, 0x00, 0xff, 0xff, 0x2d, 0x8a, 0xa9, 0x47, 0xa0, 0x00, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConnInterface
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion6
// EasyDataClient is the client API for EasyData service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type EasyDataClient interface {
// 服务端返馈信息方法
CreateTable(ctx context.Context, in *TableRequest, opts ...grpc.CallOption) (*TableReply, error)
}
type easyDataClient struct {
cc grpc.ClientConnInterface
}
func NewEasyDataClient(cc grpc.ClientConnInterface) EasyDataClient {
return &easyDataClient{cc}
}
func (c *easyDataClient) CreateTable(ctx context.Context, in *TableRequest, opts ...grpc.CallOption) (*TableReply, error) {
out := new(TableReply)
err := c.cc.Invoke(ctx, "/main.EasyData/CreateTable", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// EasyDataServer is the server API for EasyData service.
type EasyDataServer interface {
// 服务端返馈信息方法
CreateTable(context.Context, *TableRequest) (*TableReply, error)
}
// UnimplementedEasyDataServer can be embedded to have forward compatible implementations.
type UnimplementedEasyDataServer struct {
}
func (*UnimplementedEasyDataServer) CreateTable(ctx context.Context, req *TableRequest) (*TableReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method CreateTable not implemented")
}
func RegisterEasyDataServer(s *grpc.Server, srv EasyDataServer) {
s.RegisterService(&_EasyData_serviceDesc, srv)
}
func _EasyData_CreateTable_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(TableRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(EasyDataServer).CreateTable(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/main.EasyData/CreateTable",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(EasyDataServer).CreateTable(ctx, req.(*TableRequest))
}
return interceptor(ctx, in, info, handler)
}
var _EasyData_serviceDesc = grpc.ServiceDesc{
ServiceName: "main.EasyData",
HandlerType: (*EasyDataServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "CreateTable",
Handler: _EasyData_CreateTable_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api.proto",
}

View File

@ -2,6 +2,7 @@ package main
import (
"context"
proto "edb/goproto"
"log"
"net"
"runtime"
@ -24,7 +25,7 @@ func createServer() {
log.Fatalf("failed to listen: %v", err)
}
gserver := grpc.NewServer()
RegisterEasyDataServer(gserver, &server{})
proto.RegisterEasyDataServer(gserver, &server{})
go gserver.Serve(listen)
time.Sleep(time.Millisecond * 50)
}
@ -37,10 +38,10 @@ func ClientCall(wg *sync.WaitGroup, COUNT int) {
panic(err)
}
defer conn.Close()
client := NewEasyDataClient(conn)
client := proto.NewEasyDataClient(conn)
for i := 0; i < COUNT; i++ {
reply, err := client.CreateTable(context.Background(), &TableRequest{Name: "user"})
reply, err := client.CreateTable(context.Background(), &proto.TableRequest{Name: "user"})
if err == nil {
reply.GetMsg()
}
@ -53,8 +54,8 @@ func TestSayHello(t *testing.T) {
wg := &sync.WaitGroup{}
GCOUNT := 1000
COUNT := 100
GCOUNT := 400000
COUNT := 10
wg.Add(GCOUNT)
@ -74,10 +75,10 @@ func BenchmarkCreateTable(b *testing.B) {
panic(err)
}
defer conn.Close()
client := NewEasyDataClient(conn)
client := proto.NewEasyDataClient(conn)
for i := 0; i < b.N; i++ {
reply, err := client.CreateTable(context.Background(), &TableRequest{Name: "user"})
reply, err := client.CreateTable(context.Background(), &proto.TableRequest{Name: "user"})
if err != nil {
panic(err)
}

View File

@ -5,6 +5,8 @@ import (
"net"
"runtime"
proto "edb/goproto"
grpc "google.golang.org/grpc"
)
@ -19,12 +21,14 @@ func CreateServer() {
log.Fatalf("failed to listen: %v", err)
}
gserver := grpc.NewServer()
RegisterEasyDataServer(gserver, &server{})
proto.RegisterEasyDataServer(gserver, &server{})
gserver.Serve(listen)
// time.Sleep(time.Millisecond * 50)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
// test.
CreateServer()
}

View File

@ -1,6 +1,6 @@
syntax = "proto3";
package main;
package goproto;
//
service EasyData {
//

7
proto/protoc.sh Executable file
View File

@ -0,0 +1,7 @@
# !/bin/bash
BASEDIR=$(dirname "$0")
if [ ! -d "$BASEDIR/../goproto" ]; then
mkdir $BASEDIR/../goproto
fi
cd $BASEDIR
protoc --go_out=plugins=grpc:../goproto/ *.proto

16
proto/test.proto Normal file
View File

@ -0,0 +1,16 @@
syntax = "proto3";
package goproto;
//
service Test {
//
rpc SayHello(Request) returns (Reply) {}
}
//
message Request {
string what = 1;
}
//
message Reply {
string msg = 1;
}