1. 程式人生 > >【JanusGraph】第七章:JanusGraph服務

【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