Neo4jServer in Neo4jConfiguration - 4.1.0?

2019-08-10 18:33发布

问题:

I've been using the latest code in 4.1.0-BUILD-SNAPSHOT as I need some of the new bug fixes in the 4.1 branch and just noticed that "neo4jServer()" is no longer a method exposed by Neo4jConfiguration. What is the new way to initialize a server connection and an in-memory version for unit tests? Before I was using "RemoteServer" and "InProcessServer", respectively.

回答1:

Please note, the official documentation will be updated shortly.

In the meantime:

What's changed

SDN 4.1 uses the new Neo4j OGM 2.0 libraries. OGM 2.0 introduces API changes, largely due to the addition of support for Embedded as well as Remote Neo4j. Consequently, connection to a production database is now accomplished using an appropriate Driver, rather than using the RemoteServer or the InProcessServer which are deprecated.

For testing, we recommend using the EmbeddedDriver. It is still possible to create an in-memory test server, but that is not covered in this answer.

Available Drivers

The following Driver implementations are currently provided

  • http : org.neo4j.drivers.http.driver.HttpDriver
  • embedded : org.neo4j.drivers.embedded.driver.EmbeddedDriver

A driver implementation for the Bolt protocol (Neo4j 3.0) will be available soon.

Configuring a driver

There are two ways to configure a driver - using a properties file or via Java configuration. Variations on these themes exist (particularly for passing credentials), but for now the following should get you going:


Configuring the Http Driver

The Http Driver connects to and communicates with a Neo4j server over Http. An Http Driver must be used if your application is running in client-server mode. Please note the Http Driver will attempt to connect to a server running in a separate process. It can't be used for spinning up an in-process server.

Properties file configuration:

The advantage of using a properties file is that it requires no changes to your Spring configuration.

Create a file called ogm.properties somewhere on your classpath. It should contain the following entries:

  driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
  URI=http://user:password@localhost:7474

Java configuration:

The simplest way to configure the Driver is to create a Configuration bean and pass it as the first argument to the SessionFactory constructor in your Spring configuration:

import org.neo4j.ogm.config.Configuration;
...

@Bean 
public Configuration getConfiguration() {
   Configuration config = new Configuration();
   config
       .driverConfiguration()
       .setDriverClassName
        ("org.neo4j.ogm.drivers.http.driver.HttpDriver")
       .setURI("http://user:password@localhost:7474");
   return config;
}

@Bean 
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), <packages> );
}


Configuring the Embedded Driver

The Embedded Driver connects directly to the Neo4j database engine. There is no server involved, therefore no network overhead between your application code and the database. You should use the Embedded driver if you don't want to use a client-server model, or if your application is running as a Neo4j Unmanaged Extension.

You can specify a permanent data store location to provide durability of your data after your application shuts down, or you can use an impermanent data store, which will only exist while your application is running (ideal for testing).

Create a file called ogm.properties somewhere on your classpath. It should contain the following entries:

Properties file configuration (permanent data store)

driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver
URI=file:///var/tmp/graph.db

Properties file configuration (impermanent data store)

driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver

To use an impermanent data store, simply omit the URI property.

Java Configuration

The same technique is used for configuring the Embedded driver as for the Http Driver. Set up a Configuration bean and pass it as the first argument to the SessionFactory constructor:

import org.neo4j.ogm.config.Configuration;
...

@Bean 
public Configuration getConfiguration() {
   Configuration config = new Configuration();
   config
       .driverConfiguration()
       .setDriverClassName
        ("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
       .setURI("file:///var/tmp/graph.db");
   return config;
}

@Bean 
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), <packages> );
}

If you want to use an impermanent data store (e.g. for testing) do not set the URI attribute on the Configuration:

@Bean 
public Configuration getConfiguration() {
   Configuration config = new Configuration();
   config
       .driverConfiguration()
       .setDriverClassName
        ("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
   return config;
}