因果集群路由演示
以下示例将演示如何使用 cypher-shell 来更好地了解 Neo4j 因果集群实例及其对 routing 的实现。
初始情景使用 local cluster 搭建,包含 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://:7617", "https://:7414", "https://:7413"] | "LEADER" |
| ["bolt://:7627", "https://:7424", "https://:7423"] | "FOLLOWER" |
| ["bolt://:7637", "https://:7434", "https://:7433"] | "FOLLOWER" |
+-----------------------------------------------------------------------------------------------+
上述细节也可以在浏览器中运行 :sysinfo 查看。
如果连接到第 3 台实例(即 FOLLOWER)且未包含 routing 属性,则在 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://:7627", "https://:7424", "https://:7423"] | "FOLLOWER" | [] |
| "06c1399d-ec17-4cf5-a31e-fb0db135f543" | ["bolt://:7637", "https://:7434", "https://: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 的 NEW 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.
此页面有帮助吗?