Sunday, April 10, 2016

How to Dynamically reload Spring Data Neo4j graph database service with different databases

Leave a Comment

I already configured my project to use one graph database and this is in embedded mode. Here is my configuration class.

@Configuration @EnableNeo4jRepositories(basePackages = "com.comp") @EnableTransactionManagement static class ApplicationConfig {      @Value("${application.neo4j.db.path}")     private String dbPath;      public ApplicationConfig() {     }      @Configuration     static class Neo4jMoreConfig extends Neo4jConfiguration {         Neo4jMoreConfig() {             setBasePackage("com.comp");         }      }      @Bean     public GraphDatabaseService graphDatabaseService() {         return new GraphDatabaseFactory().newEmbeddedDatabase(new File(dbPath));     }  } 

When the application is deploying its creating the database based on the name that I configured in application.yml. But I have a requirement to create multiple databases for different scenarios. For that I need to reload/refresh my graphDatabaseService to include new db path. How can I do this ?

1 Answers

Answers 1

Configuring Spring Data Neo4j 4.1 in an HA Environment

Transaction Binding in HA Mode

A typical Neo4j HA cluster will consist of a master node and a couple of slave nodes for providing failover capability and optionally for handling reads. (Although it is possible to write to slaves, this is uncommon because it requires additional effort to synchronise a slave with the master node) enter image description here

Typical HA Cluster

When operating in HA mode, Neo4j does not make open transactions available across all nodes in the cluster. This means we must bind every request within a specific transaction to the same node in the cluster, or the commit will fail with 404 Not Found.

Read-only Transactions

As of version 4, Spring Data Neo4j does not distinguish between WRITE transactions and READ-ONLY transactions. We cannot therefore bind read-only transactions to slaves and write transactions to master. A future version will address this deficiency, but in the meantime the only way to ensure that everything works as expected is to direct every transaction to master. There are a couple of ways to to achieve this.

Static Binding to a Designated Master

Example cluster:

  1. master: 192.168.0.55
  2. slave1: 192.168.0.56
  3. slave2: 192.168.0.67

SDN4 Binding to master IP address

Components.driver().setURI("http://192.168.0.55:7474"); 

We don’t really recommend this approach, except for testing purposes and non-critical deployments. Firstly, it will only work if you always bring up the designated master first, and secondly, if the master goes down all subsequent transactions will fail until it is restarted. In HA mode, the cluster is able to elect a new master when this happens, but as of version 4 of Spring Data Neo4j, there is no mechanism for querying the cluster to identify the current master. The solution in this case is to use a load balancer such as HAProxy that can do this for us. This is described in the next section.

Dynamic Binding via a Load Balancer

In the Neo4j HA architecture, a cluster is typically fronted by a load balancer. The following example shows how to configure your application and set up HAProxy as a load balancer to route all requests to whichever machine in the cluster is currently identified as the master. Since only one machine can ever be the elected master, this should work exactly as we would like. Furthermore, should the elected master fail, a new server will be elected from the cluster as master and HAProxy will automatically route transactions to this server.

Example cluster fronted by HAProxy

  1. haproxy: 10.0.2.200
  2. neo4j-server1: 10.0.1.10
  3. neo4j-server2: 10.0.1.11
  4. neo4j-server3: 10.0.1.12

Spring Data Neo4j 4 Binding via HAProxy

Components.driver().setURI("http://10.0.2.200"); 

Sample haproxy.cfg

global     daemon     maxconn 256  defaults     mode http     timeout connect 5000ms     timeout client 50000ms     timeout server 50000ms  frontend http-in     bind *:80     default_backend neo4j  backend neo4j     option httpchk GET /db/manage/server/ha/master     server s1 10.0.1.10:7474 maxconn 32     server s2 10.0.1.11:7474 maxconn 32     server s3 10.0.1.12:7474 maxconn 32  listen admin     bind *:8080     stats enable 

Resource Link:

  1. Good Relationships: The Spring Data Neo4j Guide Book

For a full tutorial using java, link is here.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment