第七章:JanusGraph服务

Chapter 7. JanusGraph Server

JanusGraph使用Gremlin Server作为服务组件对客户端请求进行请求处理响应。当JanusGraph打包的时候,Gremlin Server
就被称为JanusGraph服务。

JanusGraph使用前必须手动启动,JanusGraph服务提供了一种在一个或多个JanusGraph实例上执行远程执行Gremlin
脚本的方法。这部分将会说明如何使用WebSocket配置,同时也会说明如何配置JanusGraph服务来处理HTTP交互。

7.1开始

7.1. Getting Started

7.1.1 使用默认包部署

7.1.1. Using the Pre-Packaged Distribution

JanusGraph发行版预先默认配置了Cassandra作为后端存储,Elasticsearch作为索引服务,以使用户能够快速的使用JanusGraph
服务。这些默认配置允许客户端通过WebSocket协议连接到JanusGraph服务。已经有一些支持该协议的不同语言开发的客户端。大家都熟悉的使用
WebSocket客户端就是Gremlin控制台。快速启动配置不代表生产安装配置,但是提供了一个快速试用JanusGraph
服务的方法,运行测试、看各个组件是如何在一起工作的。

快速上手步骤:

* 从发布页面 <https://github.com/JanusGraph/janusgraph/releases>下载
janusgraph-$VERSION.zip包
* 解压进入janusgraph-$VERSION目录
* 执行bin/janusgraph.sh start命令,这个步骤启动Gremlin服务同时分别启动Cassandra和ES,出于安全考虑,
Elasticsearch要求执行janusgraph.sh脚本的时候必须是在非root账号下执行。 $ bin/janusgraph.sh start
Forking Cassandra... Running `nodetool statusthrift`.. OK (returned exit status0
and printed string"running"). Forking Elasticsearch... Connecting to
Elasticsearch (127.0.0.1:9300)... OK (connected to 127.0.0.1:9300). Forking
Gremlin-Server... Connecting to Gremlin-Server (127.0.0.1:8182)... OK
(connected to127.0.0.1:8182). Run gremlin.sh to connect.
7.1.1.1连接到Gremlin服务

7.1.1.1. Connecting to Gremlin Server

执行janusgraph.sh后,Gremlin将会监听WebSocket连接。最方便的测试连接的方式是使用Gremlin的控制台。

运行bin/gremlin.sh进入Gremlin控制台,使用:remote和:>命令在Gremlin控制台操作Gremlin服务端。
$ bin/gremlin.sh \,,,/ (o o) -----oOOo-(3)-oOOo----- plugin activated:
tinkerpop.serverplugin activated: tinkerpop.hadoop plugin activated:
tinkerpop.utilitiesplugin activated: janusgraph.imports plugin activated:
tinkerpop.tinkergraphgremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Connected - localhost/127.0.0.1:8182 gremlin> :> graph.addVertex("name",
"stephen") ==>v[256] gremlin> :> g.V().values('name') ==>stephen
:remote命令告诉控制台使用conf/remote.yaml配置文件连接Gremlin服务端。这会触使连接到本地运行的Gremlin服务实例。:>
提示服务下输入的命令将被发送到当前连接的远程gremlin
服务上执行。默认情况下,远程连接是无状态的,意思就是说每一行命名都会被当做一个单独的请求进行提交。可以使用分号作为定界符在单行上发送多个语句。另外,在创建连接时,可以通过指定会话来建立具有会话的控制台。控制台会话允许您在多个输入行上重用变量。
gremlin> :remote connect tinkerpop.server conf/remote.yaml ==>Configured
localhost/127.0.0.1:8182 gremlin> graph ==>standardjanusgraph[cql:[127.0.0.1]]
gremlin> g ==>graphtraversalsource[standardjanusgraph[cql:[127.0.0.1]],
standard]gremlin> g.V() gremlin> user = "Chris" ==>Chris gremlin>
graph.addVertex("name", user) No such property: user for class: Script21 Type
':help' or ':h' for help. Display stack trace? [yN] gremlin> :remote connect
tinkerpop.server conf/remote.yaml session ==>Configured localhost/127.0.0.1:8182
-[9acf239e-a3ed-4301-b33f-55c911e04052] gremlin> g.V() gremlin> user = "Chris" =
=>Chris gremlin> user ==>Chris gremlin> graph.addVertex("name", user) ==>v[4344]
gremlin> g.V().values('name') ==>Chris
7.2清理部署程序包

7.2. Cleaning up after the Pre-Packaged Distribution

如果想删除数据库和日志使用一个干净的库,可以使用janusgraph.sh的clean命令,在执行clean命令前得先停止服务。
$ cd /Path/to/janusgraph/janusgraph-0.2.0-hadoop2/ $ ./bin/janusgraph.sh stop
Killing Gremlin-Server (pid91505)... Killing Elasticsearch (pid 91402)...
Killing Cassandra (pid91219)... $ ./bin/janusgraph.sh clean Are you sure you
want to delete all stored data and logs? [y/N] y Deleted datain
/Path/to/janusgraph/janusgraph-0.2.0-hadoop2/db Deleted logs in
/Path/to/janusgraph/janusgraph-0.2.0-hadoop2/log
7.3JanusGraph服务作为WebSocket终端

7.3. JanusGraph Server as a WebSocket Endpoint

在7.1节已经说明了默认WebSocket配置。如果你想更改配置以使用自己的Cassandra或者HBase
环境,而不是快速上手的默认配置环境,跟着执行下面操作步骤:

配置JanusGraph服务以支持WebSocket

* 首先测试本地连接到JanusGraph数据库。这步可以使用Gremlin控制台实现也可以通过程序实现。更改你自己环境下./conf
目录下的配置文件,例如,编辑./conf/janusgraph-hbase.properties配置文件,确认storage.backend、
storage.hostname、storage.hbase.table这些参数被正确指定。更多的JanusGraph后端存储配置请参考 第三章:后端存储
<https://docs.janusgraph.org/latest/storage-backends.html>。确保配置文件中包含下面配置项:
gremlin.graph=org.janusgraph.core.JanusGraphFactory
* 一旦本地配置文件测试通过得到一个可正常工作的配置文件,复制./conf目录下的配置到./conf/gremlin-server目录。 cp
conf/janusgraph-hbase.properties conf/gremlin-server/socket-janusgraph-hbase
-server.properties
* 复制./conf/gremlin-server/gremlin-server.yaml重命名为socket-gremlin-server.yaml
如果需要引用文件的原始版本,请执行此操作。 cp conf/gremlin-server/gremlin-server.yaml conf/gremlin
-server/socket-gremlin-server.yaml
* 修改socket-gremlin-server.yaml配置中下面配置项。

* a. 如果您打算从本地主机以外的其他设备连接到JanusGraph服务器,更新host的IP地址: host: 10.10.10.100
* b. 修改新配置文件的graphs部分,以使JanusGraph服务能够找到并连接上JanusGraph实例。 graphs: { graph:
conf/gremlin-server/socket-janusgraph-hbase-server.properties}
* 指定刚刚的配置yaml配置文件,启动JanusGraph服务。 bin/gremlin-server.sh ./conf/gremlin-server
/socket-gremlin-server.yaml
注意: 不要使用bin/janusgraph.sh启动,这个启动脚本会启用默认配置使用本地Cassandra和Elasticsearch环境。
6. 至此JanusGraph服务已经跑在了WebSocket模式下,可以通过7.1.1.1节提到的方法进行测试了。

7.4JanusGraph服务启用HTTP协议

7.4. JanusGraph Server as a HTTP Endpoint

7.1节快速开始默认配置的协议是WebSocket,如果想更改配使JanusGraph服务支持HTTP协议,跟着下面步骤操作:

配置JanusGraph服务以支持HTTP协议

* 首先尝试连接本地JanusGraph数据库,这一步可以在Gremlin客户端实现,也可以通过程序实现。在./conf目录下配置文件中做适当修改,例如编辑
./conf/janusgraph-hbase.properties,确保storage.backend、storage.hostname、
storage.hbase.table是被显示指定的。更多的JanusGraph后端存储配置请参考 第三章:后端存储
<https://docs.janusgraph.org/latest/storage-backends.html>。确保配置文件中包含下面配置项:
gremlin.graph=org.janusgraph.core.JanusGraphFactory
* 一旦本地配置文件测试通过得到一个可正常工作的配置文件,复制./conf目录下的配置到./conf/gremlin-server目录。 cp
conf/janusgraph-hbase.properties conf/gremlin-server/http-janusgraph-hbase
-server.properties
* 复制./conf/gremlin-server/gremlin-server.yaml到一个新的文件http-gremlin-server.yaml
。如果需要引用文件的原始版本,请执行此操作。 cp conf/gremlin-server/gremlin-server.yaml conf/gremlin
-server/http-gremlin-server.yaml
* 更新http-gremlin-server.yaml配置文件的下面这些配置项:

* a. 如果您打算从本地主机以外的其他设备连接到JanusGraph服务器,更新host的IP地址: host: 10.10.10.100
* b. 更新channelizer配置项为HttpChannelizer channelizer: org.apache.tinkerpop
.gremlin.server.channel.HttpChannelizer
* c. 修改新配置文件的graphs部分,以使JanusGraph服务能够找到并连接上JanusGraph实例。 graphs: { graph:
conf/gremlin-server/http-janusgraph-hbase-server.properties}
* 指定刚刚的配置yaml配置文件,启动JanusGraph服务。 bin/gremlin-server.sh ./conf/gremlin-server
/http-gremlin-server.yaml
* JanusGraph当前已经跑在HTTP模式下并且可以进行测试了。curl命令可以用于验证服务服务已正常工作。 curl -XPOST
-Hcontent-type:application/json -d'{"gremlin":"g.V().count()"}' http://[IP for
JanusGraph server host]:8182
7.5 JanusGraph服务同时支持WebSocket和HTTP

7.5. JanusGraph Server as Both a WebSocket and HTTP Endpoint

在JanusGraph0.2.0版本后,可以修改gremlin-server.yaml使在一个端口同时支持WebSocket和HTTP。这可以修改前面提到的
channelizer配置项实现。
channelizer: org.apache.tinkerpop.gremlin.server.channel.WsAndHttpChannelizer
7.6 JanusGraph服务高级配置

7.6. Advanced JanusGraph Server Configurations

7.6.1 使用HTTP协议需认证

7.6.1. Authentication over HTTP

在下面的例子中,你所使用的图认证数据库可能不一样,根据当前的后端存储选择合适的keyspace、table、存储目录。这个图将被用于存储用户名和密码。

7.6.1.1 HTTP基础认证

7.6.1.1. HTTP Basic authentication

启用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 http://localhost:8182 -d '{"gremlin": "g.V().count()"}'
如果已经启用认证配置,将会返回401。
curl -v -XPOST http://localhost:8182 -d '{"gremlin": "g.V().count()"}' -u
user:password
如果已启用认证配置,响应码会是200,返回值是4。

7.6.2 WebSocket认证

7.6.2. Authentication over WebSocket

WebSocket认证通过简单认证和安全层(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 } }
如果通过gremlin控制台连接,remote yaml文件需要添加username和password配置项,
username: user password: password
7.6.3 HTTP和WebSocket同时启用认证

7.6.3. Authentication over HTTP and WebSocket

如果通信管道协议同时使用HTTP和WebSocket,可以使用SaslAndHMACAuthenticator进行认证,WebSocket
使用的是SASL,HTTP使用的是基础认证,HTTP基于HASH消息的认证(HMAC)。HMAC是一种基于令牌的身份验证,设计用于HTTP。第一次使用的时候通过调用
/session接口获取一个token,然后用这个token进行认证。这被用于分摊密码加密认证所花费的时间。

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 } }
HMAC设计用户HTTP认证,首先调用/session接口获取token,该token默认一个小时过期。这个过期时间可以通过
authentication.config配置组的tokenTimeout配置项修改,这个值是长整型,单位是毫秒。

通过调用/session获取token,例如:
curl http://localhost:8182/session -XGET -u user:password {"token":
"dXNlcjoxNTA5NTQ2NjI0NDUzOkhrclhYaGhRVG9KTnVSRXJ5U2VpdndhalJRcVBtWEpSMzh5WldqRTM4MW89"
}
你可以在消息头加上token进行认证,例如:
curl -v http://localhost:8182/session -XPOST -d '{"gremlin": "g.V().count()"}'
-H"Authorization: Token
dXNlcjoxNTA5NTQ2NjI0NDUzOkhrclhYaGhRVG9KTnVSRXJ5U2VpdndhalJRcVBtWEpSMzh5WldqRTM4MW89"
原文:https://docs.janusgraph.org/latest/server.html
<https://docs.janusgraph.org/latest/server.html>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信