RPC(四)[gRPC-Golang-环境搭建]

在这里插入图片描述
gRPC官方首页:

https://grpc.io/

gRPC官方简介:

https://grpc.io/docs/guides/

gRPC-Golang开发教程:

https://grpc.io/docs/tutorials/basic/go/

1.下载

go get -u -v google.golang.org/grpc/...

因为在go的环境变量中配置了go mod,所以要注意,且在此处下载必须是带【/…】,否则容易报莫名的异常!

2.测试

1.服务端启动

1.找到下载的grpc所在的目录【使用go mod的环境变量一般在$GOPATH/pkg下面】

cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_server && ls

在这里插入图片描述
2.启动server服务

cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_server &&  go run main.go

在这里插入图片描述

2.客户端启动

另开一个终端

1.找到下载的grpc所在的目录【使用go mod的环境变量一般在$GOPATH/pkg下面】

cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_client && ls

在这里插入图片描述
2.启动client服务

cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworld/*_client &&  go run main.go

在这里插入图片描述
环境搭建成功!!!

3.客户端代码浅析

/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Package main implements a client for Greeter service.
package main

import (
        "context"
        "log"
        "os"
        "time"

        "google.golang.org/grpc"
        pb "google.golang.org/grpc/examples/helloworld/helloworld" // 引用编译好的protobuf文件
)

const (
        address     = "localhost:50051"
        defaultName = "world"
)

func main() {
        // 建立与服务器的连接
        conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
        if err != nil {
                log.Fatalf("did not connect: %v", err)
        }
        // 延迟关闭连接
        defer conn.Close()
        // 调用protobuf的函数创建客户端连接句柄
        c := pb.NewGreeterClient(conn)

        // 与服务器联系并打印其响应。
        name := defaultName
        if len(os.Args) > 1 {
                name = os.Args[1]
        }
        // context的超时设置
        ctx, cancel := context.WithTimeout(context.Background(), time.Second)
        defer cancel()
        // 调用protobuf的SayHello函数
        r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
        if err != nil {
                log.Fatalf("could not greet: %v", err)
        }
        // 打印结果
        log.Printf("Greeting: %s", r.GetMessage())
}

4.服务端代码浅析

/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

//go:generate protoc -I ../helloworld --go_out=plugins=grpc:../helloworld ../helloworld/helloworld.proto

// Package main implements a server for Greeter service.
package main

import (
        "context"
        "log"
        "net"

        "google.golang.org/grpc"
        pb "google.golang.org/grpc/examples/helloworld/helloworld" // 引用编译好的protobuf文件
)

const (
        port = ":50051"
)

// 服务器用于实现helloworld.GreeterServer。
type server struct {
        pb.UnimplementedGreeterServer
}

// SayHello实现helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
        log.Printf("Received: %v", in.GetName())
        return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func main() {
		// 创建监听
        lis, err := net.Listen("tcp", port)
        if err != nil {
                log.Fatalf("failed to listen: %v", err)
        }
        // new服务对象
        s := grpc.NewServer()
        // 注册服务
        pb.RegisterGreeterServer(s, &server{})
        if err := s.Serve(lis); err != nil {
                log.Fatalf("failed to serve: %v", err)
        }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值