嘿,我正在尝试使用 Go 和 Grpc 制作一个小型测试客户端,
opts := grpc.WithInsecure()
cc, err := grpc.Dial("localhost:9950", opts)
if err != nil {
log.Fatal(err)
}
Run Code Online (Sandbox Code Playgroud)
函数WithInsecure()调用给出警告:
grpc.WithInsecure 已弃用:使用 insecure.NewCredentials() 代替。
我不确定如何使用这个新函数调用,某处有示例吗?谢谢
我在 Go 中有一个现有项目,我正在使用协议缓冲区/gRPC。直到最近,该go_package选项还是可选的,并且生成的 Go 包名称将与 proto 包名称相同。
该文件位于项目根目录中。生成的代码文件 ( authenticator.pb.go) 位于同一位置。原型文件:
syntax = "proto3";
package authenticator;
service Authenticator {...}
Run Code Online (Sandbox Code Playgroud)
生成命令指定我要在同一目录中输出:
protoc --go_out=plugins=grpc:. authenticator.proto
Run Code Online (Sandbox Code Playgroud)
今天我拉取了新版本的协议缓冲区编译器和github.com/golang/protobuf/protoc-gen-go. 第一次运行时收到警告:
WARNING: Missing 'go_package' option in "authenticator.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
Run Code Online (Sandbox Code Playgroud)
建议的链接或多或少没用。但是教程更明确一点:
go_package 选项定义包的导入路径,该路径将包含此文件的所有生成代码。Go 包名称将是导入路径的最后一个路径组件。例如,我们的示例将使用包名称“tutorialpb”。
Run Code Online (Sandbox Code Playgroud)option go_package = "github.com/protocolbuffers/protobuf/examples/go/tutorialpb";
将此选项添加到 proto 文件并重新运行命令后,输出最终位于此路径中,相对于项目根目录。就像是:
$GOPATH/src/github.com/<org>/authenticator/github.com/<org>/authenticator/authenticator.pb.go
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下替代go_package名称:
.authenticator生成发生在正确的位置,但我得到了警告:
WARNING: …Run Code Online (Sandbox Code Playgroud) 我很难弄清楚protoc命令和 go 插件。
两者有什么不同:
protoc \
# Directory where you want the compiler to write your Go output.
--go_out=.
# vs ?
--go_opt=paths=source_relative
# vs ?
--go-grpc_out=.
# vs ?
--go-grpc_opt=paths=source_relative
Run Code Online (Sandbox Code Playgroud)
如果--go_opt生成
<name>.pb.go文件并--go-grpc_opt生成
<name>_grpc.pb.go文件为什么甚至有--go_out?
你能解释一下 protoc -文档没有说什么吗--go-grpc_opt?
并且甚至protoc -h不将 go 列为 OUT_DIR?
注意:我使用此文档安装
最近,grpc-go 引入了 mustEmbedUnimplemented*** 方法。它用于向前兼容。
简单来说,我无法理解它是如何提供帮助的,以及在没有它的情况下我们面临哪些问题?现在在我的结构中,我使用添加以下语句,但是,我不知道为什么...
type server struct {
pdfpb.UnimplementedGreetServiceServer
}
Run Code Online (Sandbox Code Playgroud)
在 Github 问题 - https://github.com/grpc/grpc-go/issues/3669 中,他们对此进行了辩论,有人可以简单地解释一下它是如何提供帮助的,以及在没有它的情况下我们面临哪些问题?
我正在gRPC使用服务器(使用protobuf)在 GO 中构建应用程序,并将其包装在 HTTPS 服务器中(使用gin)。只有 HTTPS 服务器被发布到客户端使用(我的意思是我的应用程序可以通过 REST API 访问,实际上然后在 gRPC 端点上拨号),我使用SwaggerOpenAPI3发布它(版本 3 是主要的此处要求)规范。gRPC 和 HTTPS 都是必需的,任何解决方案都应遵循此架构。
我不想在两个地方维护我的服务器规范,即我不想同时维护 proto 文件 ( .proto) 和 swagger 规范 ( .json/.yaml)。由于我绝对必须编写 proto 文件来生成 gRPC 服务器,因此我想自动生成 swagger 规范(OpenAPI3)。
我能够使用grpc-gateway库swagger从 protobuf 文件 ( .proto)生成OpenAPI2 规范,如下所示:grpc-rest-go-example。但我的要求是 OpenAPI3;更具体地说,我想使用OpenAPI3 中的功能并从proto 的功能映射到它。这在 OpenAPI2 中是不可能的,因为它不允许 API 具有多个类型定义的请求/响应主体,这是 OpenAPI3 中通过启用 oneOf、anyOf 和 allOf 构造添加的一项功能。oneOfoneof
在尝试这样做时,我偶然发现了 GoogleAPIs googleapis/ gnostic 的这个库,其描述是:
该存储库包含一个 …
问题:
我在 grpc 中接收消息时收到此错误:
rpc error: code = ResourceExhausted desc = grpc: received message larger than max (8653851 vs. 4194304)
我试过的:
我提供了增加要接收消息大小的选项,但它仍然出现相同的错误,这意味着此最大大小设置不起作用:
size := 1024 * 1024 * 12
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(size)))
conn, err := grpc.Dial(address, opts...)
Run Code Online (Sandbox Code Playgroud)
注释:
默认限制1024 * 1024 * 4 = 4194304显示在错误消息中。我原以为这个限制会增加到,1024 * 1024 * 12 = 12582912但显然没有。
我对 GRPC 比较陌生,希望确保我正在使用 golang 正确进行连接管理。我不想为每次调用都创建一个新连接,但我也不想在扩展时产生瓶颈。
我所做的是在 init 函数中创建一个连接:
var userConn *grpc.ClientConn
var userServiceName string
func init() {
userServiceName := os.Getenv("USER_SERVICE_URL")
if userServiceName == "" {
userServiceName = "localhost"
}
logging.LogDebug("userClient: Connecting to: "+userServiceName, "")
tempConn, err := grpc.Dial(userServiceName, grpc.WithInsecure())
if err != nil {
logging.LogEmergency("account_user_client.Init() Could not get the connection. "+err.Error(), "")
return
}
userConn = tempConn
}
Run Code Online (Sandbox Code Playgroud)
然后对于每个函数,我将使用该连接创建一个客户端:
c := user.NewUserClient(userConn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err …Run Code Online (Sandbox Code Playgroud) 我可以使用以下指令获取 Protobuf 枚举的字符串值:
str := testPB.Status_ENABLED.String()
Run Code Online (Sandbox Code Playgroud)
如何进行逆运算?(从字符串中获取枚举元素)。
TLDR:我正在寻找一种方法来更新每次调用的开放流上的标头,而stream.Send(msg)无需关闭流并打开一个新流。
概括
我有一个 GRPC 客户端和服务器来处理双向流。要向服务器进行身份验证,客户端必须在请求标头中发送 JWT,并将其设置为“授权”。令牌的有效期为 30 分钟。令牌过期后,服务器将终止连接。
我正在寻找一种方法来刷新客户端的授权令牌,并保持流打开。客户端应在循环中运行,每 30 分钟使用更新的令牌和更新的负载执行一个新请求。我还没有找到从客户端更新已打开流的标头的方法。
让我们看一些代码来了解客户端的样子。下面的代码有一个函数用于创建客户端的新实例,另一个函数用于建立与 GRPC 服务器的连接。
func NewWatchClient(config *Config, logger *logrus.Logger) (*WatchClient, error) {
cc, err := newConnection(config, logger)
if err != nil {
return nil, err
}
service := proto.NewWatchServiceClient(cc)
return &WatchClient{
config: config,
conn: cc,
logger: entry,
service: service,
}, nil
}
func newConnection(config *Config, logger *logrus.Logger) (*grpc.ClientConn, error) {
address := fmt.Sprintf("%s:%d", config.Host, config.Port)
// rpcCredential implements credentials.PerRPCCredentials
rpcCredential := newTokenAuth(config.Auth, config.TenantID)
return grpc.Dial(
address,
grpc.WithPerRPCCredentials(rpcCredential), …Run Code Online (Sandbox Code Playgroud) 与 protoc-gen-go 相同的症状:无法确定“simple.proto”的 Go 导入路径
\n\n\n对于具有以下内容的简单原型文件。
\nRun Code Online (Sandbox Code Playgroud)\nsyntax="proto3";\n\npackage main;\n\nmessage Person {\n string name = 1;\n int32 age = 2; \n}\n我正在尝试使用 protoc 为其生成 go 代码。我跑:
\nRun Code Online (Sandbox Code Playgroud)\nprotoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative simple.proto\n我收到以下错误:
\nRun Code Online (Sandbox Code Playgroud)\nprotoc-gen-go: unable to determine Go import path for "simple.proto"\n\nPlease specify either:\n \xe2\x80\xa2 a "go_package" option in the .proto source file, or\n \xe2\x80\xa2 a "M" argument on the command line.\n
那里的所有答案都集中在第一个选项 - 添加a "go_package" option in the …