Jump to Content
Application Development

Modernize your Java apps with Spring Boot and Spring Cloud GCP

October 28, 2020
Ray Tsang

Developer Advocate

Mike Eltsufin

Software Engineer

It's an exciting time to be a Java developer: there are new Java language features being released every 6 months, new JVM languages like Kotlin, and the shift from traditional monolithic applications to microservices architectures with modern frameworks like Spring Boot. And with Spring Cloud GCP, we’re making it easy for enterprises to modernize existing applications and build cloud-native applications on Google Cloud. 

First released two years ago, Spring Cloud GCP allows Spring Boot applications to easily utilize over a dozen Google Cloud services with idiomatic Spring Boot APIs. This means you don't need to learn a Google Cloud-specific client library, but can still utilize and realize the benefits of the managed services:

  1. If you have an existing Spring Boot application, you can easily migrate to Google Cloud services with little to no code changes.

  2. If you’re writing a new Spring Boot application, you can leverage Google Cloud services with the framework APIs you already know.

Major League Baseball recently started their journey to the cloud with Google Cloud. In addition to modernizing their infrastructure with GKE and Anthos, they are also modernizing with a microservices architecture. Spring Boot is already the standard Java framework within the organization. Spring Cloud GCP allowed MLB to adopt Google Cloud quickly with existing Spring Boot knowledge.

"We use the Spring Cloud GCP to help manage our service account credentials and access to Google Cloud services.” - Joseph Davey, Principal Software Engineer at MLB

Similarly, bol.com, an online retailer, was able to develop their Spring Boot applications on GCP more easily with Spring Cloud GCP.

"[bol.com] heavily builds on top of Spring Boot, but we only have a limited capacity to build our own modules on top of Spring Boot to integrate our Spring Boot applications with GCP. Spring Cloud GCP has taken that burden from us and makes it a lot easier to provide the integration to Google Cloud Platform." - Maurice Zeijen, Software Engineer at bol.com

Developer productivity, with little to no custom code

With Spring Cloud GCP, you can develop a new app, or migrate an existing app, to adopt a fully managed database, create event-driven applications, add distributed tracing and centralized logging, and retrieve secrets—all with little to no custom code or custom infrastructure to maintain. Let’s look at some of the integrations that Spring Cloud GCP brings to the table. 

Data

For a regular RDBMS, like PostgreSQL, MySQL, and MS SQL, you can use Cloud SQL and continue to use Hibernate with Spring Data, and connect to Cloud SQL simply by updating the JDBC configuration. But what about Google Cloud databases like Firestore, Datastore, and the globally-distributed RDBMS Cloud Spanner? Spring Cloud GCP implements all the data abstractions needed so you can continue to use Spring Data, and its data repositories, without having to rewrite your business logic. For example, you can start using Datastore, a fully-managed NoSQL database, just as you would any other database that Spring Data supports.

You can annotate a POJO class with Spring Cloud GCP annotations, similar to how you would annotate Hibernate/JPA classes:

Loading...

Then, rather than implementing your own data access objects, you can extend a Spring Data Repository interface to get full CRUD operations, as well as custom query methods.

Loading...

Spring Data and Spring Cloud GCP automatically implement the CRUD operations and generate the query for you. Best of all, you can use built-in Spring Data features like auditing and capturing data change events.

You can find full samples for Spring Data for Datastore, Firestore, and Spanner on GitHub.

Messaging

For asynchronous message processing and event-driven architectures, rather than manually provision and maintain complicated distributed messaging systems, you can simply use Pub/Sub. By using higher-level abstractions like Spring Integration, or Spring Cloud Streams, you can switch from an on-prem messaging system to Pub/Sub with just a few configuration changes.

For example, by using Spring Integration, you can define a generic business interface that can publish a message, and then configure it to send a message to Pub/Sub:

Loading...

You can consume messages in the same way. The following is an example of using Spring Cloud Stream and the standard Java 8 streaming interface to receive messages from Pub/Sub by simply configuring the application:

Loading...

You can find full samples with Spring Integration and Spring Cloud Stream on GitHub.

Observability

If a user request is processed by multiple microservices and you would like to visualize that whole call stack across microservices, then you can add distributed tracing to your services. On Google Cloud, you can store all the traces in Cloud Trace, so you don't need to manage your own tracing servers and storage.

Simply add the Spring Cloud GCP Trace starter to your dependencies, and all the necessary distributed tracing context (e.g., trace ID, span ID, etc) is captured, propagated, and reported to Cloud Trace.

Loading...

This is it—no custom code required. All the instrumentation and trace capabilities use Spring Cloud Sleuth. Spring Cloud GCP supports all of Spring Cloud Sleuth’s features, so distributed tracing is automatically integrated with Spring MVC, WebFlux, RestTemplate, Spring Integration, and more.

https://storage.googleapis.com/gweb-cloudblog-publish/images/trace_waterfall_view_eh7NQIx.max-1200x1200.jpg

Cloud Trace generates a distributed trace graph. But notice the "Show Logs" checkbox. This Trace/Log correlation feature can associate log messages to each trace so you can see the logs associated with a request to isolate issues. You can use Spring Cloud GCP Logging starter and its predefined logging configuration to automatically produce the log entry with the trace correlation data.

Loading...

You can find full samples with Logging and Trace on GitHub.

Secrets

Your microservice may also need access to secrets, such as database passwords or other credentials. Traditionally, credentials may be stored in a secret store like HashiCorp Vault. While you can continue to use Vault on Google Cloud, Google Cloud also provides the Secret Manager service for this purpose. Simply add the Spring Cloud GCP Secret Manager starter so that you can start referring to the secret values using standard Spring properties:

Loading...

In the applications.properties file, you can refer to the secret values using a special property syntax:

Loading...

You can find a full sample with Secret Manager on GitHub.

More in the works, in open source

Spring Cloud GCP closely follows the Spring Boot and Spring Cloud release trains. Currently, Spring Cloud GCP 1.2.5 works with Spring Boot 2.3 and Spring Cloud Hoxton release train. Spring Cloud GCP 2.0 is on its way and it will support Spring Boot 2.4 and the Spring Cloud Ilford release train.

In addition to core Spring Boot and Spring Cloud integrations, the team has been busy developing new components to meet developers’ needs:

Developer success is important to us. We’d love to hear your feedback, feature requests, and issues on GitHub, so we can understand your needs and prioritize our development work. 

Try it out!

Want to see everything in action? Check out the Developer Hands-on Keynote from Google Cloud Next ‘20: On Air, where Daniel Zou shows how to leverage Spring Boot and Spring Cloud GCP when modernizing your application with Anthos, Service Mesh, and more:

Video Thumbnail

You can also easily try Spring Cloud GCP with many samples. Or, you can take the guided Spring Boot on GCP course on Qwiklab or Coursera. Last but not least, you can find out about detailed features and configurations in the reference documentation.

Posted in