加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

grpc(3):使用 golang 开发 grpc 服务端和客户端

发布时间:2020-12-16 18:18:30 所属栏目:大数据 来源:网络整理
导读:1,关于grpc-go golang 可以可以做grpc的服务端和客户端。 官网的文档: http://www.grpc.io/docs/quickstart/go.html https://github.com/grpc/grpc-go 和之前写的java的grpc客户端调用相同。也需要使用protobuf的配置文件。 但是golang下面的类库非常的简

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 greetings
message 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 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,运行服务端&客户端代码


go run server/main.go

go run clinet/main.go

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

5,总结


本文的原文连接是: http://www.52php.cn/article/p-fztnfiac-bew.html 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

grpc 服务的远程调用还是非常的简单的。 但是这个只是一个helloworld ,真正要在企业内部使用的时候还需要一个注册中心。 管理所有的服务。初步计划使用 consul 存储数据。 因为consul 上面集成了非常多的好东西。还有个简单的可视化的界面。 比etcd功能多些。但是性能上面差一点。不过也非常强悍了。 企业内部使用的注册中心,已经足够了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读