服務器端文件結構主要是 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)
}