grpc-go

1,关于grpc-go

golang 可以可以做grpc的服务端和客户端。

官网的文档:

http://www.grpc.io/docs/quickstart/go.html

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

和之前写的java的grpc客户端调用相同。也需要使用protobuf的配置文件。

但是golang下面的类库非常的简单,而且golang的性能也很强悍呢。

有些简单的业务逻辑真的可以使用golang进行开发。

性能强悍而且,消耗的资源也很小。java感觉上已经非常的臃肿了。

项目已经上传到github上面了。

https://github.com/freewebsys/grpc-go-demo

2,生成代码

定义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

3,生成代码,服务端,客户端调用

cd src/helloworld

protoc -I ./ helloworld.proto –go_out=plugins=grpc:.

会生成一个go的helloworld.pb.go 文件。里面包括了grpc的远程调用和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

4,运行服务端&客户端代码

go run server/main.go

go run clinet/main.go

同时,可以使用java的客户端和服务端 <<===>> go的服务端客户端

相互调用。

grpc-go_第1张图片

5,总结

本文的原文连接是:http://blog.csdn.net/freewebsys/article/details/59483427未经博主允许不得转载。

博主地址是:http://blog.csdn.net/freewebsys

grpc 服务的远程调用还是非常的简单的。

但是这个只是一个helloworld ,真正要在企业内部使用的时候还需要一个注册中心。

管理所有的服务。初步计划使用 consul 存储数据。

因为consul 上面集成了非常多的好东西。还有个简单的可视化的界面。

比etcd功能多些。但是性能上面差一点。不过也非常强悍了。

企业内部使用的注册中心,已经足够了。

你可能感兴趣的:(grpc-go)