zoukankan      html  css  js  c++  java
  • grpc(3):使用 golang 开发 grpc 服务端和client

    1,关于grpc-go


    golang 能够能够做grpc的服务端和client。


    官网的文档:
    http://www.grpc.io/docs/quickstart/go.html
    https://github.com/grpc/grpc-go
    和之前写的java的grpcclient调用同样。也须要使用protobuf的配置文件。
    可是golang以下的类库很的简单。并且golang的性能也很强悍呢。
    有些简单的业务逻辑真的能够使用golang进行开发。
    性能强悍并且。消耗的资源也很小。

    java感觉上已经很的臃肿了。
    项目已经上传到github上面了。
    https://github.com/freewebsys/grpc-go-demo

    2,生成代码


    定义proto文件:

    syntax = "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 {
      string name = 1;
    }
    
    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }

    3,生成代码,服务端,client调用

    cd src/helloworld
    protoc -I ./ helloworld.proto –go_out=plugins=grpc:.
    会生成一个go的helloworld.pb.go 文件。里面包含了grpc的远程调用和protobuf的序列化。

    server.go

    package main
    
    import (
        "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.GreeterServer
    func (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)
        }
    }

    client.go

    package main
    
    import (
        "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() {
        // Set up 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 and print out its 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,执行服务端&client代码


    go run server/main.go

    go run clinet/main.go

    同一时候,能够使用java的client和服务端 <<===>> go的服务端client
    相互调用。

    5,总结


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

    grpc 服务的远程调用还是很的简单的。
    可是这个仅仅是一个helloworld ,真正要在企业内部使用的时候还须要一个注冊中心。
    管理全部的服务。

    初步计划使用 consul 存储数据。


    由于consul 上面集成了许多的好东西。还有个简单的可视化的界面。


    比etcd功能多些。

    可是性能上面差一点。只是也很强悍了。
    企业内部使用的注冊中心。已经足够了。

  • 相关阅读:
    04-树7 二叉搜索树的操作集
    04-树6 Complete Binary Search Tree
    04-树5 Root of AVL Tree
    04-树4 是否同一棵二叉搜索树
    05-树8 File Transfer
    05-树7 堆中的路径
    二叉树的非递归遍历(先序、中序、后序和层序遍历)
    队列的定义与操作——顺序存储和链式存储
    Maven项目的核心pom.xml解释(转)
    eclipse安装插件的三种方式
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8719049.html
Copyright © 2011-2022 走看看