Docker环境下JanusGraph无法连接Cassandra后端求助
JanusGraph Docker容器无法连接Cassandra问题排查与解决
问题描述
自定义构建JanusGraph Docker镜像后,尝试通过Docker Compose让其与Cassandra容器通信,连接失败,报错信息如下:
janusgraph-es | 2449 [main] ERROR org.apache.tinkerpop.gremlin.server.util.ServerGremlinExecutor - Could not invoke constructor on class org.janusgraph.graphdb.management.JanusGraphManager (defined by the 'graphManager' setting) with one argument of class Settings janusgraph-es | Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException janusgraph-es | at org.apache.tinkerpop.gremlin.server.util.ServerGremlinExecutor.<init>(ServerGremlinExecutor.java:91) janusgraph-es | at org.apache.tinkerpop.gremlin.server.GremlinServer.<init>(GremlinServer.java:122) janusgraph-es | at org.apache.tinkerpop.gremlin.server.GremlinServer.<init>(GremlinServer.java:86) janusgraph-es | at org.apache.tinkerpop.gremlin.server.GremlinServer.main(GremlinServer.java:345) janusgraph-es | Caused by: java.lang.reflect.InvocationTargetException janusgraph-es | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) janusgraph-es | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) janusgraph-es | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) janusgraph-es | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) janusgraph-es | at org.apache.tinkerpop.gremlin.server.util.ServerGremlinExecutor.<init>(ServerGremlinExecutor.java:80) janusgraph-es | ... 3 more janusgraph-es | Caused by: java.lang.IllegalArgumentException: Could not instantiate implementation: org.janusgraph.diskstorage.cql.CQLStoreManager janusgraph-es | at org.janusgraph.util.system.ConfigurationUtil.instantiate(ConfigurationUtil.java:64) janusgraph-es | at org.janusgraph.diskstorage.Backend.getImplementationClass(Backend.java:440) janusgraph-es | at org.janusgraph.diskstorage.Backend.getStorageManager(Backend.java:411) janusgraph-es | at org.janusgraph.graphdb.configuration.builder.GraphDatabaseConfigurationBuilder.build(GraphDatabaseConfigurationBuilder.java:50) janusgraph-es | at org.janusgraph.core.JanusGraphFactory.lambda$open$0(JanusGraphFactory.java:150) janusgraph-es | at org.janusgraph.graphdb.management.JanusGraphManager.openGraph(JanusGraphManager.java:243) janusgraph-es | at org.janusgraph.core.JanusGraphFactory.open(JanusGraphFactory.java:150) janusgraph-es | at org.janusgraph.core.JanusGraphFactory.open(JanusGraphFactory.java:100) janusgraph-es | at org.janusgraph.graphdb.management.JanusGraphManager.lambda$new$0(JanusGraphManager.java:75) janusgraph-es | at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) janusgraph-es | at org.janusgraph.graphdb.management.JanusGraphManager.<init>(JanusGraphManager.java:74) janusgraph-es | ... 8 more janusgraph-es | Caused by: java.lang.reflect.InvocationTargetException janusgraph-es | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) janusgraph-es | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) janusgraph-es | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) janusgraph-es | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) janusgraph-es | at org.janusgraph.util.system.ConfigurationUtil.instantiate(ConfigurationUtil.java:58) janusgraph-es | ... 18 more janusgraph-es | Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [/127.0.0.1:9042] Cannot connect)) janusgraph-es | at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:268) janusgraph-es | at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:107) janusgraph-es | at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1813) janusgraph-es | at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1726) janusgraph-es | at com.datastax.driver.core.Cluster.init(Cluster.java:214) janusgraph-es | at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:387) janusgraph-es | at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:366) janusgraph-es | at com.datastax.driver.core.Cluster.connect(Cluster.java:311) janusgraph-es | at org.janusgraph.diskstorage.cql.CQLStoreManager.initializeSession(CQLStoreManager.java:320) janusgraph-es | at org.janusgraph.diskstorage.cql.CQLStoreManager.<init>(CQLStoreManager.java:182) janusgraph-es | ... 23 more
提供的配置文件
Dockerfile
FROM openjdk:8-jre-slim-buster USER root RUN mkdir -p /janusgraph COPY . /janusgraph/. WORKDIR /janusgraph/bin/ CMD ["./gremlin-server.sh"]
Docker Compose
version: "3.3" services: cassandra: image: cassandra:3 container_name: cassandra ports: - "9042:9042" - "9160:9160" healthcheck: test: ["CMD-SHELL", "[ $$(nodetool statusgossip) = running ]"] interval: 30s timeout: 10s networks: - jce-network volumes: - app-volume:/var/lib/cassandra janusgraph-es: # image: docker.io/janusgraph/janusgraph:0.5.3 build: ./janusgraph-full-0.5.3 container_name: janusgraph-es # environment: # JANUS_PROPS_TEMPLATE: cql-es # janusgraph.storage.hostname: cassandra # janusgraph.index.search.hostname: elastic # graph.replace-instance-if-exists: true ports: - "8182:8182" healthcheck: test: ["CMD", "bin/gremlin.sh", "-e", "scripts/remote-connect.groovy"] interval: 10000s timeout: 3000s retries: 3 links: - "cassandra" networks: - jce-network networks: jce-network: volumes: app-volume:
补充说明:已修改JanusGraph配置,指定过Cassandra容器IP和localhost,手动启动JanusGraph可连接Docker中的Cassandra,但将JanusGraph作为Docker服务启动时无法连接。
问题原因与解决方案
核心原因
从报错NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042)可以明确:JanusGraph容器内部仍在尝试连接本地127.0.0.1的Cassandra服务,而非Docker Compose自定义网络中的Cassandra容器。
具体修复步骤
启用JanusGraph环境变量配置
你的Docker Compose中注释掉了关键的环境变量配置,取消注释并调整:environment: JANUS_PROPS_TEMPLATE: cql-es janusgraph.storage.hostname: cassandra # Docker Compose服务名,容器间可直接通过此名称访问 graph.replace-instance-if-exists: truejanusgraph.storage.hostname设置为Cassandra服务名cassandra,Docker Compose的自定义网络中,容器可通过服务名直接解析并访问对方。
确保自定义镜像支持环境变量覆盖配置
官方JanusGraph镜像会自动读取环境变量生成对应的配置文件,若自定义镜像无此逻辑,需手动修改配置:- 修改
/janusgraph/conf/janusgraph.properties,将storage.hostname设置为cassandra - 修改
/janusgraph/conf/gremlin-server/gremlin-server.yaml,确保加载正确的配置文件
- 修改
调整启动顺序与健康检查
JanusGraph启动时Cassandra可能未完全就绪,需配置依赖关联与合理的健康检查:- 在JanusGraph服务中添加
depends_on,等待Cassandra健康就绪后再启动:depends_on: cassandra: condition: service_healthy - 调整JanusGraph健康检查参数,缩短间隔确保服务就绪后再标记为健康:
healthcheck: test: ["CMD", "bin/gremlin.sh", "-e", "scripts/remote-connect.groovy"] interval: 30s timeout: 10s retries: 5
- 在JanusGraph服务中添加
验证网络连通性
进入JanusGraph容器,尝试ping Cassandra服务名,确认网络解析正常:docker exec -it janusgraph-es ping cassandra
完整修改后的Docker Compose示例
version: "3.3" services: cassandra: image: cassandra:3 container_name: cassandra ports: - "9042:9042" - "9160:9160" healthcheck: test: ["CMD-SHELL", "[ $$(nodetool statusgossip) = running ]"] interval: 30s timeout: 10s retries: 3 networks: - jce-network volumes: - app-volume:/var/lib/cassandra janusgraph-es: build: ./janusgraph-full-0.5.3 container_name: janusgraph-es environment: JANUS_PROPS_TEMPLATE: cql-es janusgraph.storage.hostname: cassandra graph.replace-instance-if-exists: true ports: - "8182:8182" healthcheck: test: ["CMD", "bin/gremlin.sh", "-e", "scripts/remote-connect.groovy"] interval: 30s timeout: 10s retries: 5 depends_on: cassandra: condition: service_healthy networks: - jce-network networks: jce-network: volumes: app-volume:
内容的提问来源于stack exchange,提问作者Hari Krishnan Ramachandran
相关产品推荐
相关产品推荐

