【JanusGraph】第七章:JanusGraph服务

第七章: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服务的方法,运行测试、看各个组件是如何在一起工作的。

快速上手步骤:

  • 发布页面下载janusgraph-$VERSION.zip
  • 解压进入janusgraph-$VERSION目录
  • 执行bin/janusgraph.sh start命令,这个步骤启动Gremlin服务同时分别启动CassandraES,出于安全考虑,Elasticsearch要求执行janusgraph.sh脚本的时候必须是在非root账号下执行。
$ bin/janusgraph.sh start
Forking Cassandra...
Running `nodetool statusthrift`.. OK (returned exit status 0 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 to 127.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.server
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.utilities
plugin activated: janusgraph.imports
plugin activated: tinkerpop.tinkergraph
gremlin> :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.shclean命令,在执行clean命令前得先停止服务。

$ cd /Path/to/janusgraph/janusgraph-0.2.0-hadoop2/
$ ./bin/janusgraph.sh stop
Killing Gremlin-Server (pid 91505)...
Killing Elasticsearch (pid 91402)...
Killing Cassandra (pid 91219)...
$ ./bin/janusgraph.sh clean
Are you sure you want to delete all stored data and logs? [y/N] y
Deleted data in /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

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

注意: 不要使用bin/janusgraph.sh启动,这个启动脚本会启用默认配置使用本地CassandraElasticsearch环境。
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协议

  1. 首先尝试连接本地JanusGraph数据库,这一步可以在Gremlin客户端实现,也可以通过程序实现。在./conf目录下配置文件中做适当修改,例如编辑./conf/janusgraph-hbase.properties,确保storage.backendstorage.hostnamestorage.hbase.table是被显示指定的。更多的JanusGraph后端存储配置请参考 第三章:后端存储。确保配置文件中包含下面配置项:
gremlin.graph=org.janusgraph.core.JanusGraphFactory
  1. 一旦本地配置文件测试通过得到一个可正常工作的配置文件,复制./conf目录下的配置到./conf/gremlin-server目录。
cp conf/janusgraph-hbase.properties conf/gremlin-server/http-janusgraph-hbase-server.properties
  1. 复制./conf/gremlin-server/gremlin-server.yaml到一个新的文件http-gremlin-server.yaml。如果需要引用文件的原始版本,请执行此操作。
cp conf/gremlin-server/gremlin-server.yaml conf/gremlin-server/http-gremlin-server.yaml
  1. 更新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}
  1. 指定刚刚的配置yaml配置文件,启动JanusGraph服务。
bin/gremlin-server.sh ./conf/gremlin-server/http-gremlin-server.yaml
  1. 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使在一个端口同时支持WebSocketHTTP。这可以修改前面提到的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

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

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文件需要添加usernamepassword配置项,

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值