我正在使用使用 HTTPS 和自签名证书托管的 GRPC 服务。当我使用如下语法进行连接时:
const client = new productService('https://grpc-server-xxx.com:9090',
grpc.credentials.createInsecure())
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误
{ Error: 14 UNAVAILABLE: DNS resolution failed
at Object.exports.createStatusError (C:\grpc\node_modules\grpc\src\common.js:91:15)
at Object.onReceiveStatus (C:\grpc\node_modules\grpc\src\client_interceptors.js:1209:28)
at InterceptingListener._callNext (C:\grpc\node_modules\grpc\src\client_interceptors.js:568:42)
at InterceptingListener.onReceiveStatus (C:\grpc\node_modules\grpc\src\client_interceptors.js:618:8) at callback (C:\grpc\node_modules\grpc\src\client_interceptors.js:847:24)code: 14,metadata: Metadata { _internal_repr: {}, flags: 0 },details: 'DNS resolution failed'
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我如何解决这个问题。
注意:我的操作系统是 Windows 10,nodejs -- v10.16.0,GRPC
我正在将现有的 http 应用程序迁移到基于 GRPC 的应用程序。一段时间以来,grpc 和 http 都将在同一个 go 文件中的不同端口上运行,我将把现有的 http 服务器部分移动到 goroutine 中。这就是它的样子。
main() {
//Move existing server into a goroutine
go func() {
s.server, err = chttp.NewHTTPSServer("443", s.certPath, s.keyPath, s.Router, s.options)
if err != nil {
log.Fatalf("server: error creating HTTP server - %v\n", err)
}
if err := s.server.Serve(); err != http.ErrServerClosed {
log.Fatalf("server: run error - %v\n", err)
}
}
// Add new grpc server
lis, err := net.Listen("tcp", ":8433")
if err != nil {
log.Fatalf("failed to …Run Code Online (Sandbox Code Playgroud) 我正在尝试为一些 Protocol Buffers 以及 gRPC 服务生成 Go 代码。
过去我使用过https://github.com/golang/protobuf一个生成命令,看起来像这样:
//go:generate protoc --proto_path=. --go-out=. --go_opt=paths=source_relative <file>.proto
//go:generate protoc --proto_path=. --go_grpc_out=. --go_grpc_opt=paths=source_relative <file>.proto
Run Code Online (Sandbox Code Playgroud)
这种方法效果很好,但该存储库已被google.golang.org/protobuf和取代google.golang.org/grpc。
据我了解,这是有意拆分的,目的是将 protobuf 和 gRPC 项目发布周期分开。
使用新的存储库,我安装了如下工具:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
Run Code Online (Sandbox Code Playgroud)
这两个工具均已安装,并且在路径上可见。我遇到的问题是protoc或者protoc-gen-go正在寻找旧的工具 - protoc-gen-go_grpc。注意下划线。
我已经阅读了我能找到的所有文档和 Github 问题,但尚未弄清楚我应该如何使用新的protoc-gen-go-grpc. 注意破折号。
$ echo $GOPATH
/Users/<username>/dev/go
$ echo $GOBIN
/Users/<username>/dev/go/bin
$ which protoc
/opt/homebrew/bin/protoc
$ which protoc-gen-go
/Users/<username>/dev/go/bin/protoc-gen-go
$ which protoc-gen-go_grpc
protoc-gen-go_grpc not found
$ which protoc-gen-go-grpc
/Users/<username>/dev/go/bin/protoc-gen-go-grpc
Run Code Online (Sandbox Code Playgroud) 我的 grpc 服务器有一个日志拦截器,并且想要向元数据添加一个值(我想在整个生命周期中跟踪请求):
func (m *middleware) loggingInterceptor(srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler)
md, ok := metadata.FromIncomingContext(ss.Context())
if !ok {
return errors.New("could not get metadata from incoming stream context")
}
// add the transaction id to the metadata so that business logic can track it
md.Append("trans-id", "some-transaction-id")
// call the handler func
return handler(srv, ss)
}
Run Code Online (Sandbox Code Playgroud)
但文档指出FromIncomingContext:
// FromIncomingContext returns the incoming metadata in ctx if it exists. The
// returned MD should not be modified. Writing …Run Code Online (Sandbox Code Playgroud) 我正在使用 gRPC 服务生成 swagger json 文件protoc-gen-swagger。输出 json 是使用空响应示例生成的,我想将响应示例添加到定义中,以便它自动填充到生成的 json 中。
这是我目前的定义。
service UserService {
rpc GetUser (GetUserRequest) returns (UserResponse){
option (google.api.http) = {
get: "/api/v1/user/{username}"
response_body: "*"
};
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
description: "Returns user object";
operation_id: "get_user";
summary: "Get User";
};
}
}
message GetUserRequest {
string username = 1;
}
message UserResponse {
User user = 1;
}
message User {
string first_name = 1;
string last_name = 2;
string username = 3;
}
Run Code Online (Sandbox Code Playgroud)
当我使用命令生成 swagger …
我可能会以错误的方式处理它,但我想定义两个或多个结构(消息)之间的关系。
以 StackOverflow 为例,假设我LabelService在标签上有一个for CRUD 操作。我也有QuestionService一个Question可以有的地方Labels。我们还假设我有 aUserService和 aUser也可以附上标签
# label.proto
service LabelService {
rpc CreateLabel() returns();
...etc
}
message Label {
string text = 1;
}
Run Code Online (Sandbox Code Playgroud)
但现在我想创建我的QuestionService和Question消息。我是如何将这两个文件关联起来的,还是在 go 代码中完成了这种关联级别?
# question.proto
service QuestionService {
rpc CreateQuestion() returns();
...etc
}
message Question {
string text = 1;
repeat Label labels = 2 # <-- how to do this?
}
# user.proto
service UserService {
rpc …Run Code Online (Sandbox Code Playgroud) 我从 Google Cloud Platform 的微服务演示开始。我很好奇当服务部署在容器中时 gRPC 存根如何工作。
据我了解,特定服务的容器是通过 YAML 配置文件中指定的服务 IP 来寻址的。那么服务的 gRPC 服务器必须监听该服务 IP 吗?但我遇到了以下代码片段:
l, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
log.Fatal(err)
}
Run Code Online (Sandbox Code Playgroud)
我想知道服务器如何监听没有IP的地址?
我有一个 gRPC 服务器,并且我已经实现了 gRPC 服务器的正常关闭,如下所示
fun main() {
//Some code
term := make(chan os.Signal)
go func() {
if err := grpcServer.Serve(lis); err != nil {
term <- syscall.SIGINT
}
}()
signal.Notify(term, syscall.SIGTERM, syscall.SIGINT)
<-term
server.GracefulStop()
closeDbConnections()
}
Run Code Online (Sandbox Code Playgroud)
这很好用。如果我grpcServer.Serve()在主 goroutine 中编写逻辑,并将关闭处理程序逻辑放入另一个 goroutine 中,则之后的语句server.GracefulStop()通常不会执行。一些 DbConnections 如果closeDbConnections()执行的话,会被关闭。
server.GracefulStop()是一个阻塞调用。绝对在完成grpcServer.Serve()之前完成server.GracefulStop()。那么,在这个调用返回后,main goroutine 需要多长时间才能停止呢?
有问题的代码
func main() {
term := make(chan os.Signal)
go func() {
signal.Notify(term, syscall.SIGTERM, syscall.SIGINT)
<-term
server.GracefulStop()
closeDbConnections()
}()
if err := grpcServer.Serve(lis); …Run Code Online (Sandbox Code Playgroud) 我是一名新的 Go 开发人员,我正在尝试构建一个同时具有 gRPC 服务器和 Gin HTTP 服务器的项目。这就是我的代码大致的样子:
package main
import (
"log"
"net"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
)
func main() {
startGRPCServer()
startHTTPServer()
}
func startGRPCServer() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
log.Fatalf("could not attach listener to port: %v", err)
}
s := grpc.NewServer()
if err := s.Serve(listener); err != nil {
log.Fatalf("could not start grpc server: %v", err)
}
}
func startHTTPServer() {
g := gin.Default()
if err := g.Run(":8000"); err != nil {
log.Fatalf("could …Run Code Online (Sandbox Code Playgroud)