golang下的grpc

facebook的thrift也是开源rpc库,性能高出grpc一倍以上,grpc发展的较晚,期待以后有长足的进步。简单来说thrift = grpc + protobuf

gRPC基于HTTP/2标准设计,带来诸如双向流控、头部压缩、单TCP连接上的多复用请求等特性。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

要求:

go version 版本最好1.7以上

protoc --version 版本最好3.1.0以上

go get -v google.golang.org/grpc

如果下载不了设置代理:(go get走的命令行代理,不是http代理!!!所以只开启shadowsocks是没用的)

windows:

set http_proxy=127.0.0.1:1080

set https_proxy=127.0.0.1:1080

 

linux:

export http_proxy=127.0.0.1:1080

export https_proxy=127.0.0.1:1080

 

proto文件

复制代码

syntax = "proto3";

 

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";

 
 

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;
}

 
复制代码

 

protoc   -I   helloworld/    helloworld/helloworld.proto   --go_out=plugins=grpc:helloworld
注意:grpc插件,否则service不能运行,代码如下:

 

测试demo:

cd $GOPATH/src/google.golang.org/grpc/examples/helloworld

运行:

复制代码
服务端代码:go run greeter_server/main.go


package main

import (
    "log"
    "net"

    "golang.org/x/net/context"
    "google.golang.org/grpc"
    pb "google.golang.org/grpc/examples/helloworld/helloworld"
    "google.golang.org/grpc/reflection"
)

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) {
    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)
    }
}
复制代码

 

复制代码
客户端代码:go run greeter_client/main.go


package main

import (
    "log"
    "os"

    "golang.org/x/net/context"
    "google.golang.org/grpc"
    pb "google.golang.org/grpc/examples/helloworld/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("Greeting: %s", r.Message)
}
复制代码

 

 

参考网站:

http://www.grpc.io/docs/quickstart/go.html         上面代码的详细解释1
http://www.jianshu.com/p/774b38306c30           上面代码的详细解释2
http://www.cnblogs.com/YaoDD/p/5504881.html
https://github.com/tenywen/share

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值