提交 2f03e8d7 编写于 作者: J Jason

删除RPC方式,改TCP消息

上级 4ae40728
:: 1 下载protoc https://github.com/google/protobuf/releases
:: 2 获取编译插件go get -u github.com/golang/protobuf/protoc-gen-go
:: 3 编译protoc --go_out=. *.proto
protoc --go_out=plugins=grpc:. *.proto
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.12.3
// source: plugin.proto
package plugin
import (
context "context"
proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
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)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
type RegisterReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Package string `protobuf:"bytes,1,opt,name=package,proto3" json:"package,omitempty"`
}
func (x *RegisterReq) Reset() {
*x = RegisterReq{}
if protoimpl.UnsafeEnabled {
mi := &file_plugin_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *RegisterReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*RegisterReq) ProtoMessage() {}
func (x *RegisterReq) ProtoReflect() protoreflect.Message {
mi := &file_plugin_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 RegisterReq.ProtoReflect.Descriptor instead.
func (*RegisterReq) Descriptor() ([]byte, []int) {
return file_plugin_proto_rawDescGZIP(), []int{0}
}
func (x *RegisterReq) GetPackage() string {
if x != nil {
return x.Package
}
return ""
}
type RegisterResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Ok bool `protobuf:"varint,1,opt,name=ok,proto3" json:"ok,omitempty"`
}
func (x *RegisterResp) Reset() {
*x = RegisterResp{}
if protoimpl.UnsafeEnabled {
mi := &file_plugin_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *RegisterResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*RegisterResp) ProtoMessage() {}
func (x *RegisterResp) ProtoReflect() protoreflect.Message {
mi := &file_plugin_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 RegisterResp.ProtoReflect.Descriptor instead.
func (*RegisterResp) Descriptor() ([]byte, []int) {
return file_plugin_proto_rawDescGZIP(), []int{1}
}
func (x *RegisterResp) GetOk() bool {
if x != nil {
return x.Ok
}
return false
}
type RequestPack struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Uri string `protobuf:"bytes,2,opt,name=uri,proto3" json:"uri,omitempty"`
Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
}
func (x *RequestPack) Reset() {
*x = RequestPack{}
if protoimpl.UnsafeEnabled {
mi := &file_plugin_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *RequestPack) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*RequestPack) ProtoMessage() {}
func (x *RequestPack) ProtoReflect() protoreflect.Message {
mi := &file_plugin_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 RequestPack.ProtoReflect.Descriptor instead.
func (*RequestPack) Descriptor() ([]byte, []int) {
return file_plugin_proto_rawDescGZIP(), []int{2}
}
func (x *RequestPack) GetId() int64 {
if x != nil {
return x.Id
}
return 0
}
func (x *RequestPack) GetUri() string {
if x != nil {
return x.Uri
}
return ""
}
func (x *RequestPack) GetBody() string {
if x != nil {
return x.Body
}
return ""
}
type ResponsePack struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Ok bool `protobuf:"varint,2,opt,name=ok,proto3" json:"ok,omitempty"`
Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"`
}
func (x *ResponsePack) Reset() {
*x = ResponsePack{}
if protoimpl.UnsafeEnabled {
mi := &file_plugin_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *ResponsePack) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ResponsePack) ProtoMessage() {}
func (x *ResponsePack) ProtoReflect() protoreflect.Message {
mi := &file_plugin_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 ResponsePack.ProtoReflect.Descriptor instead.
func (*ResponsePack) Descriptor() ([]byte, []int) {
return file_plugin_proto_rawDescGZIP(), []int{3}
}
func (x *ResponsePack) GetId() int64 {
if x != nil {
return x.Id
}
return 0
}
func (x *ResponsePack) GetOk() bool {
if x != nil {
return x.Ok
}
return false
}
func (x *ResponsePack) GetMessage() string {
if x != nil {
return x.Message
}
return ""
}
var File_plugin_proto protoreflect.FileDescriptor
var file_plugin_proto_rawDesc = []byte{
0x0a, 0x0c, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06,
0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x22, 0x27, 0x0a, 0x0b, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74,
0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x22,
0x1e, 0x0a, 0x0c, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x12,
0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22,
0x43, 0x0a, 0x0b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x50, 0x61, 0x63, 0x6b, 0x12, 0x0e,
0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x10,
0x0a, 0x03, 0x75, 0x72, 0x69, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x69,
0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
0x62, 0x6f, 0x64, 0x79, 0x22, 0x48, 0x0a, 0x0c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x50, 0x61, 0x63, 0x6b, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03,
0x52, 0x02, 0x69, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08,
0x52, 0x02, 0x6f, 0x6b, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18,
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0x7b,
0x0a, 0x06, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x12, 0x37, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69,
0x73, 0x74, 0x65, 0x72, 0x12, 0x13, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x52, 0x65,
0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x14, 0x2e, 0x70, 0x6c, 0x75, 0x67,
0x69, 0x6e, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x22,
0x00, 0x12, 0x38, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x72, 0x74, 0x12, 0x14, 0x2e, 0x70, 0x6c, 0x75,
0x67, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x50, 0x61, 0x63, 0x6b,
0x1a, 0x13, 0x2e, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x50, 0x61, 0x63, 0x6b, 0x22, 0x00, 0x28, 0x01, 0x30, 0x01, 0x42, 0x0a, 0x5a, 0x08, 0x2e,
0x3b, 0x70, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_plugin_proto_rawDescOnce sync.Once
file_plugin_proto_rawDescData = file_plugin_proto_rawDesc
)
func file_plugin_proto_rawDescGZIP() []byte {
file_plugin_proto_rawDescOnce.Do(func() {
file_plugin_proto_rawDescData = protoimpl.X.CompressGZIP(file_plugin_proto_rawDescData)
})
return file_plugin_proto_rawDescData
}
var file_plugin_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_plugin_proto_goTypes = []interface{}{
(*RegisterReq)(nil), // 0: plugin.RegisterReq
(*RegisterResp)(nil), // 1: plugin.RegisterResp
(*RequestPack)(nil), // 2: plugin.RequestPack
(*ResponsePack)(nil), // 3: plugin.ResponsePack
}
var file_plugin_proto_depIdxs = []int32{
0, // 0: plugin.Plugin.Register:input_type -> plugin.RegisterReq
3, // 1: plugin.Plugin.Start:input_type -> plugin.ResponsePack
1, // 2: plugin.Plugin.Register:output_type -> plugin.RegisterResp
2, // 3: plugin.Plugin.Start:output_type -> plugin.RequestPack
2, // [2:4] is the sub-list for method output_type
0, // [0:2] 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_plugin_proto_init() }
func file_plugin_proto_init() {
if File_plugin_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_plugin_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RegisterReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_plugin_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RegisterResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_plugin_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*RequestPack); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_plugin_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*ResponsePack); 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_plugin_proto_rawDesc,
NumEnums: 0,
NumMessages: 4,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_plugin_proto_goTypes,
DependencyIndexes: file_plugin_proto_depIdxs,
MessageInfos: file_plugin_proto_msgTypes,
}.Build()
File_plugin_proto = out.File
file_plugin_proto_rawDesc = nil
file_plugin_proto_goTypes = nil
file_plugin_proto_depIdxs = nil
}
// 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
// PluginClient is the client API for Plugin service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type PluginClient interface {
Register(ctx context.Context, in *RegisterReq, opts ...grpc.CallOption) (*RegisterResp, error)
Start(ctx context.Context, opts ...grpc.CallOption) (Plugin_StartClient, error)
}
type pluginClient struct {
cc grpc.ClientConnInterface
}
func NewPluginClient(cc grpc.ClientConnInterface) PluginClient {
return &pluginClient{cc}
}
func (c *pluginClient) Register(ctx context.Context, in *RegisterReq, opts ...grpc.CallOption) (*RegisterResp, error) {
out := new(RegisterResp)
err := c.cc.Invoke(ctx, "/plugin.Plugin/Register", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *pluginClient) Start(ctx context.Context, opts ...grpc.CallOption) (Plugin_StartClient, error) {
stream, err := c.cc.NewStream(ctx, &_Plugin_serviceDesc.Streams[0], "/plugin.Plugin/Start", opts...)
if err != nil {
return nil, err
}
x := &pluginStartClient{stream}
return x, nil
}
type Plugin_StartClient interface {
Send(*ResponsePack) error
Recv() (*RequestPack, error)
grpc.ClientStream
}
type pluginStartClient struct {
grpc.ClientStream
}
func (x *pluginStartClient) Send(m *ResponsePack) error {
return x.ClientStream.SendMsg(m)
}
func (x *pluginStartClient) Recv() (*RequestPack, error) {
m := new(RequestPack)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// PluginServer is the server API for Plugin service.
type PluginServer interface {
Register(context.Context, *RegisterReq) (*RegisterResp, error)
Start(Plugin_StartServer) error
}
// UnimplementedPluginServer can be embedded to have forward compatible implementations.
type UnimplementedPluginServer struct {
}
func (*UnimplementedPluginServer) Register(context.Context, *RegisterReq) (*RegisterResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method Register not implemented")
}
func (*UnimplementedPluginServer) Start(Plugin_StartServer) error {
return status.Errorf(codes.Unimplemented, "method Start not implemented")
}
func RegisterPluginServer(s *grpc.Server, srv PluginServer) {
s.RegisterService(&_Plugin_serviceDesc, srv)
}
func _Plugin_Register_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(RegisterReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(PluginServer).Register(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/plugin.Plugin/Register",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PluginServer).Register(ctx, req.(*RegisterReq))
}
return interceptor(ctx, in, info, handler)
}
func _Plugin_Start_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(PluginServer).Start(&pluginStartServer{stream})
}
type Plugin_StartServer interface {
Send(*RequestPack) error
Recv() (*ResponsePack, error)
grpc.ServerStream
}
type pluginStartServer struct {
grpc.ServerStream
}
func (x *pluginStartServer) Send(m *RequestPack) error {
return x.ServerStream.SendMsg(m)
}
func (x *pluginStartServer) Recv() (*ResponsePack, error) {
m := new(ResponsePack)
if err := x.ServerStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
var _Plugin_serviceDesc = grpc.ServiceDesc{
ServiceName: "plugin.Plugin",
HandlerType: (*PluginServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Register",
Handler: _Plugin_Register_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "Start",
Handler: _Plugin_Start_Handler,
ServerStreams: true,
ClientStreams: true,
},
},
Metadata: "plugin.proto",
}
syntax = "proto3";
option go_package = ".;plugin";
package plugin;
service Plugin {
rpc Register(RegisterReq) returns (RegisterResp) {}
rpc Start(stream ResponsePack) returns (stream RequestPack) {};
}
message RegisterReq{
string package = 1;
}
message RegisterResp{
bool ok = 1;
}
message RequestPack {
int64 id = 1;
string uri = 2;
string body = 3;
}
message ResponsePack {
int64 id = 1;
bool ok = 2;
string message = 3;
}
package rpc
import (
"context"
"github.com/zgwit/dtu-admin/plugin"
"log"
)
type pluginServer struct {
}
func (r *pluginServer) Register(ctx context.Context, req *plugin.RegisterReq) (*plugin.RegisterResp, error) {
return nil, nil
}
func (r *pluginServer) Start(stream plugin.Plugin_StartServer) error{
//stream.Send()
//stream.Recv()
for {
resp, err := stream.Recv()
if err != nil {
log.Println(err)
break
}
//TODO 处理
log.Println(resp)
}
return nil
}
\ No newline at end of file
package rpc
import (
"github.com/zgwit/dtu-admin/plugin"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
)
func ListenAndServe() {
lis, err := net.Listen("tcp", ":50055")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
plugin.RegisterPluginServer(s, &pluginServer{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func Request() {
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册