Getting started with Spring Cloud

tudip-logo

Tudip

05 August 2019

Why Spring Cloud?

Spring Cloud provides an efficient way for developers to quickly build a number of the common patterns in distributed systems. The interaction between distributed systems leads to boilerplate patterns. Developers will quickly rise up services and applications that implement those patterns. It helps the work well in any distributed surroundings and their own system.

What are the Components:

Spring Cloud focuses on the following components

  1. Configuration
  2. Service Discovery
  3. Circuit Breakers.
  4. Routing and messaging
  5. API Gateway
  6. Tracing
  7. CI Pipelines and Testing.

Let’s have a look on the spring cloud components in detail.

Configuration

  1. In Spring boot application we have application.properties file or application.yml for configuration which is typically in our application.
  2. But in a cloud-native application is better to take configuration outside of the application itself and put it in some kind of centralized store.
  3. For ex. We want just to change one property and don’t have to change the code, application.properties, build the application and then go to the deployment pipeline resulting in downtime just to do that. So having configuration on Config Server will eliminate that problem.
  4. Spring provides the facility to manage the configuration between these different environments.
  5. Spring cloud config uses Git, SVN, vault and even properties to store config.

Service Discovery

  1. The cloud-native application depends on the URL which can be problematic because of its dynamic nature.
  2. The cloud-native application comprises of a bunch of microservices which are going to communicate with each other which may be running on the different instances and data centers in the different regions.
  3. The URL may change hour-hour or day-day due to its dynamic nature.
  4. Service discovery helps to figure out all the components of our cloud-native application
    •  what the current URL of the service
    •  How many instances are available.
    •  What is the status and health and use that information to make a request to the service?
  5.  The could-native support couple of backend for service discovery like
    •  Netflix Eureka.
    •  Zookeeper
    •  Consul

Routing and messaging

  1. As the Native cloud application will be composed of many microservices
  2. Which will be communicating with each other. These requests can be made using HTTP request or via Async messaging protocol as well.
  3. Here the microservices will talk to Service Registry to figure out where is the service located which we need to make a request and use some kind of routing and message to call request.
  4. For Routing and Load Balancing, spring cloud provides open-source Netflix ribbon and Open Feign.
  5. For Messaging support it provides RabbitMQ or Kafka.

API Gateway

  1. Whether Public or internal clients making a request to your cloud-native application.
    We need an API client to help those APIs with microservices in our cloud-native application.
  2. Because of the dynamic nature set of microservices, today may be different tomorrow.
  3. Netflix Zuul provides leverage service discovery and load balancer.
  4. Spring Cloud Gateway is another project by Spring Team for API Gateway.

Tracing

  1. Tracing is a very useful feature in the application which runs on Spring Cloud.
  2.  A single request to the data from our application which may include a larger number of the request to the various microservices.
  3. Tracing these requests is a very critical task while debugging issues.
  4. Spring Cloud Sleuth and Zepkin for tracing the request.

CI Pipelines and Testing

  1. Building, Testing and deploying various services is critical to having successful
  2. Cloud native application.
  3. Spring Cloud  Pipelines is an opinionated pipeline for Jenkins that will automatically create pipelines for your application.
  4. Spring cloud contract allows you to accurately mock dependencies between services using the published contracts.

Architecture System

MicroserviceArchitecture

  1. When the user requests a service, The API Gateway will identify which microservice to call based on the API request.
  2. It helps to identify the services which are registered using the service registry.
  3. Load balancer helps to manage the service request for the microservices.
  4. With the help of Config server, it collects all the config details stored in Git or file.

 What are dependencies are required to build a cloud-native application?

SpringCloudDependencies-1

  1. Spring cloud starter-config
  2. Spring cloud starter-eureka
  3. Spring cloud starter-sleuth
  4. Spring cloud starter zipkin
  5. Spring cloud starter-web

Let’s have a quick look on the annotation used and application below:

@SpringCloudApplication replaces @SpringBootApplication this new annotation extends the @SpringBootApplication which provides same features like autoconfiguration, component

scanning, and property support. Along with that it also adds @EnableDiscoveryClient and @EnableCirciutBreaker

@EnableEurekaClient and @EnableDiscoveryClient annotation are available.

DiscoveryClient is an abstract interface which is on the top of Eureka Client.

  1. Greeting Service.
    Annotations
  2. Name Service.
    NameService
  3. Web Application.

    1. @EnableFeignClient service is used to create a Ribbon load balancer.

    It helps to filter the requests through our proxy service.

    Zuul has four standard filter types and filters store request and state information

    It is used so you can proxy and filter requests for your microservices.

    1.   @EnableZuulProxy

    It helps to filter the requests through our proxy service.

    Zuul has four standard filter types and filters store request and state information

    It is used so you can proxy and filter requests for your microservices.


    ZuulFiegn
    Gateway Application:
    GatewayConfig-1

    Spring Cloud Config Server:

    • All the applications need to add the spring-cloud-starter-config dependency to enable the config client.
    • Which run on the port:8888.
    • Config server will look into the application.yml file or application.properties file in the Git. @EnableConfigServer launches an embedded Spring Cloud Config Server.
    • For greeting service, config will look for greeting and similarly for name and web.

    For example: greeting service application.yml file will look like:

    spring:
    
     application:
    
       name: greeting
    

    Spring Cloud Eureka Server:

    • To register with the application with the embedded server provider by default by spring cloud.
    • We need to add the dependency of spring-cloud-starter-netflix-eureka-server.
    • By default, it runs on port: 8761

    An application running that are currently registered with the Eureka Server.

    EurekaInstances

Request a quote