commit demo1
这个提交包含在:
父节点
649363c589
当前提交
667f1eb3df
5
.gitignore
vendored
5
.gitignore
vendored
@ -8,7 +8,7 @@
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
.DS_Store
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
@ -17,7 +17,10 @@
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
*/vendor/
|
||||
*/vendor/*
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
go.sum
|
||||
|
||||
|
@ -1,2 +1,8 @@
|
||||
# micro_service
|
||||
|
||||
> 微服务简单教程
|
||||
|
||||
> 目录 `demo1` 是最基础的 `protobuf` 的简单教程和使用。
|
||||
|
||||
|
||||
[https://www.widuu.com](https://www.widuu.com) 更新教程说明,这里只是 `demo` 代码。
|
23
demo1/README.md
普通文件
23
demo1/README.md
普通文件
@ -0,0 +1,23 @@
|
||||
> 生成proto的命令
|
||||
|
||||
```shell
|
||||
protoc \
|
||||
--go_out=. \
|
||||
--go_opt=paths=import \
|
||||
--go-grpc_out=. \
|
||||
--go-grpc_opt=paths=import \
|
||||
--php_out=./php --grpc_out=./php --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin \
|
||||
```
|
||||
|
||||
> go 运行命令
|
||||
|
||||
```shell
|
||||
# 不带任何参数 会同时运行服务端和客户端进行测试后关闭 监听 1234 端口
|
||||
go run demo1/main.go
|
||||
|
||||
# mode 可以是 client 或者 server 来指定是客户端还是服务端
|
||||
go run demo2/main.go --mode=server
|
||||
|
||||
# 使用 port 参数来指定 监听端口 默认是 1234
|
||||
go run demo2/main.go --mode=server --port=5000
|
||||
```
|
65
demo1/cmd/client.go
普通文件
65
demo1/cmd/client.go
普通文件
@ -0,0 +1,65 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.widuu.com/widuu/micro_service/demo1/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
func RunClient(port string) {
|
||||
credential := insecure.NewCredentials()
|
||||
conn, err := grpc.NewClient(port, grpc.WithTransportCredentials(credential))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := proto.NewUserClient(conn)
|
||||
rsp, err := client.Register(context.Background(), &proto.RegisterRequest{
|
||||
Username: "test",
|
||||
Password: "123456",
|
||||
Email: "test@widuu.com",
|
||||
Mobile: "13800138000",
|
||||
Hobbies: []string{"football", "basketball"},
|
||||
Sex: proto.Sex_Male,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(status.FromError(err))
|
||||
return
|
||||
}
|
||||
if rsp.GetCode() != 1 {
|
||||
fmt.Println("Register Fail", rsp.GetMsg(), rsp.GetMessage())
|
||||
return
|
||||
}
|
||||
fmt.Println(rsp.GetMsg(), rsp.GetMessage(), rsp.GetInfo())
|
||||
// 登录
|
||||
other, _ := anypb.New(&proto.Code{
|
||||
Code: "0192E2",
|
||||
})
|
||||
rsp, err = client.Login(context.Background(), &proto.LoginRequest{
|
||||
Username: "test",
|
||||
Password: "123456",
|
||||
Other: other,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(status.FromError(err))
|
||||
return
|
||||
}
|
||||
if rsp.GetCode() != 1 {
|
||||
fmt.Println(rsp.GetMsg(), rsp.GetMessage())
|
||||
return
|
||||
}
|
||||
fmt.Println("Login Success, Token:", rsp.GetToken(), rsp.GetInfo())
|
||||
// 获取信息
|
||||
rsp, err = client.GetUserInfo(context.Background(), &emptypb.Empty{})
|
||||
if err != nil {
|
||||
fmt.Println(status.FromError(err))
|
||||
return
|
||||
}
|
||||
fmt.Println(rsp.GetMsg(), rsp.GetToken(), rsp.GetInfo()["username"])
|
||||
}
|
120
demo1/cmd/server.go
普通文件
120
demo1/cmd/server.go
普通文件
@ -0,0 +1,120 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
pb "code.widuu.com/widuu/micro_service/demo1/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type UserServer struct {
|
||||
// 继承 UnimplementedUserServer 实现的 mustEmbedUnimplementedUserServer
|
||||
pb.UnimplementedUserServer
|
||||
// 创建 map 用户信息模拟数据库存储
|
||||
User map[string]string
|
||||
// 加锁
|
||||
l sync.Mutex
|
||||
}
|
||||
|
||||
// 获取用户信息,正常需要传入token 而不是 empty 这里主要
|
||||
// 为了演示服务里使用 google.protobuf.Empty 类型
|
||||
func (u *UserServer) GetUserInfo(ctx context.Context, r *emptypb.Empty) (*pb.Response, error) {
|
||||
// 模拟登录
|
||||
u.l.Lock()
|
||||
defer u.l.Unlock()
|
||||
// 判断是否存在数据
|
||||
if _, ok := u.User["username"]; !ok {
|
||||
return nil, status.Error(codes.Aborted, "user not login")
|
||||
}
|
||||
return &pb.Response{
|
||||
Code: 1,
|
||||
Msg: "get user info success",
|
||||
Data: &pb.Response_Token{
|
||||
Token: u.User["token"],
|
||||
},
|
||||
Info: u.User,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *UserServer) Login(ctx context.Context, r *pb.LoginRequest) (*pb.Response, error) {
|
||||
// 模拟登录
|
||||
u.l.Lock()
|
||||
defer u.l.Unlock()
|
||||
// 判断是否存在
|
||||
if _, ok := u.User["username"]; !ok {
|
||||
return nil, status.Error(codes.Aborted, "user not exists")
|
||||
}
|
||||
if pass, ok := u.User["password"]; !ok || pass != r.Password {
|
||||
return nil, status.Error(codes.Aborted, "user password error")
|
||||
}
|
||||
// 完全是为了解析 any 类型演示使用
|
||||
info := map[string]string{"username": u.User["username"]}
|
||||
// 如果有Code
|
||||
if r.Other != nil {
|
||||
var code pb.Code
|
||||
if err := r.Other.UnmarshalTo(&code); err == nil {
|
||||
info["code"] = code.Code
|
||||
}
|
||||
}
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(r.Password))
|
||||
u.User["token"] = fmt.Sprintf("%x", hash.Sum(nil))
|
||||
return &pb.Response{
|
||||
Code: 1,
|
||||
Msg: "resgister success",
|
||||
Data: &pb.Response_Token{
|
||||
Token: u.User["token"],
|
||||
},
|
||||
Info: info,
|
||||
}, nil
|
||||
}
|
||||
func (u *UserServer) Register(ctx context.Context, r *pb.RegisterRequest) (*pb.Response, error) {
|
||||
u.l.Lock()
|
||||
defer u.l.Unlock()
|
||||
// 判断是否存在
|
||||
if _, ok := u.User["username"]; ok {
|
||||
return nil, status.Error(codes.Aborted, "user already exists")
|
||||
}
|
||||
// 注册
|
||||
u.User["username"] = r.Username
|
||||
u.User["password"] = r.Password
|
||||
u.User["email"] = r.Email
|
||||
u.User["mobile"] = r.Mobile
|
||||
u.User["hobbies"] = strings.Join(r.Hobbies, ",")
|
||||
return &pb.Response{
|
||||
Code: 1,
|
||||
Msg: "resgister success",
|
||||
Data: &pb.Response_Message{
|
||||
Message: "ok",
|
||||
},
|
||||
Info: u.User,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func RunServer(port string, ctx context.Context) {
|
||||
srv := grpc.NewServer()
|
||||
pb.RegisterUserServer(srv, &UserServer{
|
||||
User: make(map[string]string, 6),
|
||||
})
|
||||
l, err := net.Listen("tcp", port)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
srv.GracefulStop()
|
||||
}()
|
||||
if err := srv.Serve(l); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
34
demo1/main.go
普通文件
34
demo1/main.go
普通文件
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"strings"
|
||||
|
||||
"code.widuu.com/widuu/micro_service/demo1/cmd"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.String("port", ":1234", "run port")
|
||||
runMode = flag.String("mode", "", "run mode")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if !strings.Contains(*port, ":") {
|
||||
*port = ":" + *port
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
switch *runMode {
|
||||
case "client":
|
||||
cmd.RunClient(*port)
|
||||
return
|
||||
case "server":
|
||||
cmd.RunServer(*port, ctx)
|
||||
return
|
||||
default:
|
||||
go cmd.RunServer(*port, ctx)
|
||||
cmd.RunClient(*port)
|
||||
}
|
||||
}
|
559
demo1/proto/user.pb.go
普通文件
559
demo1/proto/user.pb.go
普通文件
@ -0,0 +1,559 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.34.2
|
||||
// protoc v3.21.11
|
||||
// source: demo1/proto/user.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
anypb "google.golang.org/protobuf/types/known/anypb"
|
||||
emptypb "google.golang.org/protobuf/types/known/emptypb"
|
||||
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 Sex int32
|
||||
|
||||
const (
|
||||
Sex_Male Sex = 0
|
||||
Sex_Female Sex = 1
|
||||
Sex_Unknown Sex = 2
|
||||
)
|
||||
|
||||
// Enum value maps for Sex.
|
||||
var (
|
||||
Sex_name = map[int32]string{
|
||||
0: "Male",
|
||||
1: "Female",
|
||||
2: "Unknown",
|
||||
}
|
||||
Sex_value = map[string]int32{
|
||||
"Male": 0,
|
||||
"Female": 1,
|
||||
"Unknown": 2,
|
||||
}
|
||||
)
|
||||
|
||||
func (x Sex) Enum() *Sex {
|
||||
p := new(Sex)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Sex) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
||||
func (Sex) Descriptor() protoreflect.EnumDescriptor {
|
||||
return file_demo1_proto_user_proto_enumTypes[0].Descriptor()
|
||||
}
|
||||
|
||||
func (Sex) Type() protoreflect.EnumType {
|
||||
return &file_demo1_proto_user_proto_enumTypes[0]
|
||||
}
|
||||
|
||||
func (x Sex) Number() protoreflect.EnumNumber {
|
||||
return protoreflect.EnumNumber(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Sex.Descriptor instead.
|
||||
func (Sex) EnumDescriptor() ([]byte, []int) {
|
||||
return file_demo1_proto_user_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type Code struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Code) Reset() {
|
||||
*x = Code{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_demo1_proto_user_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Code) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Code) ProtoMessage() {}
|
||||
|
||||
func (x *Code) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_demo1_proto_user_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 Code.ProtoReflect.Descriptor instead.
|
||||
func (*Code) Descriptor() ([]byte, []int) {
|
||||
return file_demo1_proto_user_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *Code) GetCode() string {
|
||||
if x != nil {
|
||||
return x.Code
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 定义 LoginRequest 消息, 包含 username, password 两个字段
|
||||
type LoginRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"`
|
||||
Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
|
||||
Other *anypb.Any `protobuf:"bytes,3,opt,name=other,proto3" json:"other,omitempty"`
|
||||
}
|
||||
|
||||
func (x *LoginRequest) Reset() {
|
||||
*x = LoginRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_demo1_proto_user_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *LoginRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*LoginRequest) ProtoMessage() {}
|
||||
|
||||
func (x *LoginRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_demo1_proto_user_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 LoginRequest.ProtoReflect.Descriptor instead.
|
||||
func (*LoginRequest) Descriptor() ([]byte, []int) {
|
||||
return file_demo1_proto_user_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *LoginRequest) GetUsername() string {
|
||||
if x != nil {
|
||||
return x.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *LoginRequest) GetPassword() string {
|
||||
if x != nil {
|
||||
return x.Password
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *LoginRequest) GetOther() *anypb.Any {
|
||||
if x != nil {
|
||||
return x.Other
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 定义 RegisterRequest 消息, 包含 username, password, email, mobile 四个字段
|
||||
type RegisterRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"`
|
||||
Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
|
||||
Email string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"`
|
||||
Mobile string `protobuf:"bytes,4,opt,name=mobile,proto3" json:"mobile,omitempty"`
|
||||
Sex Sex `protobuf:"varint,5,opt,name=sex,proto3,enum=proto.Sex" json:"sex,omitempty"`
|
||||
Hobbies []string `protobuf:"bytes,6,rep,name=hobbies,proto3" json:"hobbies,omitempty"`
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) Reset() {
|
||||
*x = RegisterRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_demo1_proto_user_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*RegisterRequest) ProtoMessage() {}
|
||||
|
||||
func (x *RegisterRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_demo1_proto_user_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 RegisterRequest.ProtoReflect.Descriptor instead.
|
||||
func (*RegisterRequest) Descriptor() ([]byte, []int) {
|
||||
return file_demo1_proto_user_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetUsername() string {
|
||||
if x != nil {
|
||||
return x.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetPassword() string {
|
||||
if x != nil {
|
||||
return x.Password
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetEmail() string {
|
||||
if x != nil {
|
||||
return x.Email
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetMobile() string {
|
||||
if x != nil {
|
||||
return x.Mobile
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetSex() Sex {
|
||||
if x != nil {
|
||||
return x.Sex
|
||||
}
|
||||
return Sex_Male
|
||||
}
|
||||
|
||||
func (x *RegisterRequest) GetHobbies() []string {
|
||||
if x != nil {
|
||||
return x.Hobbies
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 定义 Response 消息, 包含 code, msg, data 三个字段
|
||||
// 其中 data 字段使用 oneof 关键字, 表示 data 字段只能有一个字段被赋值
|
||||
type Response struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
|
||||
Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"`
|
||||
// Types that are assignable to Data:
|
||||
//
|
||||
// *Response_Token
|
||||
// *Response_Message
|
||||
Data isResponse_Data `protobuf_oneof:"data"`
|
||||
Info map[string]string `protobuf:"bytes,5,rep,name=info,proto3" json:"info,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
|
||||
}
|
||||
|
||||
func (x *Response) Reset() {
|
||||
*x = Response{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_demo1_proto_user_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Response) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Response) ProtoMessage() {}
|
||||
|
||||
func (x *Response) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_demo1_proto_user_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 Response.ProtoReflect.Descriptor instead.
|
||||
func (*Response) Descriptor() ([]byte, []int) {
|
||||
return file_demo1_proto_user_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *Response) GetCode() int32 {
|
||||
if x != nil {
|
||||
return x.Code
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (x *Response) GetMsg() string {
|
||||
if x != nil {
|
||||
return x.Msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Response) GetData() isResponse_Data {
|
||||
if m != nil {
|
||||
return m.Data
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Response) GetToken() string {
|
||||
if x, ok := x.GetData().(*Response_Token); ok {
|
||||
return x.Token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Response) GetMessage() string {
|
||||
if x, ok := x.GetData().(*Response_Message); ok {
|
||||
return x.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Response) GetInfo() map[string]string {
|
||||
if x != nil {
|
||||
return x.Info
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type isResponse_Data interface {
|
||||
isResponse_Data()
|
||||
}
|
||||
|
||||
type Response_Token struct {
|
||||
Token string `protobuf:"bytes,3,opt,name=token,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Response_Message struct {
|
||||
Message string `protobuf:"bytes,4,opt,name=message,proto3,oneof"`
|
||||
}
|
||||
|
||||
func (*Response_Token) isResponse_Data() {}
|
||||
|
||||
func (*Response_Message) isResponse_Data() {}
|
||||
|
||||
var File_demo1_proto_user_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_demo1_proto_user_proto_rawDesc = []byte{
|
||||
0x0a, 0x16, 0x64, 0x65, 0x6d, 0x6f, 0x31, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x75, 0x73,
|
||||
0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a,
|
||||
0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
|
||||
0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x19, 0x67, 0x6f,
|
||||
0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x61, 0x6e,
|
||||
0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x1a, 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x12,
|
||||
0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63,
|
||||
0x6f, 0x64, 0x65, 0x22, 0x72, 0x0a, 0x0c, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75,
|
||||
0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75, 0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12,
|
||||
0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28,
|
||||
0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x12, 0x2a, 0x0a, 0x05, 0x6f,
|
||||
0x74, 0x68, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f,
|
||||
0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79,
|
||||
0x52, 0x05, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x22, 0xbc, 0x01, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69,
|
||||
0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x75,
|
||||
0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x75,
|
||||
0x73, 0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77,
|
||||
0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77,
|
||||
0x6f, 0x72, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x03, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x05, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, 0x62,
|
||||
0x69, 0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x62, 0x69, 0x6c,
|
||||
0x65, 0x12, 0x1c, 0x0a, 0x03, 0x73, 0x65, 0x78, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a,
|
||||
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x65, 0x78, 0x52, 0x03, 0x73, 0x65, 0x78, 0x12,
|
||||
0x18, 0x0a, 0x07, 0x68, 0x6f, 0x62, 0x62, 0x69, 0x65, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09,
|
||||
0x52, 0x07, 0x68, 0x6f, 0x62, 0x62, 0x69, 0x65, 0x73, 0x52, 0x04, 0x63, 0x69, 0x74, 0x79, 0x52,
|
||||
0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x22, 0xd4, 0x01, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f,
|
||||
0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
|
||||
0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x02,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x16, 0x0a, 0x05, 0x74, 0x6f, 0x6b,
|
||||
0x65, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65,
|
||||
0x6e, 0x12, 0x1a, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x04, 0x20, 0x01,
|
||||
0x28, 0x09, 0x48, 0x00, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x2d, 0x0a,
|
||||
0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x49, 0x6e, 0x66,
|
||||
0x6f, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x1a, 0x37, 0x0a, 0x09,
|
||||
0x49, 0x6e, 0x66, 0x6f, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79,
|
||||
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76,
|
||||
0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75,
|
||||
0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x06, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x2a, 0x28, 0x0a,
|
||||
0x03, 0x53, 0x65, 0x78, 0x12, 0x08, 0x0a, 0x04, 0x4d, 0x61, 0x6c, 0x65, 0x10, 0x00, 0x12, 0x0a,
|
||||
0x0a, 0x06, 0x46, 0x65, 0x6d, 0x61, 0x6c, 0x65, 0x10, 0x01, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x6e,
|
||||
0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x10, 0x02, 0x32, 0xa2, 0x01, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72,
|
||||
0x12, 0x2d, 0x0a, 0x05, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0f,
|
||||
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12,
|
||||
0x33, 0x0a, 0x08, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x12, 0x16, 0x2e, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75,
|
||||
0x65, 0x73, 0x74, 0x1a, 0x0f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70,
|
||||
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x36, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x55, 0x73, 0x65, 0x72, 0x49,
|
||||
0x6e, 0x66, 0x6f, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x0f, 0x2e, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x13, 0x5a, 0x11,
|
||||
0x64, 0x65, 0x6d, 0x6f, 0x31, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_demo1_proto_user_proto_rawDescOnce sync.Once
|
||||
file_demo1_proto_user_proto_rawDescData = file_demo1_proto_user_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_demo1_proto_user_proto_rawDescGZIP() []byte {
|
||||
file_demo1_proto_user_proto_rawDescOnce.Do(func() {
|
||||
file_demo1_proto_user_proto_rawDescData = protoimpl.X.CompressGZIP(file_demo1_proto_user_proto_rawDescData)
|
||||
})
|
||||
return file_demo1_proto_user_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_demo1_proto_user_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
|
||||
var file_demo1_proto_user_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
|
||||
var file_demo1_proto_user_proto_goTypes = []any{
|
||||
(Sex)(0), // 0: proto.Sex
|
||||
(*Code)(nil), // 1: proto.Code
|
||||
(*LoginRequest)(nil), // 2: proto.LoginRequest
|
||||
(*RegisterRequest)(nil), // 3: proto.RegisterRequest
|
||||
(*Response)(nil), // 4: proto.Response
|
||||
nil, // 5: proto.Response.InfoEntry
|
||||
(*anypb.Any)(nil), // 6: google.protobuf.Any
|
||||
(*emptypb.Empty)(nil), // 7: google.protobuf.Empty
|
||||
}
|
||||
var file_demo1_proto_user_proto_depIdxs = []int32{
|
||||
6, // 0: proto.LoginRequest.other:type_name -> google.protobuf.Any
|
||||
0, // 1: proto.RegisterRequest.sex:type_name -> proto.Sex
|
||||
5, // 2: proto.Response.info:type_name -> proto.Response.InfoEntry
|
||||
2, // 3: proto.User.Login:input_type -> proto.LoginRequest
|
||||
3, // 4: proto.User.Register:input_type -> proto.RegisterRequest
|
||||
7, // 5: proto.User.GetUserInfo:input_type -> google.protobuf.Empty
|
||||
4, // 6: proto.User.Login:output_type -> proto.Response
|
||||
4, // 7: proto.User.Register:output_type -> proto.Response
|
||||
4, // 8: proto.User.GetUserInfo:output_type -> proto.Response
|
||||
6, // [6:9] is the sub-list for method output_type
|
||||
3, // [3:6] is the sub-list for method input_type
|
||||
3, // [3:3] is the sub-list for extension type_name
|
||||
3, // [3:3] is the sub-list for extension extendee
|
||||
0, // [0:3] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_demo1_proto_user_proto_init() }
|
||||
func file_demo1_proto_user_proto_init() {
|
||||
if File_demo1_proto_user_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_demo1_proto_user_proto_msgTypes[0].Exporter = func(v any, i int) any {
|
||||
switch v := v.(*Code); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_demo1_proto_user_proto_msgTypes[1].Exporter = func(v any, i int) any {
|
||||
switch v := v.(*LoginRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_demo1_proto_user_proto_msgTypes[2].Exporter = func(v any, i int) any {
|
||||
switch v := v.(*RegisterRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_demo1_proto_user_proto_msgTypes[3].Exporter = func(v any, i int) any {
|
||||
switch v := v.(*Response); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
file_demo1_proto_user_proto_msgTypes[3].OneofWrappers = []any{
|
||||
(*Response_Token)(nil),
|
||||
(*Response_Message)(nil),
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_demo1_proto_user_proto_rawDesc,
|
||||
NumEnums: 1,
|
||||
NumMessages: 5,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_demo1_proto_user_proto_goTypes,
|
||||
DependencyIndexes: file_demo1_proto_user_proto_depIdxs,
|
||||
EnumInfos: file_demo1_proto_user_proto_enumTypes,
|
||||
MessageInfos: file_demo1_proto_user_proto_msgTypes,
|
||||
}.Build()
|
||||
File_demo1_proto_user_proto = out.File
|
||||
file_demo1_proto_user_proto_rawDesc = nil
|
||||
file_demo1_proto_user_proto_goTypes = nil
|
||||
file_demo1_proto_user_proto_depIdxs = nil
|
||||
}
|
60
demo1/proto/user.proto
普通文件
60
demo1/proto/user.proto
普通文件
@ -0,0 +1,60 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package proto;
|
||||
|
||||
// 定义 Go 包名为 demo1, 生成的 Go 代码包名为 demo1
|
||||
option go_package = "demo1/proto;proto";
|
||||
|
||||
import "google/protobuf/empty.proto";
|
||||
import "google/protobuf/any.proto";
|
||||
|
||||
enum Sex {
|
||||
Male = 0;
|
||||
Female = 1;
|
||||
Unknown = 2;
|
||||
}
|
||||
|
||||
message Code {
|
||||
string code = 1;
|
||||
}
|
||||
|
||||
// 定义 LoginRequest 消息, 包含 username, password 两个字段
|
||||
message LoginRequest {
|
||||
string username = 1;
|
||||
string password = 2;
|
||||
google.protobuf.Any other = 3;
|
||||
}
|
||||
|
||||
// 定义 RegisterRequest 消息, 包含 username, password, email, mobile 四个字段
|
||||
message RegisterRequest {
|
||||
string username = 1;
|
||||
string password = 2;
|
||||
string email = 3;
|
||||
string mobile = 4;
|
||||
Sex sex = 5;
|
||||
repeated string hobbies = 6;
|
||||
// 以下字段为预留字段, 不参与序列化和反序列化
|
||||
reserved "city", "money";
|
||||
}
|
||||
|
||||
// 定义 Response 消息, 包含 code, msg, data 三个字段
|
||||
// 其中 data 字段使用 oneof 关键字, 表示 data 字段只能有一个字段被赋值
|
||||
message Response {
|
||||
int32 code = 1;
|
||||
string msg = 2;
|
||||
oneof data {
|
||||
string token = 3;
|
||||
string message = 4;
|
||||
}
|
||||
map<string, string> info = 5;
|
||||
}
|
||||
|
||||
// 定义 User 服务
|
||||
service User {
|
||||
// 登录服务
|
||||
rpc Login(LoginRequest) returns (Response);
|
||||
// 注册服务
|
||||
rpc Register(RegisterRequest) returns (Response);
|
||||
// 获取用户信息服务
|
||||
rpc GetUserInfo(google.protobuf.Empty) returns (Response);
|
||||
}
|
208
demo1/proto/user_grpc.pb.go
普通文件
208
demo1/proto/user_grpc.pb.go
普通文件
@ -0,0 +1,208 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.5.1
|
||||
// - protoc v3.21.11
|
||||
// source: demo1/proto/user.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
emptypb "google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
// 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.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
User_Login_FullMethodName = "/proto.User/Login"
|
||||
User_Register_FullMethodName = "/proto.User/Register"
|
||||
User_GetUserInfo_FullMethodName = "/proto.User/GetUserInfo"
|
||||
)
|
||||
|
||||
// UserClient is the client API for User 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.
|
||||
//
|
||||
// 定义 User 服务
|
||||
type UserClient interface {
|
||||
// 登录服务
|
||||
Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*Response, error)
|
||||
// 注册服务
|
||||
Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*Response, error)
|
||||
// 获取用户信息服务
|
||||
GetUserInfo(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
type userClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewUserClient(cc grpc.ClientConnInterface) UserClient {
|
||||
return &userClient{cc}
|
||||
}
|
||||
|
||||
func (c *userClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*Response, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(Response)
|
||||
err := c.cc.Invoke(ctx, User_Login_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) Register(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (*Response, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(Response)
|
||||
err := c.cc.Invoke(ctx, User_Register_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userClient) GetUserInfo(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*Response, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(Response)
|
||||
err := c.cc.Invoke(ctx, User_GetUserInfo_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UserServer is the server API for User service.
|
||||
// All implementations must embed UnimplementedUserServer
|
||||
// for forward compatibility.
|
||||
//
|
||||
// 定义 User 服务
|
||||
type UserServer interface {
|
||||
// 登录服务
|
||||
Login(context.Context, *LoginRequest) (*Response, error)
|
||||
// 注册服务
|
||||
Register(context.Context, *RegisterRequest) (*Response, error)
|
||||
// 获取用户信息服务
|
||||
GetUserInfo(context.Context, *emptypb.Empty) (*Response, error)
|
||||
mustEmbedUnimplementedUserServer()
|
||||
}
|
||||
|
||||
// UnimplementedUserServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedUserServer struct{}
|
||||
|
||||
func (UnimplementedUserServer) Login(context.Context, *LoginRequest) (*Response, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Login not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) Register(context.Context, *RegisterRequest) (*Response, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Register not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) GetUserInfo(context.Context, *emptypb.Empty) (*Response, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetUserInfo not implemented")
|
||||
}
|
||||
func (UnimplementedUserServer) mustEmbedUnimplementedUserServer() {}
|
||||
func (UnimplementedUserServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeUserServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to UserServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeUserServer interface {
|
||||
mustEmbedUnimplementedUserServer()
|
||||
}
|
||||
|
||||
func RegisterUserServer(s grpc.ServiceRegistrar, srv UserServer) {
|
||||
// If the following call pancis, it indicates UnimplementedUserServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&User_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _User_Login_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(LoginRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).Login(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_Login_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).Login(ctx, req.(*LoginRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_Register_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(RegisterRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).Register(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_Register_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).Register(ctx, req.(*RegisterRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _User_GetUserInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(emptypb.Empty)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServer).GetUserInfo(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: User_GetUserInfo_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServer).GetUserInfo(ctx, req.(*emptypb.Empty))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// User_ServiceDesc is the grpc.ServiceDesc for User service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var User_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "proto.User",
|
||||
HandlerType: (*UserServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Login",
|
||||
Handler: _User_Login_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Register",
|
||||
Handler: _User_Register_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetUserInfo",
|
||||
Handler: _User_GetUserInfo_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "demo1/proto/user.proto",
|
||||
}
|
19
go.mod
普通文件
19
go.mod
普通文件
@ -0,0 +1,19 @@
|
||||
module code.widuu.com/widuu/micro_service
|
||||
|
||||
go 1.22.1
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.9.0
|
||||
google.golang.org/grpc v1.67.0
|
||||
google.golang.org/protobuf v1.34.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/sys v0.24.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
二进制
php/GPBMetadata/Demo1/Proto/User.php
普通文件
二进制
php/GPBMetadata/Demo1/Proto/User.php
普通文件
二进制文件未显示。
58
php/Proto/Code.php
普通文件
58
php/Proto/Code.php
普通文件
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: demo1/proto/user.proto
|
||||
|
||||
namespace Proto;
|
||||
|
||||
use Google\Protobuf\Internal\GPBType;
|
||||
use Google\Protobuf\Internal\RepeatedField;
|
||||
use Google\Protobuf\Internal\GPBUtil;
|
||||
|
||||
/**
|
||||
* Generated from protobuf message <code>proto.Code</code>
|
||||
*/
|
||||
class Code extends \Google\Protobuf\Internal\Message
|
||||
{
|
||||
/**
|
||||
* Generated from protobuf field <code>string code = 1;</code>
|
||||
*/
|
||||
protected $code = '';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $data {
|
||||
* Optional. Data for populating the Message object.
|
||||
*
|
||||
* @type string $code
|
||||
* }
|
||||
*/
|
||||
public function __construct($data = NULL) {
|
||||
\GPBMetadata\Demo1\Proto\User::initOnce();
|
||||
parent::__construct($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string code = 1;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string code = 1;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setCode($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->code = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
124
php/Proto/LoginRequest.php
普通文件
124
php/Proto/LoginRequest.php
普通文件
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: demo1/proto/user.proto
|
||||
|
||||
namespace Proto;
|
||||
|
||||
use Google\Protobuf\Internal\GPBType;
|
||||
use Google\Protobuf\Internal\RepeatedField;
|
||||
use Google\Protobuf\Internal\GPBUtil;
|
||||
|
||||
/**
|
||||
* 定义 LoginRequest 消息, 包含 username, password 两个字段
|
||||
*
|
||||
* Generated from protobuf message <code>proto.LoginRequest</code>
|
||||
*/
|
||||
class LoginRequest extends \Google\Protobuf\Internal\Message
|
||||
{
|
||||
/**
|
||||
* Generated from protobuf field <code>string username = 1;</code>
|
||||
*/
|
||||
protected $username = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>string password = 2;</code>
|
||||
*/
|
||||
protected $password = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>.google.protobuf.Any other = 3;</code>
|
||||
*/
|
||||
protected $other = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $data {
|
||||
* Optional. Data for populating the Message object.
|
||||
*
|
||||
* @type string $username
|
||||
* @type string $password
|
||||
* @type \Google\Protobuf\Any $other
|
||||
* }
|
||||
*/
|
||||
public function __construct($data = NULL) {
|
||||
\GPBMetadata\Demo1\Proto\User::initOnce();
|
||||
parent::__construct($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string username = 1;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string username = 1;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setUsername($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->username = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string password = 2;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string password = 2;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setPassword($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->password = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>.google.protobuf.Any other = 3;</code>
|
||||
* @return \Google\Protobuf\Any|null
|
||||
*/
|
||||
public function getOther()
|
||||
{
|
||||
return $this->other;
|
||||
}
|
||||
|
||||
public function hasOther()
|
||||
{
|
||||
return isset($this->other);
|
||||
}
|
||||
|
||||
public function clearOther()
|
||||
{
|
||||
unset($this->other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>.google.protobuf.Any other = 3;</code>
|
||||
* @param \Google\Protobuf\Any $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setOther($var)
|
||||
{
|
||||
GPBUtil::checkMessage($var, \Google\Protobuf\Any::class);
|
||||
$this->other = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
195
php/Proto/RegisterRequest.php
普通文件
195
php/Proto/RegisterRequest.php
普通文件
@ -0,0 +1,195 @@
|
||||
<?php
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: demo1/proto/user.proto
|
||||
|
||||
namespace Proto;
|
||||
|
||||
use Google\Protobuf\Internal\GPBType;
|
||||
use Google\Protobuf\Internal\RepeatedField;
|
||||
use Google\Protobuf\Internal\GPBUtil;
|
||||
|
||||
/**
|
||||
* 定义 RegisterRequest 消息, 包含 username, password, email, mobile 四个字段
|
||||
*
|
||||
* Generated from protobuf message <code>proto.RegisterRequest</code>
|
||||
*/
|
||||
class RegisterRequest extends \Google\Protobuf\Internal\Message
|
||||
{
|
||||
/**
|
||||
* Generated from protobuf field <code>string username = 1;</code>
|
||||
*/
|
||||
protected $username = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>string password = 2;</code>
|
||||
*/
|
||||
protected $password = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>string email = 3;</code>
|
||||
*/
|
||||
protected $email = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>string mobile = 4;</code>
|
||||
*/
|
||||
protected $mobile = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>.proto.Sex sex = 5;</code>
|
||||
*/
|
||||
protected $sex = 0;
|
||||
/**
|
||||
* Generated from protobuf field <code>repeated string hobbies = 6;</code>
|
||||
*/
|
||||
private $hobbies;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $data {
|
||||
* Optional. Data for populating the Message object.
|
||||
*
|
||||
* @type string $username
|
||||
* @type string $password
|
||||
* @type string $email
|
||||
* @type string $mobile
|
||||
* @type int $sex
|
||||
* @type array<string>|\Google\Protobuf\Internal\RepeatedField $hobbies
|
||||
* }
|
||||
*/
|
||||
public function __construct($data = NULL) {
|
||||
\GPBMetadata\Demo1\Proto\User::initOnce();
|
||||
parent::__construct($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string username = 1;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string username = 1;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setUsername($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->username = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string password = 2;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string password = 2;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setPassword($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->password = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string email = 3;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string email = 3;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setEmail($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->email = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string mobile = 4;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getMobile()
|
||||
{
|
||||
return $this->mobile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string mobile = 4;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobile($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->mobile = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>.proto.Sex sex = 5;</code>
|
||||
* @return int
|
||||
*/
|
||||
public function getSex()
|
||||
{
|
||||
return $this->sex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>.proto.Sex sex = 5;</code>
|
||||
* @param int $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setSex($var)
|
||||
{
|
||||
GPBUtil::checkEnum($var, \Proto\Sex::class);
|
||||
$this->sex = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>repeated string hobbies = 6;</code>
|
||||
* @return \Google\Protobuf\Internal\RepeatedField
|
||||
*/
|
||||
public function getHobbies()
|
||||
{
|
||||
return $this->hobbies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>repeated string hobbies = 6;</code>
|
||||
* @param array<string>|\Google\Protobuf\Internal\RepeatedField $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setHobbies($var)
|
||||
{
|
||||
$arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING);
|
||||
$this->hobbies = $arr;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
180
php/Proto/Response.php
普通文件
180
php/Proto/Response.php
普通文件
@ -0,0 +1,180 @@
|
||||
<?php
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: demo1/proto/user.proto
|
||||
|
||||
namespace Proto;
|
||||
|
||||
use Google\Protobuf\Internal\GPBType;
|
||||
use Google\Protobuf\Internal\RepeatedField;
|
||||
use Google\Protobuf\Internal\GPBUtil;
|
||||
|
||||
/**
|
||||
* 定义 Response 消息, 包含 code, msg, data 三个字段
|
||||
* 其中 data 字段使用 oneof 关键字, 表示 data 字段只能有一个字段被赋值
|
||||
*
|
||||
* Generated from protobuf message <code>proto.Response</code>
|
||||
*/
|
||||
class Response extends \Google\Protobuf\Internal\Message
|
||||
{
|
||||
/**
|
||||
* Generated from protobuf field <code>int32 code = 1;</code>
|
||||
*/
|
||||
protected $code = 0;
|
||||
/**
|
||||
* Generated from protobuf field <code>string msg = 2;</code>
|
||||
*/
|
||||
protected $msg = '';
|
||||
/**
|
||||
* Generated from protobuf field <code>map<string, string> info = 5;</code>
|
||||
*/
|
||||
private $info;
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $data {
|
||||
* Optional. Data for populating the Message object.
|
||||
*
|
||||
* @type int $code
|
||||
* @type string $msg
|
||||
* @type string $token
|
||||
* @type string $message
|
||||
* @type array|\Google\Protobuf\Internal\MapField $info
|
||||
* }
|
||||
*/
|
||||
public function __construct($data = NULL) {
|
||||
\GPBMetadata\Demo1\Proto\User::initOnce();
|
||||
parent::__construct($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>int32 code = 1;</code>
|
||||
* @return int
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>int32 code = 1;</code>
|
||||
* @param int $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setCode($var)
|
||||
{
|
||||
GPBUtil::checkInt32($var);
|
||||
$this->code = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string msg = 2;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getMsg()
|
||||
{
|
||||
return $this->msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string msg = 2;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setMsg($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->msg = $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string token = 3;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
return $this->readOneof(3);
|
||||
}
|
||||
|
||||
public function hasToken()
|
||||
{
|
||||
return $this->hasOneof(3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string token = 3;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setToken($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->writeOneof(3, $var);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string message = 4;</code>
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->readOneof(4);
|
||||
}
|
||||
|
||||
public function hasMessage()
|
||||
{
|
||||
return $this->hasOneof(4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>string message = 4;</code>
|
||||
* @param string $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setMessage($var)
|
||||
{
|
||||
GPBUtil::checkString($var, True);
|
||||
$this->writeOneof(4, $var);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>map<string, string> info = 5;</code>
|
||||
* @return \Google\Protobuf\Internal\MapField
|
||||
*/
|
||||
public function getInfo()
|
||||
{
|
||||
return $this->info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated from protobuf field <code>map<string, string> info = 5;</code>
|
||||
* @param array|\Google\Protobuf\Internal\MapField $var
|
||||
* @return $this
|
||||
*/
|
||||
public function setInfo($var)
|
||||
{
|
||||
$arr = GPBUtil::checkMapField($var, \Google\Protobuf\Internal\GPBType::STRING, \Google\Protobuf\Internal\GPBType::STRING);
|
||||
$this->info = $arr;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->whichOneof("data");
|
||||
}
|
||||
|
||||
}
|
||||
|
53
php/Proto/Sex.php
普通文件
53
php/Proto/Sex.php
普通文件
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: demo1/proto/user.proto
|
||||
|
||||
namespace Proto;
|
||||
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Protobuf type <code>proto.Sex</code>
|
||||
*/
|
||||
class Sex
|
||||
{
|
||||
/**
|
||||
* Generated from protobuf enum <code>Male = 0;</code>
|
||||
*/
|
||||
const Male = 0;
|
||||
/**
|
||||
* Generated from protobuf enum <code>Female = 1;</code>
|
||||
*/
|
||||
const Female = 1;
|
||||
/**
|
||||
* Generated from protobuf enum <code>Unknown = 2;</code>
|
||||
*/
|
||||
const Unknown = 2;
|
||||
|
||||
private static $valueToName = [
|
||||
self::Male => 'Male',
|
||||
self::Female => 'Female',
|
||||
self::Unknown => 'Unknown',
|
||||
];
|
||||
|
||||
public static function name($value)
|
||||
{
|
||||
if (!isset(self::$valueToName[$value])) {
|
||||
throw new UnexpectedValueException(sprintf(
|
||||
'Enum %s has no name defined for value %s', __CLASS__, $value));
|
||||
}
|
||||
return self::$valueToName[$value];
|
||||
}
|
||||
|
||||
|
||||
public static function value($name)
|
||||
{
|
||||
$const = __CLASS__ . '::' . strtoupper($name);
|
||||
if (!defined($const)) {
|
||||
throw new UnexpectedValueException(sprintf(
|
||||
'Enum %s has no value defined for name %s', __CLASS__, $name));
|
||||
}
|
||||
return constant($const);
|
||||
}
|
||||
}
|
||||
|
65
php/Proto/UserClient.php
普通文件
65
php/Proto/UserClient.php
普通文件
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
// GENERATED CODE -- DO NOT EDIT!
|
||||
|
||||
namespace Proto;
|
||||
|
||||
/**
|
||||
* 定义 User 服务
|
||||
*/
|
||||
class UserClient extends \Grpc\BaseStub {
|
||||
|
||||
/**
|
||||
* @param string $hostname hostname
|
||||
* @param array $opts channel options
|
||||
* @param \Grpc\Channel $channel (optional) re-use channel object
|
||||
*/
|
||||
public function __construct($hostname, $opts, $channel = null) {
|
||||
parent::__construct($hostname, $opts, $channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录服务
|
||||
* @param \Proto\LoginRequest $argument input argument
|
||||
* @param array $metadata metadata
|
||||
* @param array $options call options
|
||||
* @return \Grpc\UnaryCall
|
||||
*/
|
||||
public function Login(\Proto\LoginRequest $argument,
|
||||
$metadata = [], $options = []) {
|
||||
return $this->_simpleRequest('/proto.User/Login',
|
||||
$argument,
|
||||
['\Proto\Response', 'decode'],
|
||||
$metadata, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册服务
|
||||
* @param \Proto\RegisterRequest $argument input argument
|
||||
* @param array $metadata metadata
|
||||
* @param array $options call options
|
||||
* @return \Grpc\UnaryCall
|
||||
*/
|
||||
public function Register(\Proto\RegisterRequest $argument,
|
||||
$metadata = [], $options = []) {
|
||||
return $this->_simpleRequest('/proto.User/Register',
|
||||
$argument,
|
||||
['\Proto\Response', 'decode'],
|
||||
$metadata, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息服务
|
||||
* @param \Google\Protobuf\GPBEmpty $argument input argument
|
||||
* @param array $metadata metadata
|
||||
* @param array $options call options
|
||||
* @return \Grpc\UnaryCall
|
||||
*/
|
||||
public function GetUserInfo(\Google\Protobuf\GPBEmpty $argument,
|
||||
$metadata = [], $options = []) {
|
||||
return $this->_simpleRequest('/proto.User/GetUserInfo',
|
||||
$argument,
|
||||
['\Proto\Response', 'decode'],
|
||||
$metadata, $options);
|
||||
}
|
||||
|
||||
}
|
11
php/composer.json
普通文件
11
php/composer.json
普通文件
@ -0,0 +1,11 @@
|
||||
{
|
||||
"require": {
|
||||
"grpc/grpc": "^1.57"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Proto\\": "Proto/",
|
||||
"GPBMetadata\\": "GPBMetadata/"
|
||||
}
|
||||
}
|
||||
}
|
63
php/composer.lock
自动生成的
普通文件
63
php/composer.lock
自动生成的
普通文件
@ -0,0 +1,63 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "76da866d4208ab54ede8590f127d0512",
|
||||
"packages": [
|
||||
{
|
||||
"name": "grpc/grpc",
|
||||
"version": "1.57.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/grpc/grpc-php.git",
|
||||
"reference": "b610c42022ed3a22f831439cb93802f2a4502fdf"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/grpc/grpc-php/zipball/b610c42022ed3a22f831439cb93802f2a4502fdf",
|
||||
"reference": "b610c42022ed3a22f831439cb93802f2a4502fdf",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"google/auth": "^v1.3.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-protobuf": "For better performance, install the protobuf C extension.",
|
||||
"google/protobuf": "To get started using grpc quickly, install the native protobuf library."
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Grpc\\": "src/lib/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"Apache-2.0"
|
||||
],
|
||||
"description": "gRPC library for PHP",
|
||||
"homepage": "https://grpc.io",
|
||||
"keywords": [
|
||||
"rpc"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/grpc/grpc-php/tree/v1.57.0"
|
||||
},
|
||||
"time": "2023-08-14T23:57:54+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
37
php/index.php
普通文件
37
php/index.php
普通文件
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
include "vendor/autoload.php";
|
||||
|
||||
function dump($response) {
|
||||
[$resp, $status] = $response;
|
||||
if($status->code == \Grpc\STATUS_OK) {
|
||||
$res = $resp->getInfo();
|
||||
if(isset($res["username"])){
|
||||
echo "username: ". $res["username"] ."\ntoken:" . $resp->getToken(). "\n";
|
||||
}else{
|
||||
echo $resp->getMsg() . "\n";
|
||||
}
|
||||
|
||||
}else{
|
||||
echo "error infomation:" . $status->details . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
$client = new \Proto\UserClient("127.0.0.1:1234", ["credentials" => \Grpc\ChannelCredentials::createInsecure()]);
|
||||
|
||||
$registerRequest = new \Proto\RegisterRequest();
|
||||
|
||||
$registerRequest->setUsername("test");
|
||||
$registerRequest->setPassword("test123");
|
||||
$registerRequest->setEmail("test@widuu.com");
|
||||
$registerRequest->setHobbies(["basketball", "football"]);
|
||||
|
||||
dump($client->Register($registerRequest)->wait());
|
||||
|
||||
|
||||
$loginRequest = new \Proto\LoginRequest();
|
||||
$loginRequest->setUsername("test");
|
||||
$loginRequest->setPassword("test123");
|
||||
|
||||
dump($client->Login($loginRequest)->wait());
|
||||
dump($client->GetUserInfo(new \Google\Protobuf\GPBEmpty())->wait());
|
正在加载...
在新工单中引用
屏蔽一个用户