go开源项目学习--grpc

地址:https://github.com/grpc/grpc-go

一、安装

先尝试官方命令安装:
go get google.golang.org/grpc
结果报错:
package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc "(https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
原因是网络访问不了,只能参考网上通过github来安装的方式:

//grpc源码
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
//grpc安装需要的依赖
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
//protoc-gen-go
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto

接着安装grpc:

go install google.golang.org/grpc

我是在window下安装的,这时protoc命令还不能使用,在GOPATH/bin下只有protoc-gen-go.exe,去github下载:protoc_3.6.1-win32.zip
解压将bin下protoc.exe放到$GOPATH/bin下即可,在git bash 执行测试,有显示版本号结果即成功。

protoc --version

二、helloworld实例
protobuf
支持多语言的序列化工具,相比json和xml具有以下 优势:
(1)节省空间,序列化后体积小,利于网络传输
(2)序列化和反序列化速度比json和xml快
(3)多语言支持
(4)支持向后兼容和向前兼容
一般web服务对外使用json/xml格式,在系统内部使用protobuf,在各类微服务架构中可经常看到protobuf的使用。

proto文件
protobuf的协议接口描述语法,指明收发包的消息结构和函数,实例如下:

syntax = "proto3";
package helloworld;
service Greeter {
//客户端调用函数/服务端处理函数
//对客户端生成函数:SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)  
//对服务端生产接口:SayHello(context.Context, *HelloRequest) (*HelloReply, error)
rpc SayHello (HelloRequest) returns (HelloReply) {}
//请求消息结构
message HelloRequest {
  string name = 1;
}
//回复消息结构
message HelloReply {
  string message = 1;
}

用protoc来生成对应的go文件:

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

文件主要包含:
(1)请求/回复消息结构和其对应的序列化函数等
(2)为客户端/服务端生成对应函数的接口

服务端

package main
import (
	"context"
	"log"
	"net"
	
	"google.golang.org/grpc"
	pb "google.golang.org/grpc/examples/helloworld/helloworld"
	"google.golang.org/grpc/reflection"
)
const (
	port = ":50051"
)
// 服务端结构体,实现接口SayHello
type server struct{}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	log.Printf("Received: %v", in.Name)
	return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreeterServer(s, &server{})
	reflection.Register(s)
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}

客户端:

package main
import (
	"context"
	"log"
	"os"
	"time"
	"google.golang.org/grpc"
	pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
	address     = "localhost:50051"
	defaultName = "world"
)
func main() {
	conn, err := grpc.Dial(address, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewGreeterClient(conn)
	name := defaultName
	if len(os.Args) > 1 {
		name = os.Args[1]
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r.Message)
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值