1锋华,關(guān)于grpc-go
golang 可以可以做grpc的服務端和客戶端。
官網(wǎng)的文檔:
http://www.grpc.io/docs/quickstart/go.html
https://github.com/grpc/grpc-go
和之前寫的java的grpc客戶端調(diào)用相同庇麦。也需要使用protobuf的配置文件。
但是golang下面的類庫非常的簡單,而且golang的性能也很強悍呢尤蒿。
有些簡單的業(yè)務邏輯真的可以使用golang進行開發(fā)粗合。
性能強悍而且萍嬉,消耗的資源也很小。java感覺上已經(jīng)非常的臃腫了。
項目已經(jīng)上傳到github上面了。
https://github.com/freewebsys/grpc-go-demo
定義proto文件:
syntax ="proto3";packagehelloworld;// The greeting service definition.service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest {stringname =1;}// The response message containing the greetingsmessage HelloReply {stringmessage =1;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd src/helloworld
protoc -I ./ helloworld.proto –go_out=plugins=grpc:.
會生成一個go的helloworld.pb.go 文件。里面包括了grpc的遠程調(diào)用和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,in*pb.HelloRequest) (*pb.HelloReply, error) {? ? fmt.Println("######### get client request name :"+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{})? ? // Register reflection service on gRPC server.? ? reflection.Register(s)? ? if err := s.Serve(lis); err != nil {log.Fatalf("failed to serve: %v", err)? ? }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 serverandprintoutits 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)}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
go run server/main.go
go run clinet/main.go
同時,可以使用java的客戶端和服務端 <<===>> go的服務端客戶端
相互調(diào)用哗魂。
本文的原文連接是:http://blog.csdn.net/freewebsys/article/details/59483427未經(jīng)博主允許不得轉(zhuǎn)載。
博主地址是:http://blog.csdn.net/freewebsys
grpc 服務的遠程調(diào)用還是非常的簡單的啡彬。
但是這個只是一個helloworld 羹与,真正要在企業(yè)內(nèi)部使用的時候還需要一個注冊中心。
管理所有的服務庶灿。初步計劃使用 consul 存儲數(shù)據(jù)纵搁。
因為consul 上面集成了非常多的好東西。還有個簡單的可視化的界面往踢。
比etcd功能多些腾誉。但是性能上面差一點。不過也非常強悍了峻呕。
企業(yè)內(nèi)部使用的注冊中心利职,已經(jīng)足夠了。