为不同用户类型设置路由策略,将他们定向到不同的服务器
问题陈述
存在一个包含 3 个核心节点和 1 个只读副本的因果集群设置。有 2 个用户组 - OLTP 用户和 OLAP 用户。OLTP 用户查询应仅发送到 2 个从节点,而不应发送到只读副本。OLAP 用户查询应仅发送到只读副本,而不应发送到任何从节点。原因是 OLTP 用户不应受到 OLAP 用户长时间运行查询的影响。
如何在因果集群设置中实现此功能?
为此,需要多数据中心 (Multi-DC) 设置,此功能在 Neo4j 3.2 版本中引入。首先设置多数据中心许可证。更多信息可从 Neo4j 操作手册中获取 - https://neo4j.ac.cn/docs/operations-manual/current/clustering/causal-clustering/multi-data-center/。
按照 Neo4j 文档中的说明配置包含 3 个核心节点和 1 个只读副本的因果集群 (CC)。然后,在 neo4j.conf
文件中进行相应的更改,以配置哪些应用程序/用户应访问集群的哪些实例。以下配置创建了一个名为 oltp_app
的服务器组,并启用多数据中心许可证,这是服务器组所必需的。
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();
有关上述配置属性的更多文档可在此处查阅:https://neo4j.ac.cn/docs/operations-manual/current/clustering/causal-clustering/multi-data-center/load-balancing/#_prerequisite_configuration
现在,为了仅在只读副本上为 OLAP 用户提供服务,请按如下方式配置只读副本
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 时,由于核心服务器组设置为 oltp_app
,只读查询将仅路由到 2 个从节点。
驱动程序部分提供了路由上下文的文档链接:https://neo4j.ac.cn/docs/developer-manual/current/drivers/client-applications/#_routing_drivers_with_routing_context
多数据中心功能仅在 Neo4j 3.2 及更高版本中可用。 |
此页面有帮助吗?