知识库

因果集群路由演示

以下将演示如何使用 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` 命令,也可以获取上述详细信息。

如果连接到第三个实例(即一个 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` 客户端已使用新的路由表进行了更新,因此将写入操作发送到 `:7637` 上的新 LEADER。

如果连接没有通过 `bolt+routing` 建立,那么连接将转到由其端口定义的实例;如果该 Neo4j 实例退出,则未来通过 `cypher-shell` 提交的操作将导致:

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