This is a creation in Article, where the information may have evolved or changed.
GRPC is a high-performance, general-purpose, open-source RPC framework developed by Google primarily for mobile applications and based on the HTTP/2 protocol standards, developed based on the PROTOBUF (Protocol buffers) serialization protocol, and supports many development languages. GRPC provides an easy way to precisely define services and automatically generate a highly reliable client function library for iOS, Android, and background support services. Clients take advantage of advanced streaming and linking capabilities to help conserve bandwidth, reduce the number of TCP links, conserve CPU usage, and battery life.
1, General account installation Protobuf
unzip protobuf-cpp-3.0.0-alpha-3.zipcd protobuf-3.0.0-alpha-3/.&&-u github.com/golang/protobuf/protoc-gen-go#golang 插件
2. Define Grpc.proto File
"proto3"; //protobuf3协议package inf;//请求message UserRq { int32 id = 1;}//响应message UserRp { string name = 1;}//服务service Data { rpc GetUser(UserRq) returns (UserRp);}
and then compile
cd ~/src/infprotoc --go_out=plugins=grpc:. grpc.proto
3. The package referenced by get
go get -u google.golang.org/grpc
Due to the wall, some of our dependent package files can be downloaded in the following ways:
On GitHub you can find the source code, download and copy to the corresponding directory:
GOOGLE.GOLANG.ORG/GRPC corresponding code address in: Https://github.com/grpc/grpc-go
Google.golang.org/cloud/compute/metadata corresponding code address in: Https://github.com/GoogleCloudPlatform/gcloud-golang
Golang.org/x/oauth2 corresponding code address in: Https://github.com/golang/oauth2
Golang.org/x/net/context corresponding code address in: Https://github.com/golang/net
The source code for these packages can also be downloaded via http://gopm.io/or http://golangtc.com/download/package.
4. Service-Side code
GRPC Project Main. GoPackage Mainimport ("INF" "Log" "NET" "Runtime" "StrConv" "Golang.org/x/net/context" "Google.golang.org/grpc") Const (Port ="41005") Type Data Struct{}func main () {runtime. Gomaxprocs(Runtime. Numcpu())//From service lis, err: = Net. Listen("TCP",":"+port) If err! = Nil {log. Fatalf("Failed to listen:%v", err)} s: = Grpc. NewServer() INF. Registerdataserver(s, &data{}) s. Serve(LIS) Log. Println("GRPC Server in:%s", port)}//definition method func (T *data) GetUser (CTX context. Context, Request *inf. Userrq) (Response *inf. USERRP, err error) {response = &inf. USERRP{Name:strconv. Itoa(Int (Request. Id)) +": Test",} return response, err}
5. Client code
Package Mainimport ("INF" "Log" "Runtime" "StrConv" "Strings" "Sync" "Time" "Math/rand" "Golang.org/x/net/context" "Google.golang.org/grpc") VAR (wg sync. Waitgroup) Const (Networktype ="TCP"Server ="127.0.0.1"Port ="41005"Parallel = -Connection parallelism times =100000Number of requests per connection) Func main () {runtime. Gomaxprocs(Runtime. Numcpu()) Currtime: = Time. now()//parallel request for I: =0; i < int (parallel); i++ {Wg. ADD(1) go func () {defer WG. Done() exe ()} ()} WG. Wait() Log. Printf("Time Taken:%.2f", Time. now(). Sub(Currtime). Seconds())}func exe () {//Establish connection conn, _: = Grpc. Dial(Server +":"+ port) Defer conn. Close() Client: = INF. Newdataclient(conn) for I: =0; i < int (times); i++ {GetUser (client)}}func getUser (client inf. Dataclient) {var request inf. Userrqr: = Rand. INTN(parallel) Request. Id= Int32 (R) Response, _: = Client. GetUser(Context. Background(), &request)//Call the remote method//To determine if the returned result is correct if ID, _: = StrConv. Atoi(Strings. Split(Response. Name,":")[0]); ID! = r {Log. Printf("Response error% #v", Response)}}
Reference
Http://www.infoq.com/cn/news/2015/03/grpc-google-http2-protobuf
Https://github.com/google/protobuf
Https://github.com/golang/protobuf
http://studygolang.com/articles/3192