grpc

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

本项目为GRPC示例项目,此处为Go端项目,可独立运行,也可结合Java项目grpc-java/src/main/proto/grpc-demo.proto对应的项目配合使用

使用方式:

先编写xxx.proto文件到某个目录下, .proto文件可参考 ./grpc-demo.proto

image-20200927144353443

编写完成后打开Goland的Terminal, 在主目录下输入:

cd library/platform/grpc/demo
protoc -I ./ grpc-demo.proto --go_out=plugins=grpc:.

image-20200927145334722

其中:
-I    后面指定proto文件存放目录和proto文件
--go_out=plugins=grpc:   后面指定生成 .pb.go 代码存放的目录

之后会在下图目录中生成一个 .pb.go 文件, 此文件不要去编辑它

image-20200927144702284

然后开始编写Go的服务端,重点如下图

image-20200803141549143

image-20200803141731800

客户端重点如下:

image-20200803141850135

至此 Go项目的Grpc示例项目编写完成, 启动./service/grpc-demo-service.go可以启动服务端:

image-20200927144935078

image-20200927145019937

服务端启动后, 启动客户端:

image-20200803142328995

以上就是 Go端测试结果.

当配合 Java/grpc-java项目使用时, .proto文件在不同语言之间也应该保持一模一样, Go项目可以作为服务器, Java项目作为客户端; 也可以 Java作为服务端, Go项目作为客户端

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_grpc_demo_proto protoreflect.FileDescriptor

Functions

func RegisterGrpcDemoServiceServer

func RegisterGrpcDemoServiceServer(s *grpc.Server, srv GrpcDemoServiceServer)

Types

type GrpcDemoServiceClient

type GrpcDemoServiceClient interface {
	GetName(ctx context.Context, in *Person, opts ...grpc.CallOption) (*Name, error)
	AddOperation(ctx context.Context, in *Param, opts ...grpc.CallOption) (*Result, error)
}

GrpcDemoServiceClient is the client API for GrpcDemoService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

type GrpcDemoServiceServer

type GrpcDemoServiceServer interface {
	GetName(context.Context, *Person) (*Name, error)
	AddOperation(context.Context, *Param) (*Result, error)
}

GrpcDemoServiceServer is the server API for GrpcDemoService service.

type Name

type Name struct {
	Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
	// contains filtered or unexported fields
}

func (*Name) Descriptor deprecated

func (*Name) Descriptor() ([]byte, []int)

Deprecated: Use Name.ProtoReflect.Descriptor instead.

func (*Name) GetMessage

func (x *Name) GetMessage() string

func (*Name) ProtoMessage

func (*Name) ProtoMessage()

func (*Name) ProtoReflect

func (x *Name) ProtoReflect() protoreflect.Message

func (*Name) Reset

func (x *Name) Reset()

func (*Name) String

func (x *Name) String() string

type Param

type Param struct {
	X int32 `protobuf:"varint,1,opt,name=x,proto3" json:"x,omitempty"`
	Y int32 `protobuf:"varint,2,opt,name=y,proto3" json:"y,omitempty"`
	// contains filtered or unexported fields
}

func (*Param) Descriptor deprecated

func (*Param) Descriptor() ([]byte, []int)

Deprecated: Use Param.ProtoReflect.Descriptor instead.

func (*Param) GetX

func (x *Param) GetX() int32

func (*Param) GetY

func (x *Param) GetY() int32

func (*Param) ProtoMessage

func (*Param) ProtoMessage()

func (*Param) ProtoReflect

func (x *Param) ProtoReflect() protoreflect.Message

func (*Param) Reset

func (x *Param) Reset()

func (*Param) String

func (x *Param) String() string

type Person

type Person struct {
	FirstName string `protobuf:"bytes,1,opt,name=firstName,proto3" json:"firstName,omitempty"`
	LastName  string `protobuf:"bytes,2,opt,name=lastName,proto3" json:"lastName,omitempty"`
	// contains filtered or unexported fields
}

请求参数 基于序号的协议字段映射, 所以字段可以乱序, 可缺段

func (*Person) Descriptor deprecated

func (*Person) Descriptor() ([]byte, []int)

Deprecated: Use Person.ProtoReflect.Descriptor instead.

func (*Person) GetFirstName

func (x *Person) GetFirstName() string

func (*Person) GetLastName

func (x *Person) GetLastName() string

func (*Person) ProtoMessage

func (*Person) ProtoMessage()

func (*Person) ProtoReflect

func (x *Person) ProtoReflect() protoreflect.Message

func (*Person) Reset

func (x *Person) Reset()

func (*Person) String

func (x *Person) String() string

type Result

type Result struct {
	Message int32 `protobuf:"varint,1,opt,name=message,proto3" json:"message,omitempty"`
	// contains filtered or unexported fields
}

func (*Result) Descriptor deprecated

func (*Result) Descriptor() ([]byte, []int)

Deprecated: Use Result.ProtoReflect.Descriptor instead.

func (*Result) GetMessage

func (x *Result) GetMessage() int32

func (*Result) ProtoMessage

func (*Result) ProtoMessage()

func (*Result) ProtoReflect

func (x *Result) ProtoReflect() protoreflect.Message

func (*Result) Reset

func (x *Result) Reset()

func (*Result) String

func (x *Result) String() string

type UnimplementedGrpcDemoServiceServer

type UnimplementedGrpcDemoServiceServer struct {
}

UnimplementedGrpcDemoServiceServer can be embedded to have forward compatible implementations.

func (*UnimplementedGrpcDemoServiceServer) AddOperation

func (*UnimplementedGrpcDemoServiceServer) GetName

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL