golang grpc

依赖安装

#下载 protoBuf: 
#编译完成的下载地址:https://github.com/protocolbuffers/protobuf/release
# linux 或 mac 安装

$ git clone https://github.com/protocolbuffers/protobuf.git
#protobuf的语法 
#要想使用 protobuf必须得先定义 proto文件。所以得先熟悉 protobuf的消息定义的相关语法。 定义一个消息类型 
#或者直接将压缩包拖入后解压 
$ unzip protobuf.zip 

#安装依赖库 
$ sudo apt-get install autoconf  automake  libtool  curl  make  g++  unzip libffidev -y 
#安装 
$ cd protobuf/ 
$ ./autogen.sh 
$ ./configure 
$ make 
$ sudo make install 
$ sudo ldconfig   

# 刷新共享库 很重要的一步啊 #安装的时候会比较卡 #成功后需要使用命令测试 

$ protoc –h  

#win安装的
# 直接在这下载windows所需的zip包 解压后将exe文件放到环境变量中即可  https://github.com/protocolbuffers/protobuf/releases

 
 #安装protoc-gen-go插件
 #安装 (我的是go1.13版本的 用的是goproxy.io代理)
 $ go get -v -u github.com/golang/protobuf/protoc-gen-go 
 #编译 
 $ cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go/ 
 $ go build 
 #将生成的 protoc-gen-go可执行文件,放在/bin目录下 
 $ sudo cp protoc-gen-go /bin/

 

1 . proto 文件

syntax = "proto3" ;

package myproto ;


//定义服务

service Helloserver{
    //一个打招呼的函数
    rpc Sayhello(HelloReq)returns(HelloRsp){}
    //一个说名字的服务
    rpc Sayname(NameReq)returns(NameRsp){}
}

//客户端发送给服务端
message  HelloReq{
    string name = 1 ;
}

//服务端返回给客户端
message HelloRsp{
    string msg =1 ;
}

//客户端发送给服务端
message NameReq{
    string name = 1 ;

}

//服务端返回给客户端
message NameRsp{
    string msg =1 ;
}

执行以下命令生成go依赖文件

protoc --go_out=./ *.proto #不加grpc插件 
protoc --go_out=plugins=grpc:./ *.proto #添加grpc插件 

2 . server 代码

package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	pd "grpcTest/myproto"
	"net"
)

type server struct {}

//一个打招呼的函数

//rpc
//函数关键字(对象)函数名(客户端发送过来的内容 , 返回给客户端的内容) 错误返回值

//grpc
//函数关键字 (对象)函数名 (cotext,客户端发过来的参数 )(发送给客户端的参数,错误)
func (this *server)Sayhello(ctx context.Context, in *pd.HelloReq) (out * pd.HelloRsp, err error){
	fmt.Println(in.Name)
	return  &pd.HelloRsp{Msg:"hello"+in.Name},nil
}
//一个说名字的服务
func (this *server)Sayname(ctx context.Context, in *pd.NameReq) ( out *pd.NameRsp, err error){
	fmt.Println(in.Name)
	return &pd.NameRsp{Msg:in.Name+"早上好"},nil
}

func main() {
	//创建网络
	ln ,err :=net.Listen("tcp",":10086")
	if err !=nil{
		fmt.Println("网络错误",err)
	}

	//创建grpc的服务
	srv:=grpc.NewServer()

	//注册服务
	pd.RegisterHelloserverServer(srv,&server{})



	//等待网络连接
	err=srv.Serve(ln)
	if err!=nil {
		fmt.Println("网络错误",err)
	}

}

3 . client 代码

package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	pd "grpcTest/myproto"
)

func main() {
	//客户端连接服务器
	conn ,err :=grpc.Dial("127.0.0.1:10086",grpc.WithInsecure())
	if err!=nil {
		fmt.Println("网络异常",err)
	}
	//网络延迟关闭
	defer  conn.Close()


	//获得grpc句柄
	c:=pd.NewHelloserverClient(conn)

	//通过句柄调用函数
	re ,err :=c.Sayhello(context.Background(),&pd.HelloReq{Name:"熊猫"})
	if err!=nil {
		fmt.Println("sayhello 服务调用失败")
	}
	fmt.Println("调用sayhello的返回",re.Msg)



	re1 ,err :=c.Sayname(context.Background(),&pd.NameReq{Name:"托尼斯塔克"})
	if err !=nil{
		fmt.Println("say name 调用失败")
	}
	fmt.Println("调用Sayname的返回",re1.Msg)

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值