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功能多些。

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值