栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Protobuf 介绍与实战27:rpc里如何定义无返回值 无参数方法?(grpc-go protobuf Empty 无返回值 无参数)

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Protobuf 介绍与实战27:rpc里如何定义无返回值 无参数方法?(grpc-go protobuf Empty 无返回值 无参数)

1  go grpc-go 相关技术专栏 总入口

2  Protobuf介绍与实战 图文专栏 文章目录

1、在proto文件里定义rpc方法时,如何实现无参数设置、无返回值设置呢?
1.1、第1步:导入指定文件

1.2、第2步:设置方法为无参数设置、无返回值设置

2、测试用例
2.1、proto样例文件
syntax = "proto3";

import "google/protobuf/empty.proto";

package proto;

message HelloRequest {
  string name = 1;
}
message HelloReply {
  string message = 1;
}

service Greeter {
  // 模拟1:没有返回值 情况
  rpc SayHello1 (HelloRequest) returns (google.protobuf.Empty) {}
  // 模拟2:没有参数 情况
  rpc SayHello2 (google.protobuf.Empty) returns (HelloReply) {}
  // 模拟3:没有参数,没有返回值 情况
  rpc SayHello3 (google.protobuf.Empty) returns (google.protobuf.Empty) {}
}

可以通过类似下面的命令,生成指定的文件:

protoc --go_out=. --go-grpc_out=. helloworld.proto
2.2、server代码样例
package main

import (
	"context"
	"fmt"
	"github.com/CodisLabs/codis/pkg/utils/log"
	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc"
	"grpc-go-study/grpc-other/test_empty/proto"
	"net"
)

const (
	port = ":50051"
)

type server struct {
	proto.UnimplementedGreeterServer
}

func (s *server) SayHello1(context.Context, *proto.HelloRequest) (*empty.Empty, error) {
	fmt.Println("--->this is SayHello1<---")
	return &empty.Empty{}, nil
}

func (s *server) SayHello2(context.Context, *empty.Empty) (*proto.HelloReply, error) {
	fmt.Println("--->this is SayHello2<---")
	return &proto.HelloReply{
		Message: "--->this is SayHello2<---",
	}, nil
}

func (s *server) SayHello3(context.Context, *empty.Empty) (*empty.Empty, error) {
	fmt.Println("--->this is SayHello3<---")
	return &empty.Empty{}, nil
}

func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Errorf("failed to listen: %v", err)
	}

	s := grpc.NewServer()

	proto.RegisterGreeterServer(s, &server{})
	if err := s.Serve(lis); err != nil {
		log.Errorf("failed to serve: %v", err)
	}
}

2.3、client代码样例
package main

import (
	"context"
	"github.com/CodisLabs/codis/pkg/utils/log"
	"github.com/golang/protobuf/ptypes/empty"
	"google.golang.org/grpc"
	"grpc-go-study/grpc-other/test_empty/proto"
)

const (
	address = "localhost:50051"

	defaultName = "---helloworld---"
)

func main() {
	conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Errorf("did not connect: %v", err)
		panic(err)
	}
	defer conn.Close()

	c := proto.NewGreeterClient(conn)
	ctx := context.Background()

	msgEmpty, err := c.SayHello1(ctx, &proto.HelloRequest{Name: defaultName})
	if err != nil {
		panic(err.Error())
	}
	log.Infof("SayHello1tresult:%s", msgEmpty.String())

	msgResp, err := c.SayHello2(ctx, &empty.Empty{})
	if err != nil {
		panic(err.Error())
	}
	log.Infof("SayHello2tresult:%s", msgResp.String())

	msgEmpty, err = c.SayHello3(ctx, &empty.Empty{})
	if err != nil {
		panic(err.Error())
	}
	log.Infof("SayHello3tresult:%s", msgEmpty.String())

}

下一篇文章

  Protobuf 类名、属性大小写是否有影响?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/903013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号