RESTful Microservices on the Google Cloud Platform

No Comments

This tutorial shows you how to develop a RESTful microservice running on the Google Cloud Platform.

I already explained how to deploy Spring Boot applications to the AppEngine and how to set up a MongoDB replica set in the Compute Engine. Today you will learn how to expose data from the MongoDB backend with a REST endpoint implemented with Spring Boot.

System Overview

Our application will be deployed to the AppEngine flex environment while the MongoDB backend is running in the Compute Engine.

AppEngine Microservice

Since applications deployed to the flex environment are inside the same network as the instances in the Compute Engine, the MongoDB replica set members can be reached by the internal IP addresses.

RESTful Microservice

We will build an application that implements a RESTful API for data from a MongoDB replica set. With Spring Boot and the starters for Spring Data MongoDB and Spring Data REST, this will be quite easy and we can focus on the business domain rather than dealing with technical aspects.

Our domain object PointOfInterest holds information on interesting places stored in the collection pois and looks like this:

@Data
@Document(collection = "pois")
public class PointOfInterest {
 
	@Id private String id;
 
	private String name;
 
	@Field("desc") private String description;
 
	private GeoJsonPoint location;
}

The @Data annotation from Project Lombok helps us reduce writing boilerplate code for getters and setters. The other annotations are from the Spring Data MongoDB API and they map the class to a collection, mark the primary key field and map another field. For more details you may read my introduction to Spring Data MongoDB.

To access a MongoDB server, we use these properties in our application.yaml file:

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/test
    rest:
      base-path: /api

This points to a single MongoDB instance on our local machine and uses the database test.

With Spring Data MongoDB, writing a repository for our domain object becomes easy like this:

public interface PointOfInterestRepository 
   extends MongoRepository<PointOfInterest, String> {}

This already provides all CRUD operations and support for pagination.

Spring Data REST finally helps us expose this repository via a REST API using the HAL media type with a single additional annotation:

@RestResource(path="/pois")
public interface PointOfInterestRepository 
   extends MongoRepository<PointOfInterest, String> {}

Given the base path /api (as defined in application.yml, see above) for the REST API, we can access our points of interest locally at this URL:

$curl http://localhost:8080/api/pois

AppEngine Configuration

In my preivous post I explained how to deploy Spring Boot applications to the AppEngine flex environment. We just need a single line of configuration in order to configure our application to use the MongoDB replica set inside the Compute Engine.

First we need to lookup the internal IP addresses of the MongoDB instances:

$ gcloud compute instances list --filter="name:('mongo*')"
NAME                   ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
mongo-0-arbiters-vm-0  europe-west3-b  n1-standard-1               10.156.0.2   35.198.163.26  RUNNING
mongo-0-servers-vm-0   europe-west3-b  n1-highmem-4                10.156.0.4   35.198.73.31   RUNNING
mongo-0-servers-vm-1   europe-west3-b  n1-highmem-4                10.156.0.3   35.198.79.23   RUNNING

These are the instances we set up before. Now the connection string for the MongoDB replica set looks like this:

mongodb://10.156.0.3:27017,10.156.0.6:27017,10.156.0.5:27017/test?replicaSet=rs0

In order to set a different value for the corresponding Spring Boot parameter spring.data.mongodb.uri, we benefit from the environment awareness of Spring Boot and define an environment variable in app.yaml (the deployment descriptor for AppEngine applications):

env_variables:
  SPRING_DATA_MONGODB_URI: mongodb://10.156.0.3:27017,10.156.0.6:27017,10.156.0.5:27017/test?replicaSet=rs0

This will overwrite the MongoDB URI defined in the standard Spring Boot configuration resource application.yml.

After deploying the application, we can finally test the REST API:

$ curl https://sb-gcp-dot-[PROJECT_ID].appspot.com/api/pois
{
  "_embedded" : {
    "pointOfInterests" : [ {
      "name" : "Unperfekthaus",
      "description" : "Im Unperfekthaus bekommen Künstler ...",
      "location" : {
        "x" : 7.111941,
        "y" : 51.430811,      
        "type" : "Point",
        "coordinates" : [ 7.0075, 51.45902 ]
      },
      ...
}

Summary

I showed you how easy you can implement a RESTful microservice with Spring Boot and how to configure it to access a backend in the Compute Engine.

The full source code of this example application can be found at my GitHub repository ttrelle/sb-gcp/rest-api-mongodb.

Tobias Trelle

Dipl.-Math. Tobias Trelle is a Senior IT Consultant at codecentric AG in Solingen/Germany. He’s into IT business for nearly 20 years and is interested in software architecture and scalability. Tobias gives talks at conferences and meetups and is the author of the German book “MongoDB: Der praktische Einstieg”.

Comment

Your email address will not be published. Required fields are marked *