You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot连接Neo4j 3节点集群实现高可用配置求助

Spring Boot连接Neo4j集群实现高可用的正确配置

错误原因

你之前的配置将多个节点地址用逗号拼接在单个neo4j:// URI中,Neo4j Java Driver会把整个字符串识别为单一节点地址,导致格式解析失败,触发Invalid address format错误。

解决方案

方案1:使用Spring Data Neo4j标准多节点配置(推荐)

如果你的应用基于Spring Data Neo4j,直接使用uris(复数)配置项指定多个集群节点地址,Spring Boot会自动解析为节点列表并创建支持故障转移的Driver实例:

spring:
  neo4j:
    uris:
      - neo4j://10.253.229.110:7687
      - neo4j://10.253.228.184:7687
      - neo4j://10.252.21.127:7687
    authentication:
      username: your-username
      password: your-password

若需结合环境变量占位符,可采用逗号分隔带完整前缀的地址:

spring:
  neo4j:
    uris: "${GRAPH_CLUSTER_URIS:neo4j://10.253.229.110:7687,neo4j://10.253.228.184:7687,neo4j://10.252.21.127:7687}"
    authentication:
      username: your-username
      password: your-password

方案2:手动创建Neo4j Driver实例(自定义配置场景)

如果你的应用手动管理Neo4j Driver,需传入多个ServerAddress对象构建Driver,而非单个拼接字符串:

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.ServerAddress;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;

@Configuration
public class Neo4jConfig {

    @Bean
    public Driver neo4jDriver() {
        List<ServerAddress> clusterAddresses = List.of(
            ServerAddress.of("10.253.229.110", 7687),
            ServerAddress.of("10.253.228.184", 7687),
            ServerAddress.of("10.252.21.127", 7687)
        );
        return GraphDatabase.driver(clusterAddresses, AuthTokens.basic("your-username", "your-password"));
    }
}

额外注意事项

  • 确保Neo4j集群已配置为因果集群或高可用模式,节点间通信正常。
  • 使用与Neo4j集群版本兼容的Java Driver稳定版,新版本对集群地址解析和故障转移的支持更完善。
  • 若使用neo4j://协议,驱动可通过初始节点自动发现整个集群,因此最少配置2个节点即可实现高可用,避免单点故障。

内容的提问来源于stack exchange,提问作者Arjun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 03:23:19