In this blog post, I'll discuss the process of building a microservice that is backed by a graph database and the technologies leveraged to accomplish it.
Choosing the Technologies
When building a microservice, I recommend trying to follow the tenets of The Twelve-Factor App.
With those tenets in mind, I'll be building this microservice in Java using Maven for its declarative dependency management and build process and Dropwizard for its straightforward architecture and configuration. Dropwizard offers great facilities for being able to monitor your application as well as functionality to run one-off tasks where the source for those tasks can be bundled with your service application code.
Getting Started With Dropwizard
One of the selling points of Dropwizard is that it's not a heavyweight framework. Its a collection of some of the best Java libraries glued together to form an easy to use REST service. To get started, let's create a Maven pom file and the typical project directory structure.
To run a Dropwizard application, we need to have an implementation of an io.dropwizard.Configuration that represents the single yml config file required by Dropwizard, an implementation of an io.dropwizard.Application, and a yml config file.
By default, the base Dropwizard configuration class has all of the necessary defaults for us to be able to run the Dropwizard application. Now that we have all the required files necessary to run, let's include a Maven exec directive to launch the application.
Now, let's run the application: mvn exec:java.
At this point, the application should be up and running without much to show for it other than some default administrative resources that Dropwizard includes. Those resources should be available by navigating to http://localhost:8081/ in your browser. One of these operational resources is the application healthchecks. This resource can be utilized to monitor different components of your application and can be used to provide alerts when portions of your system are not functioning properly.
Connecting to the Graph Database
Now that we have our application up and running, let's get it hooked up to our JanusGraph database. To do this, we'll utilize Expero's dropwizard-tinkerpop library.
This library provides all of the necessary configuration parameters required to connect a Dropwizard application to an Apache TinkerPop enabled graph database. Additionally, it includes a healthcheck to verify the application's ability to connect to the graph database as well as hooks into Dropwizard's lifecycle management to properly shut down connections when the application halts.
Let's update our applications configuration to support the connection.
Now let's update our yml file to include the configuration to connect to a locally running instance.
As we're using JanusGraph and it has a custom format for edge serialization, we'll need to include the janusgraph-core dependency in our application as well.
Now, in our application startup, let's build a Cluster connection using the TinkerPopFactory included in the configuration. Additionally, let's build a GraphTraversalSource to support executing gremlin traversals against the graph.
If you startup the application now, and navigate to the healthcheck page, you'll notice there is now an entry for the tinker pop server connection.
At this point, we can begin creating API's that utilize the graph database. In part 2 of the series, we'll look into packaging our application into a docker image to simplify our deployments.