知识库

设置不同用户类型的路由策略以将他们导向不同的服务器

问题陈述

当前有一个由 3 个 Core 节点和 1 个只读副本组成的因果集群。存在两类用户——OLTP 用户和 OLAP 用户。OLTP 用户的查询应只路由到 2 个 Followers(从节点),而不能到只读副本。OLAP 用户的查询应只路由到只读副本,不能到任何 Followers。这样可以避免 OLTP 用户受到 OLAP 长时间查询的影响。

如何在因果集群设置中实现此目标?

为此需要使用 Multi-DC(多数据中心)设置,该功能在 Neo4j 3.2 版中引入。首先启用 Multi-DC 许可证。更多信息请参阅 Neo4j 运维手册 - /docs/operations-manual/current/clustering/causal-clustering/multi-data-center/

按照 Neo4j 文档的说明,将因果集群(CC)配置为 3 个 Core 节点和 1 个只读副本。随后在 neo4j.conf 文件中进行相应修改,以配置哪些应用/用户可以访问集群的哪些实例。下面的配置创建了一个名为 oltp_app 的服务器组,并启用了 Multi-DC 许可证(服务器组需要此许可证)。

核心 1、核心 2 与 核心 3
causal_clustering.server_groups=oltp_app
causal_clustering.multi_dc_license=true
causal_clustering.load_balancing.plugin=server_policies
causal_clustering.load_balancing.config.server_policies.oltp=groups(oltp_app); halt();

接下来,为了只为 OLAP 用户提供只读副本服务,需要如下配置只读副本

READ_REPLICA
causal_clustering.server_groups=olap_app
causal_clustering.multi_dc_license=true
causal_clustering.load_balancing.plugin=server_policies
causal_clustering.load_balancing.config.server_policies.olap=groups(olap_app); halt();

如何在应用程序中使用驱动?

OLTP 用户使用的应用程序应使用如下驱动配置

客户端应用 URL

Driver driver = GraphDatabase.driver( "bolt+routing://127.0.0.1:7687?policy=oltp" , AuthTokens.basic( "neo4j", "password"), Config.build().withMaxTransactionRetryTime( 15, TimeUnit.SECONDS ).toConfig() );

在 URI 中指定 server_policy 名称——bolt+routing://127.0.0.1:7687?policy=oltp。使用上述 URI 时,只读查询只会被路由到 2 个 Followers,因为 Core 的 server_groups 已设置为 oltp_app

关于路由上下文的文档链接位于驱动程序章节:/docs/developer-manual/current/drivers/client-applications/#_routing_drivers_with_routing_context

Multi-DC 功能仅在 Neo4j 3.2 及以上版本可用。
© . This site is unofficial and not affiliated with Neo4j, Inc.