跳到内容

JanusGraph 服务器

JanusGraph 使用 Gremlin Server 引擎作为服务器组件来处理和回答客户端查询,并用 JanusGraph 的便利功能对其进行扩展。从现在开始,我们将称之为 JanusGraph 服务器。

JanusGraph 服务器必须手动启动才能使用。JanusGraph 服务器提供了一种远程执行针对其托管的一个或多个 JanusGraph 实例的 Gremlin 遍历的方法。本节将描述如何使用 WebSocket 配置,以及如何配置 JanusGraph 服务器来处理 HTTP 端点交互。有关如何从不同语言连接到 JanusGraph 服务器的信息,请参阅 连接到 JanusGraph

启动 JanusGraph 服务器

JanusGraph 服务器附带一个名为 bin/janusgraph-server.sh 的脚本用于启动

$ ./bin/janusgraph-server.sh console
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/var/lib/janusgraph/lib/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/var/lib/janusgraph/lib/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
0    [main] INFO  org.janusgraph.graphdb.server.JanusGraphServer  -                                                                       
   mmm                                mmm                       #     
     #   mmm   m mm   m   m   mmm   m"   "  m mm   mmm   mmmm   # mm  
     #  "   #  #"  #  #   #  #   "  #   mm  #"  " "   #  #" "#  #"  # 
     #  m"""#  #   #  #   #   """m  #    #  #     m"""#  #   #  #   # 
 "mmm"  "mm"#  #   #  "mm"#  "mmm"   "mmm"  #     "mm"#  ##m#"  #   # 
                                                         #            
                                                         "            
[...]
2240 [gremlin-server-boss-1] INFO  org.apache.tinkerpop.gremlin.server.GremlinServer  - Channel started at port 8182.

JanusGraph 服务器由提供的 YAML 文件 conf/gremlin-server/gremlin-server.yaml 配置。该文件向 JanusGraph 服务器说明了许多事情,并且基于 Gremlin Server 配置,请参阅 Gremlin Server

janusgraph-server.sh 的用法

JanusGraph 服务器可以在前台启动,带标准输出日志,也可以分离式启动。

$ ./bin/janusgraph-server.sh
Usage: ./bin/janusgraph-server.sh {start [conf file]|stop|restart [conf file]|status|console|usage <group> <artifact> <version>|<conf file>}

    start           Start the server in the background. Configuration file can be specified as a second argument
                    or as JANUSGRAPH_YAML environment variable. If configuration file is not specified
                    or has invalid path than JanusGraph server will try to use the default configuration file
                    at relative location conf/gremlin-server/gremlin-server.yaml
    stop            Stop the server
    restart         Stop and start the server. To use previously used configuration it should be specified again
                    as described in "start" command
    status          Check if the server is running
    console         Start the server in the foreground. Same rules are applied for configurations as described
                    in "start" command
    usage           Print out this help message

In case command is not specified and the configuration is specified as the first argument, JanusGraph Server will
 be started in the foreground using the specified configuration (same as with "console" command).

环境变量

变量 描述 默认值
$JANUSGRAPH_HOME 默认 janusgraph 安装的根目录。 (janusgraph-server.sh 下的默认目录)
$JANUSGRAPH_CONF 包含所有服务器和图配置的配置目录。 $JANUSGRAPH_HOME/conf
$LOG_DIR 日志目录 "$JANUSGRAPH_HOME/logs"
$LOG_FILE 默认日志文件 "$LOG_DIR/janusgraph.log"
$PID_DIR "$JANUSGRAPH_HOME/run"
$PID_FILE "$PID_DIR/janusgraph.pid"
$JANUSGRAPH_YAML JanusGraph 服务器配置路径 "$JANUSGRAPH_CONF/gremlin-server/gremlin-server.yaml"
$JANUSGRAPH_LIB JanusGraph 库目录 "$JANUSGRAPH_HOME/lib"
$JAVA_HOME 如果未设置,java home 将回退到 java 未设置
$JAVA_OPTIONS_FILE "$JANUSGRAPH_CONF/jvm.options"
$JAVA_OPTIONS 未设置
$CP 可用于覆盖类路径。(专家模式) 未设置
$DEBUG 如果您通过创建此环境变量启用调试,则将启用 bash 调试。 未设置

配置 jvm.options

JanusGraph 运行在 JVM 上,JVM 可针对特殊用例进行配置。因此,JanusGraph 提供了一个 jvm.options 文件,其中包含一些默认选项。

# Copyright 2020 JanusGraph Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://apache.ac.cn/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#################
# HEAP SETTINGS #
#################

-Xms4096m
-Xmx4096m


########################
# GENERAL JVM SETTINGS #
########################


# enable thread priorities, primarily so we can give periodic tasks
# a lower priority to avoid interfering with client workload
-XX:+UseThreadPriorities

# allows lowering thread priority without being root on linux - probably
# not necessary on Windows but doesn't harm anything.
# see http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workar
-XX:ThreadPriorityPolicy=42

# Enable heap-dump if there's an OOM
-XX:+HeapDumpOnOutOfMemoryError

# Per-thread stack size.
-Xss256k

# Make sure all memory is faulted and zeroed on startup.
# This helps prevent soft faults in containers and makes
# transparent hugepage allocation more effective.
-XX:+AlwaysPreTouch

# Enable thread-local allocation blocks and allow the JVM to automatically
# resize them at runtime.
-XX:+UseTLAB
-XX:+ResizeTLAB
-XX:+UseNUMA


####################
# GREMLIN SETTINGS #
####################

-Dgremlin.io.kryoShimService=org.janusgraph.hadoop.serialize.JanusGraphKryoShimService


#################
#  GC SETTINGS  #
#################

### CMS Settings

-XX:+UseParNewGC
-XX:+UseConcMarkSweepGC
-XX:+CMSParallelRemarkEnabled
-XX:SurvivorRatio=8
-XX:MaxTenuringThreshold=1
-XX:CMSInitiatingOccupancyFraction=75
-XX:+UseCMSInitiatingOccupancyOnly
-XX:CMSWaitDuration=10000
-XX:+CMSParallelInitialMarkEnabled
-XX:+CMSEdenChunksRecordAlways
-XX:+CMSClassUnloadingEnabled

JanusGraph 服务器作为 WebSocket 端点

入门 中描述的默认配置已经是 WebSocket 配置。如果您想更改默认配置以使用自己的 Cassandra 或 HBase 环境,而不是使用快速启动环境,请按照以下步骤操作

配置 JanusGraph 服务器以用于 WebSocket

  1. 首先测试与 JanusGraph 数据库的本地连接。此步骤适用于使用 Gremlin Console 测试连接,或从程序连接。在 ./conf 目录中的属性文件中为您的环境进行适当的更改。例如,编辑 ./conf/janusgraph-hbase.properties 并确保 storage.backend、storage.hostname 和 storage.hbase.table 参数设置正确。有关配置 JanusGraph 以用于各种存储后端的信息,请参阅 存储后端。确保属性文件包含以下行

    gremlin.graph=org.janusgraph.core.JanusGraphFactory
    

  2. 一旦本地配置经过测试并且您拥有一个可用的属性文件,请将属性文件从 ./conf 目录复制到 ./conf/gremlin-server 目录。

    cp conf/janusgraph-hbase.properties
    conf/gremlin-server/socket-janusgraph-hbase-server.properties
    

  3. ./conf/gremlin-server/gremlin-server.yaml 复制到一个名为 socket-gremlin-server.yaml 的新文件。这样做以防您需要参考文件的原始版本

    cp conf/gremlin-server/gremlin-server.yaml
    conf/gremlin-server/socket-gremlin-server.yaml
    

  4. 编辑 socket-gremlin-server.yaml 文件并进行以下更新

    1. 如果您计划从 localhost 以外的地方连接到 JanusGraph 服务器,请更新主机的 IP 地址

      host: 10.10.10.100
      

    2. 更新 graphs 部分以指向您的新属性文件,以便 JanusGraph 服务器可以找到并连接到您的 JanusGraph 实例

      graphs: { graph:
          conf/gremlin-server/socket-janusgraph-hbase-server.properties}
      

  5. 启动 JanusGraph 服务器,指定您刚刚配置的 yaml 文件

    bin/janusgraph-server.sh console ./conf/gremlin-server/socket-gremlin-server.yaml
    

  6. JanusGraph 服务器现在应该以 WebSocket 模式运行,并且可以通过遵循 连接到 Gremlin Server 中的说明进行测试。

重要

不要使用 bin/janusgraph.sh。那会启动默认配置,它会启动一个单独的 Cassandra/Elasticsearch 环境。

JanusGraph 服务器作为 HTTP 端点

入门 中描述的默认配置是 WebSocket 配置。如果您想更改默认配置以将 JanusGraph 服务器用作 JanusGraph 数据库的 HTTP 端点,请按照以下步骤操作

  1. 首先测试与 JanusGraph 数据库的本地连接。此步骤适用于使用 Gremlin Console 测试连接,或从程序连接。在 ./conf 目录中的属性文件中为您的环境进行适当的更改。例如,编辑 ./conf/janusgraph-hbase.properties 并确保 storage.backend、storage.hostname 和 storage.hbase.table 参数设置正确。有关配置 JanusGraph 以用于各种存储后端的信息,请参阅 存储后端。确保属性文件包含以下行

    gremlin.graph=org.janusgraph.core.JanusGraphFactory
    

  2. 一旦本地配置经过测试并且您拥有一个可用的属性文件,请将属性文件从 ./conf 目录复制到 ./conf/gremlin-server 目录。

    cp conf/janusgraph-hbase.properties conf/gremlin-server/http-janusgraph-hbase-server.properties
    

  3. ./conf/gremlin-server/gremlin-server.yaml 复制到一个名为 http-gremlin-server.yaml 的新文件。这样做以防您需要参考文件的原始版本

    cp conf/gremlin-server/gremlin-server.yaml conf/gremlin-server/http-gremlin-server.yaml
    

  4. 编辑 http-gremlin-server.yaml 文件并进行以下更新

    1. 如果您计划从 localhost 以外的地方连接到 JanusGraph 服务器,请更新主机的 IP 地址

      host: 10.10.10.100
      

    2. 更新 channelizer 设置以指定 HttpChannelizer

      channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
      

    3. 更新 graphs 部分以指向您的新属性文件,以便 JanusGraph 服务器可以找到并连接到您的 JanusGraph 实例

      graphs: { graph:
          conf/gremlin-server/http-janusgraph-hbase-server.properties}
      

  5. 启动 JanusGraph 服务器,指定您刚刚配置的 yaml 文件

    bin/janusgraph-server.sh console ./conf/gremlin-server/http-gremlin-server.yaml
    

  6. JanusGraph 服务器现在应该以 HTTP 模式运行并可供测试。可以使用 curl 来验证服务器是否正常工作。

    curl -XPOST -Hcontent-type:application/json -d *{"gremlin":"g.V().count()"}* [IP for JanusGraph server host](http://):8182 
    

JanusGraph 服务器同时作为 WebSocket 和 HTTP 端点

从 JanusGraph 0.2.0 开始,您可以配置 gremlin-server.yaml 以通过同一端口同时接受 WebSocket 和 HTTP 连接。这可以通过如下更改前面任何示例中的 channelizer 来实现。

channelizer: org.apache.tinkerpop.gremlin.server.channel.WsAndHttpChannelizer

高级 JanusGraph 服务器配置

HTTP 认证

重要

在以下示例中,credentialsDb 应该与您正在使用的图(graph)不同。它应该配置正确的后端和不同的键空间、表或存储目录,这取决于配置的后端。此图将用于存储用户名和密码。

HTTP 基本认证

要在 JanusGraph 服务器中启用基本认证,请在您的 gremlin-server.yaml 中包含以下配置。

 authentication: {
   authenticator: org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator,
   authenticationHandler: org.apache.tinkerpop.gremlin.server.handler.HttpBasicAuthenticationHandler,
   config: {
     defaultUsername: user,
     defaultPassword: password,
     credentialsDb: conf/janusgraph-credentials-server.properties
    }
 }

验证基本认证是否配置正确。例如

curl -v -XPOST https://:8182 -d '{"gremlin": "g.V().count()"}'

如果认证配置正确,应该返回 401,并且

curl -v -XPOST https://:8182 -d '{"gremlin": "g.V().count()"}' -u user:password
如果认证配置正确,应该返回 200 和 4 的结果。

WebSocket 认证

WebSocket 上的认证通过简单认证和安全层(https://zh.wikipedia.org/wiki/Simple_Authentication_and_Security_Layer[SASL])机制进行。

要启用 SASL 认证,请在 gremlin-server.yaml 中包含以下配置

authentication: {
  authenticator: org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator,
  authenticationHandler: org.apache.tinkerpop.gremlin.server.handler.SaslAuthenticationHandler,
  config: {
    defaultUsername: user,
    defaultPassword: password,
    credentialsDb: conf/janusgraph-credentials-server.properties
  }
}

重要

在前面的示例中,credentialsDb 应该与您正在使用的图(graph)不同。它应该配置正确的后端和不同的键空间、表或存储目录,这取决于配置的后端。此图将用于存储用户名和密码。

如果您通过 gremlin 控制台连接,您的远程 yaml 文件应该用适当的值修改 usernamepassword 属性。

username: user
password: password

HTTP 和 WebSocket 认证

如果您正在为 HTTP 和 WebSocket 使用组合 channelizer,您可以使用 SaslAndHMACAuthenticator 通过 SASL 进行 WebSocket 认证,通过基本认证进行 HTTP 认证,以及通过基于哈希的消息认证码(https://zh.wikipedia.org/wiki/Hash-based_message_authentication_code[HMAC])进行 HTTP 认证。HMAC 是一种基于令牌的认证,旨在用于 HTTP。您首先通过 /session 端点获取一个令牌,然后使用该令牌进行认证。它用于摊销使用基本认证加密密码所花费的时间。

gremlin-server.yaml 应包含以下配置

authentication: {
  authenticator: org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator,
  authenticationHandler: org.janusgraph.graphdb.tinkerpop.gremlin.server.handler.SaslAndHMACAuthenticationHandler,
  config: {
    defaultUsername: user,
    defaultPassword: password,
    hmacSecret: secret,
    credentialsDb: conf/janusgraph-credentials-server.properties
  }
}

重要

在前面的示例中,credentialsDb 应该与您正在使用的图(graph)不同。它应该配置正确的后端和不同的键空间、表或存储目录,这取决于配置的后端。此图将用于存储用户名和密码。

重要

请注意这里的 hmacSecret。如果您希望能够在每个服务器上使用相同的 HMAC 令牌,则所有正在运行的 JanusGraph 服务器上的此值应该相同。

对于通过 HTTP 进行 HMAC 认证,这将创建一个 /session 端点,该端点默认提供一个在一小时后过期的令牌。令牌的此超时可以在 authentication.config 映射中的 tokenTimeout 配置选项中进行配置。此值为 Long 类型,以毫秒为单位。

您可以通过向 /session 端点发出 get 请求来使用 curl 获取令牌。例如

curl https://:8182/session -XGET -u user:password

{"token": "dXNlcjoxNTA5NTQ2NjI0NDUzOkhrclhYaGhRVG9KTnVSRXJ5U2VpdndhalJRcVBtWEpSMzh5WldqRTM4MW89"}

然后您可以使用该令牌通过“Authorization: Token”头进行认证。例如

curl -v https://:8182/session -XPOST -d '{"gremlin": "g.V().count()"}' -H "Authorization: Token dXNlcjoxNTA5NTQ2NjI0NDUzOkhrclhYaGhRVG9KTnVSRXJ5U2VpdndhalJRcVBtWEpSMzh5WldqRTM4MW89"

扩展 JanusGraph 服务器

注意

我们目前正在重构 JanusGraph 服务器。如果您想获取信息或提供意见,请参阅 问题 #2119

通过实现 Gremlin Server 提供的接口并将其与 JanusGraph 结合使用,可以扩展 Gremlin Server 以支持其他通信方式。更多详细信息请参阅 TinkerPop 相关文档。