问题
I am working on Dgraph
and Go
integration.I am trying to access Dgraph
query in Go
and for that I am using github.com/dgraph-io/dgo
library.
Here is the code :
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
)
func main() {
query := `{
people(func: has(name)) {
name
follows{
name
}
}
}`
conn, err := grpc.Dial("x.x.x.x:8000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
txn := dgraphClient.NewTxn()
txn.Query(ctx, query)
request := &api.Request{
Query: query,
}
response, err := txn.Do(ctx, request)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(response.Json))
}
I am getting error rpc error: code = Unavailable desc = connection closed when I an trying to run code.
As I am new to the Go
and DGraph Database
I have very limited knowledge.
Can anyone please help what exact changes need to be done to fix this error.
回答1:
The service at the port 8000 is the UI only. If you wanna use gRPC you have to call it in the port 9080. Never 8080, 8000, 6080, and so on. Check this docs https://dgraph.io/docs/deploy/ports-usage/#types-of-ports
In this example https://github.com/dgraph-io/dgo/blob/a38d5eaacbf8667cc2d6e7b40bd0978cede4000f/examples_test.go#L35
It is using the port 9180. Cuz the Cluster for testing has an offset of 100. That means that all ports in the Alpha increases 100 ints. So, all ports in Alpha have to take into account the offset. The rest API is 8180 and the gRPC is 9180.
In the usual cases, you would never think about using offset when you are learning things. So you probably started a normal cluster. And the default port is 9080.
来源:https://stackoverflow.com/questions/63372312/rpc-error-code-unavailable-desc-connection-closed-in-go-code