Beliebte Suchanfragen
//

RESTful Microservices on the Google Cloud Platform

8.4.2018 | 3 minutes of reading time

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.

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:

1@Data
2@Document(collection = "pois")
3public class PointOfInterest {
4 
5    @Id private String id;
6 
7    private String name;
8 
9    @Field("desc") private String description;
10 
11    private GeoJsonPoint location;
12}

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:

1spring:
2  data:
3    mongodb:
4      uri: mongodb://localhost:27017/test
5    rest:
6      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:

1public interface PointOfInterestRepository 
2   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:

1@RestResource(path="/pois")
2public interface PointOfInterestRepository 
3   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:

1$ gcloud compute instances list --filter="name:('mongo*')"
2NAME                   ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
3mongo-0-arbiters-vm-0  europe-west3-b  n1-standard-1               10.156.0.2   35.198.163.26  RUNNING
4mongo-0-servers-vm-0   europe-west3-b  n1-highmem-4                10.156.0.4   35.198.73.31   RUNNING
5mongo-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:

1mongodb://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):

1env_variables:
2  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:

1$ curl https://sb-gcp-dot-[PROJECT_ID].appspot.com/api/pois
2{
3  "_embedded" : {
4    "pointOfInterests" : [ {
5      "name" : "Unperfekthaus",
6      "description" : "Im Unperfekthaus bekommen Künstler ...",
7      "location" : {
8        "x" : 7.111941,
9        "y" : 51.430811,      
10        "type" : "Point",
11        "coordinates" : [ 7.0075, 51.45902 ]
12      },
13      ...
14}

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 .

share post

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.