grpc(3):使用 golang 開發 grpc 服務端和用戶端

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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";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 greetingsmessage HelloReply {  string message = 1;}

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

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() {    // 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,運行服務端&用戶端代碼

go run server/main.go

go run clinet/main.go

同時,可以使用java的用戶端和服務端 <<===>> go的服務端用戶端
相互調用。

5,總結

本文的原文串連是: http://blog.csdn.net/freewebsys/article/details/59483427 未經博主允許不得轉載。
博主地址是:http://blog.csdn.net/freewebsys

grpc 服務的遠程調用還是非常的簡單的。
但是這個只是一個helloworld ,真正要在企業內部使用的時候還需要一個註冊中心。
管理所有的服務。初步計劃使用 consul 儲存資料。
因為consul 上面整合了非常多的好東西。還有個簡單的可視化的介面。
比etcd功能多些。但是效能上面差一點。不過也非常強悍了。
企業內部使用的註冊中心,已經足夠了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.