知识库

因果集群路由演示

以下将演示如何使用 Cypher shell 更好地了解 Neo4j 因果集群实例及其 路由 实现。

初始场景使用 本地集群 设置描述,其中包含 3 个核心实例,1 个 LEADER 和 2 个 FOLLOWER。使用 dbms.cluster.overview() 输出报告

$ ./cypher-shell -a bolt://192.168.0.97:7617
Connected to Neo4j 3.2.2 at bolt://192.168.0.97:7617.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j> call dbms.cluster.overview() yield addresses, role;
+-----------------------------------------------------------------------------------------------+
| addresses                                                                    | role           |
+-----------------------------------------------------------------------------------------------+
| ["bolt://localhost:7617", "http://localhost:7414", "https://localhost:7413"] | "LEADER"       |
| ["bolt://localhost:7627", "http://localhost:7424", "https://localhost:7423"] | "FOLLOWER"     |
| ["bolt://localhost:7637", "http://localhost:7434", "https://localhost:7433"] | "FOLLOWER"     |
+-----------------------------------------------------------------------------------------------+

上述详细信息也可通过在浏览器中运行 :sysinfo 获取。

如果连接到第 3 个实例(即 FOLLOWER)并且未包含路由属性,则无法在 FOLLOWER 上写入,这是预期的,例如

$ ./cypher-shell -a bolt://192.168.0.97:7637
Connected to Neo4j 3.2.2 at bolt://192.168.0.97:7637.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j> create (m:Person {id:123});
No write operations are allowed directly on this database. Writes must pass through the leader. The role of this server is: FOLLOWER
neo4j> :exit

但是,如果连接到同一个 FOLLOWER 实例并且包含 bolt+routing 属性,则可以进行写入,例如

$ ./cypher-shell -a bolt+routing://192.168.0.97:7637
Connected to Neo4j 3.2.2 at bolt://192.168.0.97:7637.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j> create (m:Person {id:123});
0 rows available after 791 ms, consumed after another 3 ms
neo4j> :exit

在这种情况下,尽管 Cypher shell 已启动并描述为连接到端口 :7637 上的 FOLLOWER,但由于 bolt+routing,连接实际上已重定向到 LEADER,例如

$ ./cypher-shell -a bolt+routing://192.168.0.97:7637
Connected to Neo4j 3.2.2 at bolt+routing://192.168.0.97:7637.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j> call dbms.cluster.role();
+----------+
| role     |
+----------+
| "LEADER" |
+----------+

最后,如果在初始 Cypher shell 连接建立后 LEADER 发生故障,并且因此重新选举导致实例 3 中的 FOLLOWER 被报告为新的 LEADER,作为证据

neo4j> call dbms.cluster.overview();
+-------------------------------------------------------------------------------------------------------------------------------------------------+
| id                                     | addresses                                                                    | role           | groups |
+-------------------------------------------------------------------------------------------------------------------------------------------------+
| "0ec70285-5f4a-4a4a-97ce-916592525944" | ["bolt://localhost:7627", "http://localhost:7424", "https://localhost:7423"] | "FOLLOWER"     | []     |
| "06c1399d-ec17-4cf5-a31e-fb0db135f543" | ["bolt://localhost:7637", "http://localhost:7434", "https://localhost:7433"] | "LEADER"       | []     |
+-------------------------------------------------------------------------------------------------------------------------------------------------+

Cypher shell 连接能够继续,例如

neo4j> create (n:Person {id:456});
0 rows available after 133 ms, consumed after another 1 ms
Added 1 nodes, Set 1 properties, Added 1 labels

在这种情况下,Cypher shell 客户端已更新了新的路由表,因此将 WRITEs 发送到 :7637 上的新 LEADER。

如果连接没有使用 `bolt+routing` 建立,则连接将转到其 :port 定义的实例,如果该 Neo4j 实例退出,则通过 Cypher shell 的未来提交将导致

SSL Connection terminated while receiving data. This can happen due to network instabilities, or due to restarts of the database.