neo4j高可用環境搭建


neo4j是一個高性能的面向圖的數據庫,據說是java世界“最受歡迎的圖數據庫”,雖然,圖數據庫本身也沒有幾種可以選......。

neo4j的高可用很容易搭建,本文使用的是neo4j-2.0.2版本,在其官方網站的manual中有非常詳細的例子,參考其manual的ch23,http://docs.neo4j.org/chunked/2.0.2/ha-setup-tutorial.html。其中提到了2個情況下的例子,一個是真正的生產情況下,采用多台機器部署的情況;另一個是開發環境中,在一台機器上啟動多個服務模擬生產的高可用。本文采用的是后一種,此次實驗依然是在ubuntu 64bit上進行。

先去官網上下載neo4j的企業版,地址在:http://dist.neo4j.org/neo4j-enterprise-2.0.2-unix.tar.gz。由於neo4j是用java寫的,所以,沒有JRE是不行的,安裝Java的過程在這里略過,我用的是目前最新發布的java 8。

XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

下載之后解壓,需要用root用戶安裝neo4j的service,

XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ sudo ./bin/neo4j-installer install
WARNING: this installer is deprecated and may not be the optimal way to install Neo4j on your system.
  Please see the Neo4j Manual for up to date information on installing Neo4j.
Press any key to continue
Graph-like power should be handled carefully. What user should run Neo4j? [neo4j] XXXXX
 Adding system startup for /etc/init.d/neo4j-service ...
   /etc/rc0.d/K20neo4j-service -> ../init.d/neo4j-service
   /etc/rc1.d/K20neo4j-service -> ../init.d/neo4j-service
   /etc/rc6.d/K20neo4j-service -> ../init.d/neo4j-service
   /etc/rc2.d/S20neo4j-service -> ../init.d/neo4j-service
   /etc/rc3.d/S20neo4j-service -> ../init.d/neo4j-service
   /etc/rc4.d/S20neo4j-service -> ../init.d/neo4j-service
   /etc/rc5.d/S20neo4j-service -> ../init.d/neo4j-service

安裝的時候有一個提示非常重要,讓你選哪個用戶是neo4j的主用戶,默認是neo4j,一般情況下你系統里面不會有這樣一個用戶的,所以一定要改為你常用的那個用戶。之后就可以啟動neo4j

XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ service neo4j-service status
Neo4j Server is not running
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ service neo4j-service start
WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual.
Using additional JVM arguments:  -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled
Starting Neo4j Server...WARNING: not changing user
process [14512]... waiting for server to be ready................ OK.
http://localhost:7474/ is ready.
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ service neo4j-service status
Neo4j Server is running at pid 14512
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ 

啟動時會打印一些比較簡單的log,打印了進程號。

如果要做高可用的話,需要多個neo4j的數據庫,把解壓的這個目錄復制2份,這樣就有3個內容一樣的目錄,用於高可用的配置。neo4j的高可用主要需要修改conf/neo4j.properties和conf/neo4j-server.properties這2個文件。節點1需要修改的配置如下,

conf/neo4j.properties:

# Unique server id for this Neo4j instance
# can not be negative id and must be unique
ha.server_id = 1

# IP and port for this instance to bind to for communicating data with the
# other neo4j instances in the cluster.
ha.server = 127.0.0.1:6363
online_backup_server = 127.0.0.1:6366

# IP and port for this instance to bind to for communicating cluster information
# with the other neo4j instances in the cluster.
ha.cluster_server = 127.0.0.1:5001

# List of other known instances in this cluster
ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003



conf/neo4j-server.properties

# database location
org.neo4j.server.database.location=data/graph.db

# http port (for all data, administrative, and UI access)
org.neo4j.server.webserver.port=7474

# webserver IP bind
org.neo4j.server.webserver.address=0.0.0.0

# https port (for all data, administrative, and UI access)
org.neo4j.server.webserver.https.port=7484

# HA - High Availability
# SINGLE - Single mode, default.
org.neo4j.server.database.mode=HA

neo4j中有一個關於IP的配置,0.0.0.0,表示neo4j需要監聽該機器上的所有ip,一般對neo4j提供的web管理功能,都會采用這種方式,即配置org.neo4j.server.webserver.address這個參數。

節點2和節點3上的配置比較類似,只列出節點2的配置,如下,

conf/neo4j.properties:

# Unique server id for this Neo4j instance
# can not be negative id and must be unique
ha.server_id = 2

# IP and port for this instance to bind to for communicating data with the
# other neo4j instances in the cluster.
ha.server = 127.0.0.1:6364
online_backup_server = 127.0.0.1:6367

# IP and port for this instance to bind to for communicating cluster information
# with the other neo4j instances in the cluster.
ha.cluster_server = 127.0.0.1:5002

# List of other known instances in this cluster
ha.initial_hosts = 127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003



conf/neo4j-server.properties

# database location
org.neo4j.server.database.location=data/graph.db

# http port (for all data, administrative, and UI access)
org.neo4j.server.webserver.port=7475

# webserver IP bind
org.neo4j.server.webserver.address=0.0.0.0

# https port (for all data, administrative, and UI access)
org.neo4j.server.webserver.https.port=7485

# HA - High Availability
# SINGLE - Single mode, default.
org.neo4j.server.database.mode=HA

配置完成之后,就可以啟動包含3個neo4j節點的HA服務。啟動步驟如下

XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ ./bin/neo4j start
WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual.
Using additional JVM arguments:  -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled
Starting Neo4j Server...WARNING: not changing user
HA instance started in process [20002]. Will be operational once connected to peers. See /home/XXXXX/neo4j-enterprise-2.0.2/data/log/console.log for current status.
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2$ cd ../neo4j-enterprise-2.0.2-node2/
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node2$ ./bin/neo4j start
WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual.
Using additional JVM arguments:  -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled
Starting Neo4j Server...WARNING: not changing user
HA instance started in process [22238]. Will be operational once connected to peers. See /home/XXXXX/neo4j-enterprise-2.0.2-node2/data/log/console.log for current status.
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node2$ cd ../neo4j-enterprise-2.0.2-node3/
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node3$ ./bin/neo4j start
WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual.
Using additional JVM arguments:  -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled
Starting Neo4j Server...WARNING: not changing user
HA instance started in process [22605]. Will be operational once connected to peers. See /home/XXXXX/neo4j-enterprise-2.0.2-node3/data/log/console.log for current status.
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node3$ jps
20002 Bootstrapper
22238 Bootstrapper
22605 Bootstrapper
23805 Jps
XXXXX@XXXXX-asus:~/neo4j-enterprise-2.0.2-node3$ 

最后用jps查看進程,發現有3個Bootstrapper進程,這個就是neo4j節點的進程。啟動完成之后,可以用web方式登陸http://localhost:7474/webadmin/進行管理,如果直接登陸http://localhost:7474/返回的是http://localhost:7474/browser/,這個頁面中能做的事情就很少,只可以運行一些簡單的查詢。

webadmin頁面有5個標簽,如下圖

在最后一個標簽“Server Info”中有一項“High Availability”,可以查看HA運行的信息,以下是節點1的信息,

在第3個標簽“Console”中可以敲入Cypher(neo4j的查詢語言),用於管理。

我們可以在節點1中使用Cypher插入數據,

neo4j-sh (?)$ CREATE (ee { name: "Emil", from: "Sweden" }) RETURN ee.name;
==> +---------+
==> | ee.name |
==> +---------+
==> | "Emil"  |
==> +---------+
==> 1 row
==> Nodes created: 1
==> Properties set: 2
==> 847 ms
neo4j-sh (?)$ START ee=node(*) WHERE ee.name! = "Emil" RETURN ee;
==> SyntaxException: This syntax is no longer supported (missing properties are now returned as null). (line 1, column 27)
==> "START ee=node(*) WHERE ee.name! = "Emil" RETURN ee"
==>                            ^
neo4j-sh (?)$ START a = node(0) RETURN a;
==> +------------------------------------+
==> | a                                  |
==> +------------------------------------+
==> | Node[0]{name:"Emil",from:"Sweden"} |
==> +------------------------------------+
==> 1 row
==> 38 ms

然后再去節點2或者節點3上查詢,

neo4j-sh (?)$ START a = node(0) RETURN a;
==> +------------------------------------+
==> | a                                  |
==> +------------------------------------+
==> | Node[0]{name:"Emil",from:"Sweden"} |
==> +------------------------------------+
==> 1 row
==> 339 ms
neo4j-sh (?)$ 

發現,數據已經由節點1同步到節點3,說明,HA已經在正常的工作了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM