Neo4J HighAvailability 设置向导
这是一个设置Neo4j HA集群和将嵌入Neo4j或者Neo4j服务器作为集群节点运行设置的向导。
设置协调器集群(Coordinator cluster)
HA集群使用协调器集群管理自身和生命周期活动协调,如选择一个主机。当运行一个Neo4j HA集群时,协调器集群用来为集群协调在Neo4j HA实体工作之前必须安装和配置。
以下是在一台本地机器上设置3个协调器实体。
下载并解压Neo4j企业版安装包(Neo4j Enterprise)
从http://neo4j.org/download下载Neo4j企业版压缩包并解压出三份Neo4j的安装包(取名叫$NEO4J_HOME1, $NEO4J_HOME12 $NEO4J_HOME3)。
设置并启动协调器集群
在$NEO4J_HOME1/conf/coor.cfg文件中,调整协调器的客户端端口(clientPort)并让协调器在本地端口范围搜索到其他协调器集群成员:
#$NEO4J_HOME1/conf/coord.cfg
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
...
clientPort=2181
分别在
$NEO4J_HOME2和$NEO4J_HOME3中的其他两个配置文件都有不同的客户端端口(clientPort)设置,但其他设置参数都与第一个文件相同:
#$NEO4J_HOME2/conf/coord.cfg server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890 ... clientPort=2182 |
#$NEO4J_HOME2/conf/coord.cfg ... server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890 ... clientPort=2183 |
下一步需要在每个目录下面创建一个叫“myid”并包含服务器各自id并且与server.1,server.2,server.3的值相等的配置文件:
neo4j_home1$ echo '1' > data/coordinator/myid
neo4j_home2$ echo '2' > data/coordinator/myid
neo4j_home3$ echo '3' > data/coordinator/myid
或直接去
$NEO4J_HOME1/ data/coordinator/目录下修改myid文件写入1。
$NEO4J_HOME2/ data/coordinator/目录下修改myid文件写入2。
$NEO4J_HOME3/ data/coordinator/目录下修改myid文件写入3。
现在可以启动协调器实体:
neo4j_home1$ ./bin/neo4j-coordinator start
neo4j_home2$ ./bin/neo4j-coordinator start
neo4j_home3$ ./bin/neo4j-coordinator start
在HA模式下启动Neo4j服务器
在conf/neo4j.properties文件里,可以为3个安装设置必须的HA模式参数,为所有实体调整ha.server_id值:
#$NEO4J_HOME1/conf/neo4j.properties #unique server id for this graph database #can not be negative id and must be unique ha.server_id = 1
#ip and port for this instance to bind to ha.server = localhost:6001
#connection information to the coordinator cluster client ports ha.coordinators = localhost:2181,localhost:2182,localhost:2183 |
#$NEO4J_HOME2/conf/neo4j.properties #unique server id for this graph database #can not be negative id and must be unique ha.server_id = 2
#ip and port for this instance to bind to ha.server = localhost:6001
#connection information to the coordinator cluster client ports ha.coordinators = localhost:2181,localhost:2182,localhost:2183 |
#$NEO4J_HOME3/conf/neo4j.properties #unique server id for this graph database #can not be negative id and must be unique ha.server_id = 3
#ip and port for this instance to bind to ha.server = localhost:6001
#connection information to the coordinator cluster client ports ha.coordinators = localhost:2181,localhost:2182,localhost:2183 |
为了避免启动服务时端口冲突,调整所有实体的REST端口,位于conf/neo4j-server.properties里并允许HA模式:
perties and enable HA mode:
#$NEO4J_HOME1/conf/neo4j-server.properties ... # http port (for all data, administrative, and UI access) org.neo4j.server.webserver.port=7474 ... # Allowed values: # HA - High Availability # SINGLE - Single mode, default. # To run in High Availability mode, configure the coord.cfg file, and the # neo4j.properties config file, then uncomment this line: org.neo4j.server.database.mode=HA |
#$NEO4J_HOME2/conf/neo4j-server.properties ... # http port (for all data, administrative, and UI access) org.neo4j.server.webserver.port=7475 ... # Allowed values: # HA - High Availability # SINGLE - Single mode, default. # To run in High Availability mode, configure the coord.cfg file, and the # neo4j.properties config file, then uncomment this line: org.neo4j.server.database.mode=HA |
#$NEO4J_HOME3/conf/neo4j-server.properties ... # http port (for all data, administrative, and UI access) org.neo4j.server.webserver.port=7476 ... # Allowed values: # HA - High Availability # SINGLE - Single mode, default. # To run in High Availability mode, configure the coord.cfg file, and the # neo4j.properties config file, then uncomment this line: org.neo4j.server.database.mode=HA |
为避免JMX端口冲突调整分配端口给所有的实体,位于conf/neo4j-wrapper.properties:
#$NEO4J_HOME1/conf/neo4j-wrapper.properties
...
# Remote JMX monitoring, adjust the following lines ifneeded.
# Also make sure to update the jmx.access andjmx.password files with appropriate permission roles and passwords,
# the shipped configuration contains only a read onlyrole called 'monitor' with password 'Neo4j'.
# For more details, see: http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html
wrapper.java.additional.4=-Dcom.sun.management.jmxremote.port=3637
...
#$NEO4J_HOME2/conf/neo4j-wrapper.properties
...
# Remote JMX monitoring, adjust the following lines ifneeded.
# Also make sure to update the jmx.access andjmx.password files with appropriate permission roles and passwords,
# the shipped configuration contains only a read onlyrole called 'monitor' with password 'Neo4j'.
# For more details, see: http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html
wrapper.java.additional.4=-Dcom.sun.management.jmxremote.port=3638
...
#$NEO4J_HOME3/conf/neo4j-server.properties
...
# Remote JMX monitoring, adjust the following lines ifneeded.
# Also make sure to update the jmx.access andjmx.password files with appropriate permission roles and passwords,
# the shipped configuration contains only a read onlyrole called 'monitor' with password 'Neo4j'.
# For more details, see: http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html
wrapper.java.additional.4=-Dcom.sun.management.jmxremote.port=3639
...
现在开启三个服务器实体:
neo4j_home1$ ./bin/neo4j start
neo4j_home2$ ./bin/neo4j start
neo4j_home3$ ./bin/neo4j start
在windows环境下结果命令行提示如下:
错误: 找不到口令文件: D:\ProgramFiles\glassfishv3\jdk\jre\lib\management\jmxremote.password。
可以在以下文件中修改即可正常运行:
#$NEO4J_HOME1/conf/neo4j-wrapper.properties
wrapper.java.additional.5=-Dcom.sun.management.jmxremote.authenticate=false
#$NEO4J_HOME2/conf/neo4j-wrapper.properties
wrapper.java.additional.5=-Dcom.sun.management.jmxremote.authenticate=false
#$NEO4J_HOME3/conf/neo4j-wrapper.properties
wrapper.java.additional.5=-Dcom.sun.management.jmxremote.authenticate=false
linux环境目前还没做测试。
现在可以通过以下三个地址访问三个servers(第一个被选成主机,因为他为第一个启动的)
http://localhost:7474/webadmin/#/info/org.neo4j/High%20Availability/
http://localhost:7475/webadmin/#/info/org.neo4j/High%20Availability/
http://localhost:7476/webadmin/#/info/org.neo4j/High%20Availability/
过检查HA配置状态。二选一,REST API也对JMX开放,所以可以通过检查HA JMX实体 e.g:
curl -H "Content-Type:application/json" -d'["org.neo4j:*"]' http://localhost:7474/db/manage/server/jmx/query
可以得到的响应如下次:
"description" : "Information about allinstances in this cluster",
"name" :"InstancesInCluster",
"value" : [ {
"description": "org.neo4j.management.InstanceInfo",
"value" : [{
"description": "address",
"name": "address"
}, {
"description": "instanceId",
"name": "instanceId"
}, {
"description": "lastCommittedTransactionId",
"name": "lastCommittedTransactionId",
"value": 1
}, {
"description": "machineId",
"name": "machineId",
"value": 1
}, {
"description": "master",
"name": "master",
"value": true
} ],
"type" :"org.neo4j.management.InstanceInfo"
在HA模式下开启Neo4j嵌入式数据库
如果是使用Maven和Neo4j Embedded,简单添加以下依赖配置到工程中即可:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ha</artifactId>
<version>${neo4j-version}</version>
</dependency>
${neo4j-version}表示的当前所用Neo4j版本号。
如果手动下载jar导入到项目,请参考其他文章。
使用Neo4j-HA在代码中的区别就是创建图形数据库服务。
GraphDatabaseService db = new HighlyAvailableGraphDatabase(path, config );
配置(config)中可以包含标准配置参数(上面提供的作为config参数的部分或者在neo4j.properties中必须包含:)
#HA instance1
#unique machine id for this graph database
#can not be negative id and must be unique
ha.server_id = 1
#ip and port for this instance to bind to
ha.server = localhost:6001
#connection information to the coordinator cluster clientports
ha.coordinators =localhost:2181,localhost:2182,localhost:2183
enable_remote_shell = port=1331
首先需要创建数据库。就跟开启一个嵌入式图形数据库一样简单,指出一个路径并关闭。
ap<String,String> config =HighlyAvailableGraphDatabase.loadConfigurations( configFile );
GraphDatabaseService db = new HighlyAvailableGraphDatabase(path, config );
创建一个设置机器id=1的配置文件并允许远程shell。主方法需要dbde 路径设置为第一个参数并将配置文件设置第二个参数。
现在可以通过shell连接实体:
neo4j_home1$ ./bin/neo4j-shell -port 1331
NOTE: Remote Neo4j graph database service 'shell' at port1331
Welcome to the Neo4j Shell! Enter 'help' for a list ofcommands
neo4j-sh (0)$ hainfo
I'm currently master
Connected slaves:
因此第一个加入集群的实体将被选为主机。开启其他实体需要第二个配置参数和db的路径。
#HA instance2
#unique machine id for this graph database
#can not be negative id and must be unique
ha.server_id = 2
#ip and port for this instance to bind to
ha.server = localhost:6001
#connection information to the coordinator cluster clientports
ha.coordinators =localhost:2181,localhost:2182,localhost:2183
enable_remote_shell = port=1332
现在开始shell连接到端口1332:
neo4j_home1$ ./bin/neo4j-shell -port 1332
NOTE: Remote Neo4j graph database service 'shell' at port1332
Welcome to the Neo4j Shell! Enter 'help' for a list ofcommands
neo4j-sh (0)$ hainfo
I'm currently slave