Reader small image

You're reading from  Mastering Spring Cloud

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788475433
Edition1st Edition
Languages
Right arrow
Author (1)
Piotr Mińkowski
Piotr Mińkowski
author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski

Right arrow

Chapter 3. Spring Cloud Overview

In Chapter 1, Introduction to Microservices, I mentioned the cloud-native development style and also that Spring Cloud helps you in the easy adoption of the best practices associated with this concept. The most commonly used best practices have been collected together in an interesting initiative called The Twelve-Factor App. As you might read on their website (https://12factor.net/), this is a methodology for building Software as a Service (SaaS) modern applications, which must be scalable, easily deployable on cloud platforms, and delivered in the continuous deployment process. It is worth being familiar with these principles, especially if you are a developer who builds applications running as a service. Spring Boot and Spring Cloud provide features and components that make your application compliant with Twelve-Factor rules. We can distinguish some typical features that the most modern distributed systems usually use. Every opinionated framework should...

Beginning with the basics


Let's go back to the previous chapter for a moment. There I have already described in detail the structure of a Spring Boot project. Configuration should be provided in YAML or a properties file with the application or the application-{profile} name. In contrast to a standard Spring Boot application, Spring Cloud is based on the configuration taken from a remote server. However, minimal settings are needed inside the application; for example, its name and config server address. That's why a Spring Cloud application creates a bootstrap context, which is responsible for loading properties from the external sources. Bootstrap properties are added with the highest priority and they cannot be overridden by local configuration. Bootstrap context, which is a parent for the main application context, uses bootstrap.yml instead of application.yml. Usually, we put the application name and Spring Cloud Config settings, as follows:

spring:
  application:
    name: person-service...

Discovery and distributed configuration


Service discovery and distributed configuration management are vital parts of the microservices architecture. The technical implementation of these two different mechanisms is pretty similar. It comes down to storing parameters under specific keys in a flexible key-value storage. Actually, there are several interesting solutions available on the market which provide both of these functionalities. Spring Cloud integrates with the most popular of them. But there is also one exception where Spring Cloud has its own implementation created only for distributed configuration. This feature is available under the Spring Cloud Config project. In contrast, Spring Cloud does not provide its own implementation for service registration and discovery.

As usual, we can divide this project into the server and client-side support. The server is the one, central place where all of the external properties for applications are managed across all of the environments. Configuration...

Distributed tracing with Sleuth


Another one of Spring Cloud's essential functionalities is distributed tracing. It is implemented in the Spring Cloud Sleuth library. Its primary purpose is to associate subsequent requests dispatched between different microservices under processing single input request. As in most cases, these are HTTP requests that implement tracing mechanisms based on HTTP headers. The implementation is built over Slf4j and MDC. Slf4j provides facade and abstraction for specific logging frameworks such as logback, log4j, or java.util.logging. MDC or mapped diagnostic context in full, is a solution for distinguishing log output from different sources and enriching them with additional information that could be not available in the actual scope.

Spring Cloud Sleuth adds trace and span IDs to the Slf4J MDC, so that we are able to extract all of the logs with a given trace or span. It also adds some other entries such as application name or exportable flag. It integrates with...

Messaging and integration


I have already mentioned messaging brokers and their usage for communication between your application and Zipkin server. Generally, Spring Cloud supports two types of communications via synchronous/asynchronous HTTP and with messaging brokers. The first project from this area is Spring Cloud Bus. It allows you to send broadcast events to applications informing them about state changes such as configuration property updates or other management commands. Actually, we might want to use starters for AMQP with a RabbitMQ broker or for Apache Kafka. As usual, we only need to include spring-cloud-starter-bus-amqp or spring-cloud-starter-bus-kafka to the dependency management and all other necessary operations are performed through auto-configuration. 

Spring Cloud Bus is a rather small project allowing you to use distributed messaging features for common operations such as broadcasting configuration change events. The right framework for building a system consisting of...

Cloud platform support


Pivotal Cloud Foundry is a cloud-native platform for deploying and managing modern applications. Pivotal Software, as some of you probably already know, is an owner of the Spring framework trademark. The patronage of a large, commercial platform is one of the important reasons for Spring's growing popularity. What is obvious is that PCF fully supports both Spring Boot's executable JAR files, and all of Spring Cloud microservices patterns such as Config Server, service registry, and circuit breaker. These types of tools can be easily run and configured using the marketplace available on the UI dashboard or client command line. Development for PCF is even simpler than with standard Spring Cloud application. The only thing we have to do is to include the right starters to project dependencies: 

  • spring-cloud-services-starter-circuit-breaker
  • spring-cloud-services-starter-config-client
  • spring-cloud-services-starter-service-registry

It's difficult to find an opinionated cloud...

Other useful libraries


There are some important aspects surrounding microservices architecture, which can't be considered its core features, but are also very important. The first of them is security.

Security

The big part of standard implementation for securing APIs with mechanisms such as OAuth2, JWT, or basic authentication is available in Spring Security and Spring Web projects. Spring Cloud Security uses those libraries to allow us to easily create systems that implement common patterns such as single sign-on and token relay. To enable security management for our application we should include the spring-cloud-starter-security starter.

Automated testing

The next important area in microservices development is automated testing. For microservices architecture, contact tests are growing in importance. Martin Fowler gave the following definition:

"An integration contract test is a test at the boundary of an external service verifying that it meets the contract expected by a consuming service...

Projects overview 


As you can see, Spring Cloud contains many subprojects providing integration with lots of different tools and solutions. I think it is easy to lose track, especially if you are using Spring Cloud for the first time. In accordance with the principle that one diagram might express things better than a thousand words, I'm presenting the most important projects divided into categories as shown in the following diagram:

Release trains


As we can see in the preceding diagram, there are many projects inside Spring Cloud and there are many relationships between them. By definition, these are all independent projects with different release cascades and version numbers. In a situation like this, dependency management in our application might be problematic and that will require knowledge about relationships between versions of all projects. To help make it easier, Spring Cloud introduced the starter mechanism, which we have already discussed, and release trains. The release trains are identified by names, not versions, to avoid confusion with the subprojects. What is interesting is that they are named after London tube stations and they are alphabetically ordered. The first release was Angel,  the second was Brixton, and so on. The whole mechanism of dependency management is based on BOM (bill of materials), which is a standard Maven concept for managing artifacts versioned independently. Here's an actual table...

Summary


In this chapter, I have introduced the most important projects that are part of Spring Cloud. I have pointed out several areas to which I assigned each of those projects. After reading this chapter, you should be able to recognize which library has to be included in your application to able to implement patterns such as service discovery, distributed configuration, circuit breaker, or load balancer. You should also recognize the differences between application context, and bootstrap context and understand how to include dependencies in the project using dependency management based on the release trains concept. The last thing I wanted to draw your attention to in this chapter were some tools integrated with Spring Cloud such as Consul, Zookeeper, RabbitMQ, or Zipkin. I described all of them in some details. I also pointed out the projects responsible for interaction with those tools.

This chapter completes the first part of the book. In this part, the main goal was to get you into...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Spring Cloud
Published in: Apr 2018Publisher: PacktISBN-13: 9781788475433
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski