Spring Cloud Stream: an integrated framework of Spring Cloud GCP

Reading Time: 3 minutes

Spring Cloud GCP

Google Cloud Platform (GCP) provides a cloud computing service that runs the Spring projects in the cloud environment. Spring Cloud GCP project makes the Spring framework a first-class citizen of the Google Cloud Platform. It provides many different libraries which help in using the Google Cloud Platform for applications that are built on Spring Framework.

What is Spring Cloud Stream?

Spring Cloud GCP provides a framework that is Spring Cloud Stream which is used for building highly scalable event-driven microservices connected with shared messaging systems. it works with messaging services like RabbitMQ and Apache Kafka.

RabbitMQ and Apache Kafka are two open-source messaging applications that distribute messages from producers to consumers. They also use the publish-subscribe (pub-sub) mechanism.

The building blocks of Spring Cloud Stream

There are three core blocks in it

  1. Message: It contains the message that the user wants to send and receive according to the pub-sub mechanism. This message helps the producer and the consumer to communicate with the destination.
  2. Destination Binders: It is the most important building block that supports and provides integration with the external messaging system.
  3. Destination Bindings: This block is based upon the simple concept of consuming and producing the message between the two applications.

Basic Concepts of Spring Cloud

  1. Binder: Depending upon the messaging system we chose we will have to specify the messaging platform dependency for it.
  2. Sink: In Spring Cloud Stream, we use the sink to consume the messages from the queue.
  3. Channel: The channel simply represents the input and output pipe between the Spring Cloud Application and the Middleware Platform. A channel is always linked with a queue. If we use this approach we do not need to give the queue any name in the application code. In conclusion, if in any scenario we need to change the queue we do not have to change the code of that application.

Tools Required

  1. Docker
  2. Spring Boot
  3. Spring Cloud Dependency
  4. Maven
  5. JAVA 8 or newer

Dependencies

Maven:-

 <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>spring-cloud-gcp-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency> 

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-stream</artifactId>
    </dependency>

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

Gradle:-

dependencies {
  implementation 'com.google.cloud:spring-cloud-gcp-starter'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencies {
  implementation 'org.springframework.cloud:spring-cloud-stream'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Initializing Spring Cloud GCP Project

We can create a Spring Cloud GCP project with the help of Spring Intializr, it offers various modules that can be used in the project.

  1. GCP Support: Contains auto-configuration support for every Spring Cloud GCP integration.
  2. Cloud Stream: It is a framework that helps in creating event-driven microservices
  3. GCP Messaging: It adds the GCP support entry and all the required dependencies so that the Google Cloud Pub/Sub works out of the box.
  4. GCP Storage: Adds the GCP support entry and all the required dependencies so that Google Cloud Storage integration works out of the box.

Conclusion

We have learned what is Spring Cloud GCP and the basic concept of Spring Cloud Stream. We covered three core blocks and basic concepts. Tools required to begin our project and the dependencies required for the project and how to create a Spring Cloud GCP project using Spring Initializer

Resources

https://docs.spring.io/spring-cloud-gcp/docs/current/reference/html/

https://docs.spring.io/spring-cloud-gcp/docs/current/reference/html/#spring-cloud-stream

knoldus

Written by 

Hey there, I am ambitious and driven. I thrive on challenges and constantly set goals for myself, so I have something to strive towards. I'm not comfortable with settling, and I'm always looking for an opportunity to do better and achieve greatness.

Leave a Reply