Java使用自定义凭证通过Gremlin连接Cosmos DB失败
Java通过Gremlin连接Cosmos DB失败排查
尝试用Gremlin和Maven在Java中连接需用户名密码验证的Cosmos DB,传入endpoint、用户名、密码及端口创建Cluster,再通过Cluster启动Client并创建根遍历查询数据库,但查询返回网络错误,集群可用主机数为0,怀疑问题出在Cluster创建环节。
已尝试用连接字符串替代单独传入凭证信息,也测试过Python脚本,均连接失败。
连接代码
private static GraphTraversalSource connect(String endpoint, String username, String password, int port) { System.out.println("STARTING"); System.out.println("creating cluster"); Cluster cluster = Cluster.build() .addContactPoint(endpoint) .port(port) .credentials(username, password) .enableSsl(true) .maxWaitForConnection(100000) // 增加连接超时时间 .maxContentLength(65536) .create(); int numHosts = cluster.availableHosts().size(); System.out.println("Cluster created successfully with the following hosts:"); System.out.println(numHosts); System.out.println("Connecting"); Client client = cluster.connect(); System.out.println("Determining traversal source"); // 定义源和目标图遍历源 final GraphTraversalSource G = AnonymousTraversalSource.traversal().withRemote(DriverRemoteConnection.using(client)); System.out.println("Finding count"); try { long vertexCount = G.V().count().next(); System.out.println("Source count: " + vertexCount); } catch(Exception e) { System.out.println("FAILED TO QUERY"); System.exit(1); } return G; }
程序输出(已替换实际地址为endpoint)
STARTING creating cluster Cluster创建成功,可用主机数: 0 Connecting 确定遍历源 查询顶点数量 09:28:44.187 [gremlin-driver-loop-1] ERROR o.a.t.g.d.Connection - 服务器关闭了通道2017caf4上的连接 - 计划从连接池中移除该连接 (Host{address=endpoint/internalIP, hostUri=wss://endpoint:port/gremlin}) 连接池状态 (size=2 max=8 min=2 toCreate=0 bin=0) ==> Connection{channel=2017caf4 isDead=true borrowed=1 pending=1 markedReplaced=false closing=false created=2024-07-23T16:28:43.549866Z thread=gremlin-driver-conn-scheduler-1} > Connection{channel=83a2625b isDead=false borrowed=0 pending=0 markedReplaced=false closing=false created=2024-07-23T16:28:43.549858Z thread=gremlin-driver-conn-scheduler-2} -- 废弃连接 -- 查询失败 进程退出,代码1
内容的提问来源于stack exchange,提问作者Nikhil Menon
相关产品推荐
相关产品推荐

