4

我正在研究DgraphGo集成。我正在尝试访问Dgraph查询,Go为此我正在使用github.com/dgraph-io/dgo库。

这是代码:

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))
}

我收到错误rpc error: code = Unavailable desc = connection closed当我试图运行代码时。由于我是新手,Go而且DGraph Database我的知识非常有限。

任何人都可以帮助解决此错误需要进行哪些确切的更改。

4

2 回答 2

3

因为我遇到了和你一样的样子,经过一个小时的工作,我发现原因是我使用了代理export https_proxy=http://192.168.3.92:7890 http_proxy=http://192.168.3.92:7890 all_proxy=socks5://192.168.3.92:7890,当我执行unset https_proxy http_proxy all_proxy时,它成功了!

于 2021-08-06T12:01:59.473 回答
1

8000 端口的服务只是 UI。如果你想使用 gRPC,你必须在端口 9080 中调用它。永远不要使用 8080、8000、6080 等等。检查此文档https://dgraph.io/docs/deploy/ports-usage/#types-of-ports

在这个例子中https://github.com/dgraph-io/dgo/blob/a38d5eaacbf8667cc2d6e7b40bd0978cede4000f/examples_test.go#L35

它正在使用端口 9180。因为用于测试的集群的偏移量为 100。这意味着 Alpha 中的所有端口都增加了 100 个整数。因此,Alpha 中的所有端口都必须考虑偏移量。其余 API 为 8180,gRPC 为 9180。

在通常情况下,您在学习时永远不会考虑使用偏移量。所以你可能启动了一个普通的集群。默认端口是 9080。

于 2020-08-26T02:42:59.713 回答