golang grpc 封装类 客户端代码 服务端代码

最新小编在学习golang中的grpc在学习中也遇到了许多问题,我写了一个属于自己的grpc封装类分享给大家

helper 封装文件

package grpc_helper

import (
	"context"
	"google.golang.org/grpc"
	"net"
	"time"
)
var GrpcConn *GrpcHelper
type ServiceFn func(server *grpc.Server)
type ClientFn func(conn *grpc.ClientConn, ctx context.Context) (err error)
type GrpcHelperInterface interface {
	ServiceRegister( fn ServiceFn) (err error)
	ClientRegister(fn ClientFn) (err error)
}
type GrpcHelper struct {
	Conn *grpc.ClientConn
	Port string
	Host string
	//context.WithTimeout(context.Background(), time.Second/10)
	ContextFn func()(context.Context,context.CancelFunc)
	WithOutTime time.Duration
}

func (h *GrpcHelper) ClientRegister(fn ClientFn) error {
	if h.Conn == nil {
		conn, err := grpc.Dial(h.Host+":"+h.Port, grpc.WithInsecure(), grpc.WithBlock())
		if err != nil {
			return err
		}
		h.Conn=conn
	}
	defer h.Conn.Close()
	var ctx context.Context
	var cancel context.CancelFunc
	if h.ContextFn!=nil {
		ctx, cancel = h.ContextFn()
	}else{
		if h.WithOutTime==0 {
			ctx, cancel = context.WithTimeout(context.Background(), time.Second)
		}else{
			ctx, cancel = context.WithTimeout(context.Background(), h.WithOutTime)
		}
	}
	defer cancel()
	if err := fn(h.Conn, ctx); err != nil {
		return err
	}
	return nil
}
func (h *GrpcHelper) ServiceRegister( fn ServiceFn) (err error) {
	listener, err2 := net.Listen("tcp", h.Host+":"+h.Port)
	if err2 != nil {
		return err2
	}
	server := grpc.NewServer()
	fn(server)
	if err := server.Serve(listener); err != nil {
		return err
	}
	return
}
type GrpcOption struct {
	Port string //端口 "8080"
	Host string //地址 “ 127.0.0.1 ”
	ContextFn func()(context.Context,context.CancelFunc)
	WithOutTime time.Duration
}
func NewGrpcHelper(option GrpcOption) GrpcHelperInterface {
	 var g GrpcHelperInterface
	if option.Host=="" {
		option.Host="127.0.0.1"
	}
	if option.Port=="" {
		option.Port="8089"
	}
	 g = &GrpcHelper{
		Port: option.Port,
		Host: option.Host,
		ContextFn: option.ContextFn,
		WithOutTime: option.WithOutTime,
	}
	return g
}

helper_test.go

package grpc_helper

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"testing"
	"time"
)

var grpcHelper GrpcHelperInterface

func test() GrpcOption  {
	return GrpcOption{
		Port: "55088",
		Host: "127.0.0.1",
		//延迟超时设置 方法重写 有优先于WithOutTime
		ContextFn: func() (context.Context, context.CancelFunc) {
			return context.WithTimeout(context.Background(),time.Second)
		},
		WithOutTime: time.Second*5,
	}
}
func service()  GrpcHelperInterface {
	if grpcHelper!=nil {
		return grpcHelper
	}else {
		grpcHelper = NewGrpcHelper(test())
		return grpcHelper
	}
}
func TestServiceHelper(t *testing.T) {
	service()
	if grpcHelper!=nil {
		err := grpcHelper.ServiceRegister(func(server *grpc.Server) {
			RegisterRoleServer(server, new(Role))
		})
		fmt.Println(err)
	}
}
func TestClientHelper(t *testing.T) {
	service()
	if  grpcHelper !=nil{
		var Re *RoleListResponse
		err := grpcHelper.ClientRegister(func(conn *grpc.ClientConn, ctx context.Context) (err error) {
			client := NewRoleClient(conn)
			req := RoleListRequest{
				Id:   1,
				Name: "ZhangSan32132",
				Age:  25,
			}
			Re, err = client.Rolelist(ctx, &req)
			return nil
		})
		fmt.Println(Re)
		fmt.Println(err)
	}
}

grpc proto自动生成的文件 role.pb.go

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.25.0
// 	protoc        v3.14.0
// source: role.proto

//option go_package="pb";

package grpc_helper

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 RoleResponse 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"`
	Age  int32  `protobuf:"varint,3,opt,name=age,proto3" json:"age,omitempty"`
}

func (x *RoleResponse) Reset() {
	*x = RoleResponse{}
	if protoimpl.UnsafeEnabled {
		mi := &file_role_proto_msgTypes[0]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *RoleResponse) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*RoleResponse) ProtoMessage() {}

func (x *RoleResponse) ProtoReflect() protoreflect.Message {
	mi := &file_role_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 RoleResponse.ProtoReflect.Descriptor instead.
func (*RoleResponse) Descriptor() ([]byte, []int) {
	return file_role_proto_rawDescGZIP(), []int{0}
}

func (x *RoleResponse) GetId() int64 {
	if x != nil {
		return x.Id
	}
	return 0
}

func (x *RoleResponse) GetName() string {
	if x != nil {
		return x.Name
	}
	return ""
}

func (x *RoleResponse) GetAge() int32 {
	if x != nil {
		return x.Age
	}
	return 0
}

type RoleListResponse struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Rolelist []*RoleResponse `protobuf:"bytes,3,rep,name=Rolelist,proto3" json:"Rolelist,omitempty"`
}

func (x *RoleListResponse) Reset() {
	*x = RoleListResponse{}
	if protoimpl.UnsafeEnabled {
		mi := &file_role_proto_msgTypes[1]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *RoleListResponse) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*RoleListResponse) ProtoMessage() {}

func (x *RoleListResponse) ProtoReflect() protoreflect.Message {
	mi := &file_role_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 RoleListResponse.ProtoReflect.Descriptor instead.
func (*RoleListResponse) Descriptor() ([]byte, []int) {
	return file_role_proto_rawDescGZIP(), []int{1}
}

func (x *RoleListResponse) GetRolelist() []*RoleResponse {
	if x != nil {
		return x.Rolelist
	}
	return nil
}

type RoleListRequest 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"`
	Age  int32  `protobuf:"varint,3,opt,name=age,proto3" json:"age,omitempty"`
}

func (x *RoleListRequest) Reset() {
	*x = RoleListRequest{}
	if protoimpl.UnsafeEnabled {
		mi := &file_role_proto_msgTypes[2]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *RoleListRequest) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*RoleListRequest) ProtoMessage() {}

func (x *RoleListRequest) ProtoReflect() protoreflect.Message {
	mi := &file_role_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 RoleListRequest.ProtoReflect.Descriptor instead.
func (*RoleListRequest) Descriptor() ([]byte, []int) {
	return file_role_proto_rawDescGZIP(), []int{2}
}

func (x *RoleListRequest) GetId() int64 {
	if x != nil {
		return x.Id
	}
	return 0
}

func (x *RoleListRequest) GetName() string {
	if x != nil {
		return x.Name
	}
	return ""
}

func (x *RoleListRequest) GetAge() int32 {
	if x != nil {
		return x.Age
	}
	return 0
}

type RoleInfo 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"`
	Age  int32  `protobuf:"varint,3,opt,name=age,proto3" json:"age,omitempty"`
}

func (x *RoleInfo) Reset() {
	*x = RoleInfo{}
	if protoimpl.UnsafeEnabled {
		mi := &file_role_proto_msgTypes[3]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *RoleInfo) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*RoleInfo) ProtoMessage() {}

func (x *RoleInfo) ProtoReflect() protoreflect.Message {
	mi := &file_role_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 RoleInfo.ProtoReflect.Descriptor instead.
func (*RoleInfo) Descriptor() ([]byte, []int) {
	return file_role_proto_rawDescGZIP(), []int{3}
}

func (x *RoleInfo) GetId() int64 {
	if x != nil {
		return x.Id
	}
	return 0
}

func (x *RoleInfo) GetName() string {
	if x != nil {
		return x.Name
	}
	return ""
}

func (x *RoleInfo) GetAge() int32 {
	if x != nil {
		return x.Age
	}
	return 0
}

var File_role_proto protoreflect.FileDescriptor

var file_role_proto_rawDesc = []byte{
	0x0a, 0x0a, 0x72, 0x6f, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x70, 0x72,
	0x63, 0x22, 0x44, 0x0a, 0x0c, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
	0x65, 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, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01,
	0x28, 0x05, 0x52, 0x03, 0x61, 0x67, 0x65, 0x22, 0x41, 0x0a, 0x10, 0x52, 0x6f, 0x6c, 0x65, 0x4c,
	0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2d, 0x0a, 0x08, 0x52,
	0x6f, 0x6c, 0x65, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e,
	0x70, 0x72, 0x63, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
	0x52, 0x08, 0x52, 0x6f, 0x6c, 0x65, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x47, 0x0a, 0x0f, 0x52, 0x6f,
	0x6c, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 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, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03,
	0x61, 0x67, 0x65, 0x22, 0x40, 0x0a, 0x08, 0x52, 0x6f, 0x6c, 0x65, 0x49, 0x6e, 0x66, 0x6f, 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, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05,
	0x52, 0x03, 0x61, 0x67, 0x65, 0x32, 0x77, 0x0a, 0x04, 0x52, 0x6f, 0x6c, 0x65, 0x12, 0x39, 0x0a,
	0x08, 0x52, 0x6f, 0x6c, 0x65, 0x6c, 0x69, 0x73, 0x74, 0x12, 0x14, 0x2e, 0x70, 0x72, 0x63, 0x2e,
	0x52, 0x6f, 0x6c, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
	0x15, 0x2e, 0x70, 0x72, 0x63, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65,
	0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65,
	0x41, 0x64, 0x64, 0x12, 0x14, 0x2e, 0x70, 0x72, 0x63, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x4c, 0x69,
	0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x70, 0x72, 0x63, 0x2e,
	0x52, 0x6f, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x62, 0x06,
	0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_role_proto_rawDescOnce sync.Once
	file_role_proto_rawDescData = file_role_proto_rawDesc
)

func file_role_proto_rawDescGZIP() []byte {
	file_role_proto_rawDescOnce.Do(func() {
		file_role_proto_rawDescData = protoimpl.X.CompressGZIP(file_role_proto_rawDescData)
	})
	return file_role_proto_rawDescData
}

var file_role_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_role_proto_goTypes = []interface{}{
	(*RoleResponse)(nil),     // 0: prc.RoleResponse
	(*RoleListResponse)(nil), // 1: prc.RoleListResponse
	(*RoleListRequest)(nil),  // 2: prc.RoleListRequest
	(*RoleInfo)(nil),         // 3: prc.RoleInfo
}
var file_role_proto_depIdxs = []int32{
	0, // 0: prc.RoleListResponse.Rolelist:type_name -> prc.RoleResponse
	2, // 1: prc.Role.Rolelist:input_type -> prc.RoleListRequest
	2, // 2: prc.Role.RoleAdd:input_type -> prc.RoleListRequest
	1, // 3: prc.Role.Rolelist:output_type -> prc.RoleListResponse
	0, // 4: prc.Role.RoleAdd:output_type -> prc.RoleResponse
	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_role_proto_init() }
func file_role_proto_init() {
	if File_role_proto != nil {
		return
	}
	if !protoimpl.UnsafeEnabled {
		file_role_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*RoleResponse); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
		file_role_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*RoleListResponse); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
		file_role_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*RoleListRequest); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
		file_role_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*RoleInfo); 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_role_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   4,
			NumExtensions: 0,
			NumServices:   1,
		},
		GoTypes:           file_role_proto_goTypes,
		DependencyIndexes: file_role_proto_depIdxs,
		MessageInfos:      file_role_proto_msgTypes,
	}.Build()
	File_role_proto = out.File
	file_role_proto_rawDesc = nil
	file_role_proto_goTypes = nil
	file_role_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

// RoleClient is the client API for Role service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type RoleClient interface {
	Rolelist(ctx context.Context, in *RoleListRequest, opts ...grpc.CallOption) (*RoleListResponse, error)
	RoleAdd(ctx context.Context, in *RoleListRequest, opts ...grpc.CallOption) (*RoleResponse, error)
}

type roleClient struct {
	cc grpc.ClientConnInterface
}

func NewRoleClient(cc grpc.ClientConnInterface) RoleClient {
	return &roleClient{cc}
}

func (c *roleClient) Rolelist(ctx context.Context, in *RoleListRequest, opts ...grpc.CallOption) (*RoleListResponse, error) {
	out := new(RoleListResponse)
	err := c.cc.Invoke(ctx, "/prc.Role/Rolelist", in, out, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

func (c *roleClient) RoleAdd(ctx context.Context, in *RoleListRequest, opts ...grpc.CallOption) (*RoleResponse, error) {
	out := new(RoleResponse)
	err := c.cc.Invoke(ctx, "/prc.Role/RoleAdd", in, out, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// RoleServer is the server API for Role service.
type RoleServer interface {
	Rolelist(context.Context, *RoleListRequest) (*RoleListResponse, error)
	RoleAdd(context.Context, *RoleListRequest) (*RoleResponse, error)
}

// UnimplementedRoleServer can be embedded to have forward compatible implementations.
type UnimplementedRoleServer struct {
}

func (*UnimplementedRoleServer) Rolelist(context.Context, *RoleListRequest) (*RoleListResponse, error) {
	return nil, status.Errorf(codes.Unimplemented, "method Rolelist not implemented")
}
func (*UnimplementedRoleServer) RoleAdd(context.Context, *RoleListRequest) (*RoleResponse, error) {
	return nil, status.Errorf(codes.Unimplemented, "method RoleAdd not implemented")
}

func RegisterRoleServer(s *grpc.Server, srv RoleServer) {
	s.RegisterService(&_Role_serviceDesc, srv)
}

func _Role_Rolelist_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
	in := new(RoleListRequest)
	if err := dec(in); err != nil {
		return nil, err
	}
	if interceptor == nil {
		return srv.(RoleServer).Rolelist(ctx, in)
	}
	info := &grpc.UnaryServerInfo{
		Server:     srv,
		FullMethod: "/prc.Role/Rolelist",
	}
	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
		return srv.(RoleServer).Rolelist(ctx, req.(*RoleListRequest))
	}
	return interceptor(ctx, in, info, handler)
}

func _Role_RoleAdd_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
	in := new(RoleListRequest)
	if err := dec(in); err != nil {
		return nil, err
	}
	if interceptor == nil {
		return srv.(RoleServer).RoleAdd(ctx, in)
	}
	info := &grpc.UnaryServerInfo{
		Server:     srv,
		FullMethod: "/prc.Role/RoleAdd",
	}
	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
		return srv.(RoleServer).RoleAdd(ctx, req.(*RoleListRequest))
	}
	return interceptor(ctx, in, info, handler)
}

var _Role_serviceDesc = grpc.ServiceDesc{
	ServiceName: "prc.Role",
	HandlerType: (*RoleServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "Rolelist",
			Handler:    _Role_Rolelist_Handler,
		},
		{
			MethodName: "RoleAdd",
			Handler:    _Role_RoleAdd_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "role.proto",
}

role.go 实现  接口方法  Rolelist

package grpc_helper

import (
	"context"
)

type Role struct {

}

func (r Role) Rolelist(ctx context.Context, request *RoleListRequest) (rrs *RoleListResponse, err error) {

	var rsOne []*RoleResponse
	var ss RoleListResponse
	rsOne=append(rsOne,&RoleResponse{
		Id: 30,Name: request.Name,Age: 30,
	} )
	rsOne=append(rsOne,&RoleResponse{
		Id: 25,Name: request.Name,Age: 25,
	} )
	ss.Rolelist=rsOne
	rrs=&ss
	return


}

func (r Role) RoleAdd(ctx context.Context, request *RoleListRequest) (*RoleResponse, error) {
	panic("implement me")
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值