Spring Boot microservices with logging using Spring Cloud GCP

Reading Time: 4 minutes

What is Spring Boot and Spring Cloud GCP?

Spring Boot: Spring Boot is a java-based framework i.e. free and open-source. 

Using Spring boot you can create java (web)apps mostly based on microservices 

architecture. It makes it easier for you to focus on development work by simply 

providing default dependencies that you may require in your project (it takes care of 

boiler-plate code).

Spring Cloud GCP: Spring cloud GCP provides various libraries, these libraries (in 

turn) make it easier for you to use GCP (Google Cloud Platform) through applications 

that are built using Spring (a java framework). Some of the best integrated features of 

GCP include Cloud Firestore, Cloud Spanner, Cloud DataStore, Cloud Pub/Sub, logging 

etc.

What are Microservices?

Microservices are written separately (micro means small and service here means the software i.e. it will perform the task needed and it will obviously making use of microservices so in essence micro-services work together when they are combined will do the required task ) and will help us to achieve a defined goal. Spring uses microservices architecture. The main advantages of using 

micro-services is that:

  • Developers can work independently on each microservice (loosely coupled)
  • If a micro-service is down that won’t make the whole (web)app go down.
  • Make the testing and bug fixing easier (unit-testing will enable developers to find bugs at primary level)

Note: From security perspective micro-services architecture may not be the best choice 

you can have (eg. Apache Log4J vulnerability)

Steps to use Spring Boot microservices using Spring Cloud GCP:

Step 1: Hit the Spring Initializer website i.e. https://start.spring.io/. Choose the 

configurations and dependencies according to the project requirements.

Step 2: Make sure you have chosen “GCP Support” as one the dependencies in your 

Project.

Step 3: Generate, download(will be downloaded automatically), extract (if necessary, 

depends on the IDE), open the project in your favorite IDE.

Step 4: Include this dependency into pom.xml file under the project folder.

<dependency>

    <groupId>com.google.cloud</groupId>

    <artifactId>spring-cloud-gcp-starter-data-spanner</artifactId>

</dependency>

Step 5: After that, introduce the gcp-starter-trace dependency under dependencyManagement(inside pom.xml) (if it is not present there already):

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-gcp-starter-trace</artifactId>

<version>3.2.1</version>

</dependency>

Step 6: Don’t forget to use the cloud spanner friendly Spring notations. In application.properties file under resources, 

you can add spanner instance id and database. Use prebuilt @RepositoryRestResource to mimic the 

behaviour of REST APIs. Your application.properties file should look like this:

spring.cloud.gcp.spanner.instance-id=sample

spring.cloud.gcp.spanner.database=demo

Step 7: Your main java file should look like this:

part-1

part-2

Step 8: Run your Spring boot application.

Setting up logging using Spring Cloud GCP:

Note: Make sure you have completed all the previous steps.

Step 1: Include the following dependency into the pom.xml under 

dependencyManagement:

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-gcp-starter-logging -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-gcp-starter-logging</artifactId>

<version>1.0.0.M3</version>

</dependency>

Step 2: Also add the following piece of code into {Project Name}>src>resources>logback-spring.xml :

<configuration>

<include resource=”org/springframework/boot/logging/logback/defaults.xml" />

<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<springProfile name="logging-json">

<include resource="org/springframework/cloud/gcp/logging/logback-json-appender.xml"/>

<root level="INFO">

<appender-ref ref="CONSOLE_JSON"/>

</root>

</springProfile>

<springProfile name="logging-api">

<include resource="org/springframework/cloud/gcp/logging/logback-appender.xml"/>

<root level="INFO">

<appender-ref ref="STACKDRIVER"/>

</root>

</springProfile>

<springProfile name="logging-console | default">

<root level="INFO">

<appender-ref ref="CONSOLE"/>

</root>

</springProfile>

</configuration>

Step 3: Log into your GCP account and you should see the logs coming into cloud stack-trace.

Summary:

  • In this blog you learnt about Spring Boot, Spring Cloud GCP. We touched briefly on micro-services.
  • You learnt how to configure & use Spring Boot microservices with Spring Cloud GCP.
  • In the end, we also got to know about the logging set-up using Spring Cloud GCP.

Conclusion:

  • To make use of advantages provided by microservices architecture and Spring boot on Cloud we can use Spring Cloud GCP.
  • Running your spring boot application on Google Cloud along with logging capabilities is not that tedious task.
  • For more blogs on cutting-edge technologies. Please visit: knoldus blogs.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading