如何将Spring启动应用程序与Neo4J HA Cluster集成?

时间:2018-02-18 12:32:56

标签: neo4j spring-data-neo4j

这是我当前的配置类。我正在使用本地neo4j实例。我想更改配置以连接到neo4j HA群集。我知道集群设施在neo4j企业版中可用。

@Configuration
@EnableTransactionManagement
@ComponentScan("io.sample")
@PropertySource("classpath:neo4j.properties")
public class PersistentContext {

   @Value("${neo4j.driverClassName}")
   private String driverClassName;

   @Value("${neo4j.connectionUrl}")
   private String connectionUrl;

   @Value("${neo4j.username}")
   private String username;

   @Value("${neo4j.password}")
   private String password;

   /**
     * Creates session factory
     * 
     * @return {@link SessionFactory} neo4j session factory
   */
   @Bean
   public SessionFactory getSessionFactory() {
      return new SessionFactory(configuration(), 
     "io.sample.model");
   }

   /**
    * Creates neo4j transcationManager by passing session factory
    * 
    * @return {@link Neo4jTransactionManager} transaction manager for 
      neo4j
    * @throws Exception
    *             when failed to create neo4j transaction manager
   */
   @Bean
   public Neo4jTransactionManager transactionManager() throws 
   Exception {
    return new Neo4jTransactionManager(getSessionFactory());
   }

   /**
    * Creates configuration by setting driverClass, connectionUrl and
    * credentials
    * 
    * @return {@link org.neo4j.ogm.config.Configuration} instance for 
      neo4j
    *         configuration
    */
    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        org.neo4j.ogm.config.Configuration configuration = new 
        org.neo4j.ogm.config.Configuration();
       configuration.driverConfiguration()
      .setDriverClassName(driverClassName)
      .setURI(connectionUrl).setCredentials(username, password);
       return configuration;
    }
 }

neo4j.properties文件如下: -

neo4j.driverClassName=org.neo4j.ogm.drivers.http.driver.HttpDriver
neo4j.connectionUrl=http://localhost:7474
neo4j.username=neo4j
neo4j.password=root

1 个答案:

答案 0 :(得分:0)

它应该开箱即用,但......(稍后)

例如,您可以创建三个Neo4j实例:neo4j-machine1neo4j-machine2neo4j-machine3并根据in the docs解释设置必要的配置。使用此配置,您仍然可以通过HTTP连接到其中一台计算机,它将负责分发请求。

现在"但是":如果此计算机脱机,您将无法再访问群集。更好的解决方案是在您的网络中引入代理服务器。有一个配置HAProxy in the OGM docs的例子。