首页 > 解决方案 > 如何使用 java api 获取 Neo4j 集群状态

问题描述

我正在尝试使用 java API 查找 neo4j 集群运行状况。我看到 CLI CALL dbms.cluster.overview() 是否有任何等效的 java api

标签: neo4jjava

解决方案


1. 变体“Spring Boot”

如果您可以选择带有 Spring Data Neo4J 的 Spring Boot,您可以定义一个 DAO,它执行您的 cypher 语句并在自己的 QueryResult 类中接收结果。

1.1 一般查询DAO

@Repository
public interface GeneralQueriesDAO extends Neo4jRepository<String, Long> {    
    @Query("CALL dbms.cluster.overview() YIELD id, addresses, role, groups, database;")
    ClusterOverviewResult[] getClusterOverview();
}

1.2 ClusterOverviewResult

@QueryResult
public class ClusterOverviewResult {
    private String id;  // This is id of the instance.
    private List<String> addresses; // This is a list of all the addresses for the instance.
    private String role; // This is the role of the instance, which can be LEADER, FOLLOWER, or READ_REPLICA.
    private List<String> groups; // This is a list of all the server groups which an instance is part of.
    private String database; // This is the name of the database which the instance is hosting.

    // omitted default constructor as well getter and setter for clarity    
} 

1.3 程序流程

@Autowired
private GeneralQueriesDAO generalQueriesDAO;

[...]

ClusterOverviewResult[] clusterOverviewResult = generalQueriesDAO.getClusterOverview();

2.变体“没有弹簧”

如果没有 Spring Boot,粗略的过程可能是:

Session session = driver.session();
StatementResult result = session.run("Cypher statement");

3.变体“HTTP端点”

另一种选择是使用HTTP 端点来监控 Neo4j Causal Cluster 的健康状况


推荐阅读