GRPC (3): Developing GRPC server and client with Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1, about Grpc-go

Golang can do GRPC server and client.
Documents on official Website:
Http://www.grpc.io/docs/quickstart/go.html
Https://github.com/grpc/grpc-go
Same as the GRPC client call that was written earlier in Java. You also need to use the Protobuf configuration file.
But Golang the following class library is very simple, and the performance of Golang is also very powerful.
Some simple business logic can really be developed using Golang.
The performance is tough and the resources consumed are small. Java feels very bloated.
The project has been uploaded to GitHub.
Https://github.com/freewebsys/grpc-go-demo

2, Generate code

Define the Proto file:

"proto3";package helloworld;// The greeting service definition.service Greeter {  // Sends a greeting  rpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest {  string1;}// The response message containing the greetingsmessage HelloReply {  string1;}

3, generate code, server side, client call

CD Src/helloworld
Protoc-i./HELLOWORLD.PROTO–GO_OUT=PLUGINS=GRPC:.
A go helloworld.pb.go file is generated. This includes the remote invocation of the GRPC and the serialization of the Protobuf.

Server.go

Package Mainimport ("Log"    "NET"    "Golang.org/x/net/context"    "Google.golang.org/grpc"Pb"Github.com/freewebsys/grpc-go-demo/src/helloworld"    "Google.golang.org/grpc/reflection"    "FMT") Const (Port =": 50051")//server is used to implement HelloWorld. Greeterserver. Type server struct{}//SayHello implements HelloWorld. GreeterserverFunc (S *server) SayHello (CTX context. Context,inch*pb. Hellorequest) (*PB. Helloreply, error) {FMT. Println("######### get client Request name:"+inch. Name) Return &PB. Helloreply{Message:"Hello"+inch. 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{})//Register reflection service on GRPC server. Reflection. Register(s) If err: = S. Serve(LIS); Err! = Nil {Log. Fatalf("Failed to serve:%v", err)}}

Client.go

Package Mainimport ("Log"    "OS"    "Golang.org/x/net/context"    "Google.golang.org/grpc"Pb"Github.com/freewebsys/grpc-go-demo/src/helloworld") Const (address ="localhost:50051"DefaultName ="World") Func Main () {//SetUp a connection to the server. Conn, err: = Grpc. Dial(Address, Grpc. Withinsecure()) If err! = Nil {log. Fatalf("did not connect:%v", err)} defer conn. Close() c: = PB. Newgreeterclient(conn)//Contact the server andPrint outIts response. Name: = DefaultName If Len (OS. Args) >1{name = OS. Args[1]} r, err: = C. SayHello(Context. Background(), &PB. Hellorequest{name:name}) If err! = Nil {log. Fatalf("Could not greet:%v", err)} log. Printf("####### Get Server Greeting response:%s"R. Message)}

4, run Server & client code

Go Run server/main.go

Go Run clinet/main.go

At the same time, you can use Java client and server side <<===>> Go Server client
Call each other.

5, summary

The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/59483427 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys

The remote invocation of the

Grpc service is still very simple.
But this is just a helloworld, the real need to use within the enterprise when a registry is needed. The
manages all services. Initially plan to use consul to store data.
Because consul integrates a lot of good stuff. There's also a simple visual interface. The
is more powerful than ETCD. But the performance is almost there. But it's also very tough.
The registry used within the enterprise is sufficient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.