GoLang GRPC使用

服务器端文件结构主要是 proto 文件夹(主要是.proto文件),service文件夹,mian.go文件

定义GRPC  ptoto文件   注意:

                                            1. 头   syntax="proto3";  必要

                                            2. 包package的定义

                                            3. service 的定义

                                            4. rpc方法的定义 

                                            5.message 的定义

                                            6. repeated参数的定义(即集合参数)

syntax="proto3";
// 写实例,这里先这样定义package
option go_package = ".;protoes";

// 服务名称 和 rpc 方法   注意 请求必须加参数
service UserService{
    rpc GetUserUerById(Uid) returns (User);
    rpc GetUserListByDeptId(DeptParam) returns (UserList);
}
service OtherService{
    rpc GetSumBySumParam(SumParam) returns (SumResult);
}

message Uid{
// 用户Id
int32 userid=1;
}
message User{
// 用户id
int32 userid=1;
// 用户名
string username=2;
}

// 请求参数
message DeptParam{
    // 部门Id
   string dptName=1;
}

// 响应集合 列表  应该repeated 的用法
message UserList{
repeated User userlist=1;
}

message SumParam{
  repeated int32 arrNum=1;
}

message SumResult{
int32 sum=1;
}

 

写好上面的文件可以在命令行 cd 到当前文件所在的文件夹 通过

protoc --go_out=plugins=grpc:. mygrpc.proto

命令来生成pb文件

 

 

服务方法代码见下, 我这里写了两个服务

 注意 方法要和.proto里定义的方法名及参数一致

package services

import (
    "context"
    proto "mygrpc/proto"
)

type UserService struct {

}

// 根据用户名得到用户
func (this *UserService) GetUserUerById(ctx context.Context,Uid *proto.Uid) (*proto.User,error){
    user :=&proto.User{}
    user.Userid=1
    user.Username="zs"
    return user,nil
}

// 根据用户名得到所有的用户集合
func (this *UserService) GetUserListByDeptId(ctx context.Context,u *proto.DeptParam) (*proto.UserList,error){
    Alllist := &proto.UserList{}
    var userlist []*proto.User
    user :=&proto.User{}
    user.Userid=1
    user.Username="zs"
    userlist=append(userlist,user)
    Alllist.Userlist=userlist
    return Alllist,nil
}
package services

import (
    "context"
    protoes "mygrpc/proto"
)

// 其实服务
type OtherService struct {

}

// 这里 写了个计算求和
func (this *OtherService)  GetSumBySumParam(ctx context.Context,param *protoes.SumParam) (*protoes.SumResult,error){
    result :=&protoes.SumResult{}
    for _,v :=range param.ArrNum{
        result.Sum +=v
    }
    return result,nil
}

 

main()方法通用TCP挂载服务向外提搞服务

package main

import (
    "fmt"
    "google.golang.org/grpc"
    pb "mygrpc/proto"
    "mygrpc/services"
    "net"
)

func main(){
    // 开始服务器端监听
    listen,err :=net.Listen("tcp","localhost:8090")
    fmt.Println("listen :8090")
    if err!=nil{
        fmt.Println("listen err:",err)
        return
    }
    // 建立grpc服务实体
    grpcServer:=grpc.NewServer()
    // 注册 Grpc 相关服务   我这里写了两个服务
    pb.RegisterUserServiceServer(grpcServer,&services.UserService{})
    pb.RegisterOtherServiceServer(grpcServer,&services.OtherService{})
    // 将服务挂载到服务监听
    grpcServer.Serve(listen)
}

客户端代码   主要是引入服务端的pb.go文件

 

 

 然后在main()里面调用

package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"

    protoes "grpcclient/protoes"
)

func main(){
    fmt.Println("客户端开始……")
    conn,err:=grpc.Dial(":8090",grpc.WithInsecure())
    if err!=nil{
        fmt.Println("creeate conn err:",err)
        return
    }
    defer conn.Close()
    // 建立User客户端
    userClient:=protoes.NewUserServiceClient(conn)
    // 调用  UserService  服务里面的  GetUserListByDeptId 方法
    uerlist,err:= userClient.GetUserListByDeptId(context.Background(),&protoes.DeptParam{DptName: "1"})
    if err!=nil{
        fmt.Println("调用Grpc服务GetUserListByDeptId出错:",err)
    }
    fmt.Println(uerlist)  // 打印 GetUserListByDeptId 结果
    param:= make([]int32,3)
    param[0]=1
    param[1]=1
    param[2]=1
    sumParam:= &protoes.SumParam{ArrNum:param}
    // 建立Other服务客户端
    otherClient :=protoes.NewOtherServiceClient(conn)
    // 调用GetAaddB  求合方法
    result,err:= otherClient.GetSumBySumParam(context.Background(),sumParam)
    if err!=nil{
        fmt.Println("调用Grpc服务GetAaddB出错:",err)
    }
    fmt.Println(result)
}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章