Spring Microservices In Action Latest Edition

0 views
Skip to first unread message

Taneka Tarring

unread,
Aug 5, 2024, 11:54:16 AM8/5/24
to flavevcanbubb
Bydividing large applications into separate self-contained units, Microservices are a great step toward reducing complexity and increasing flexibility. Spring Microservices in Action, Second Edition teaches you how to build microservice-based applications using Java and the Spring platform. This second edition is fully updated for the latest version of Spring, with expanded coverage of API routing with Spring Cloud Gateway, logging with the ELK stack, metrics with Prometheus and Grafana, security with the Hashicorp Vault, and modern deployment practices with Kubernetes and Istio.

Building and deploying microservices can be easy in Spring! Libraries like Spring Boot, Spring Cloud, and Spring Cloud Gateway reduce the boilerplate code in REST-based services. They provide an effective toolbox to get your microservices up and running on both public and private clouds.


The easiest way to get started is visit start.spring.io, select your Spring Boot version and the Spring Cloud projects you want to use. This will add the corresponding Spring Cloud BOM version to your Maven/Gradle file when you generate the project.


If you an existing Spring Boot app you want to add Spring Cloud to that app, the first step is to determine the version of Spring Cloud you should use. The version you use in your app will depend on the version of Spring Boot you are using.


Bug fixes and backwards compatible features are added to each release train via a service release (SR). Once you determine which version of Spring Cloud to use, you should use the latest service release for that release train. You can find the latest service release information on our release notes page.


It is recommended that you use release train BOM spring-cloud-dependencies This is a BOM-only version and it just contains dependency management and no plugin declarations or direct references to Spring or Spring Boot. You can Spring Boot parent POM, or use the BOM from Spring Boot (spring-boot-dependencies) to manage Spring Boot versions.


Just like Spring Boot, many Spring Cloud projects include starters that you can add as dependencies to add various cloud native features to your project. In many cases, many features are enabled purely by adding the starter to your classpath. The starter names are documented within the individual projects. Below is an example of how you would add a Spring Cloud Config Client and a Spring Cloud Netflix Eureka client to your application.


A cloud-native orchestration service for composable microservice applications on modern runtimes. Easy-to-use DSL, drag-and-drop GUI, and REST-APIs together simplifies the overall orchestration of microservice based data pipelines.


Spring Cloud Function promotes the implementation of business logic via functions. It supports a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).


A lightweight event-driven microservices framework to quickly build applications that can connect to external systems. Simple declarative model to send and receive messages using Apache Kafka or RabbitMQ between Spring Boot apps.


Spring Cloud Stream Applications are out of the box Spring Boot applications providing integration with external middleware systems such as Apache Kafka, RabbitMQ etc. using the binder abstraction in Spring Cloud Stream.


A short-lived microservices framework to quickly build applications that perform finite amounts of data processing. Simple declarative for adding both functional and non-functional features to Spring Boot apps.


Spring Cloud is an umbrella project consisting of independent projects with, in principle, different release cadences. To manage the portfolio a BOM (Bill of Materials) is published with a curated set of dependencies on the individual project. Go here to read about the Release Train naming conventions.


The Spring Boot Core Feature Management libraries provide support for implementing feature flags in a Spring Boot application. These libraries allow you to declaratively add feature flags to your code.


The Feature Management libraries also manage feature flag lifecycles behind the scenes. For example, the libraries refresh and cache flag states, or guarantee a flag state to be immutable during a request call. In addition, the Spring Boot library offers integrations, including MVC controller actions, routes, and middleware.


The Spring Boot feature manager FeatureManager gets feature flags from the framework's native configuration system. As a result, you can define your application's feature flags by using any configuration source that Spring Boot supports, including the local bootstrap.yml file or environment variables. FeatureManager relies on dependency injection. You can register the feature management services by using standard conventions:


We recommend that you keep feature flags outside the application and manage them separately. Doing so allows you to modify flag states at any time and have those changes take effect in the application right away. App Configuration provides a centralized place for organizing and controlling all your feature flags through a dedicated portal UI. App Configuration also delivers the flags to your application directly through its Spring Boot client libraries.


Each feature flag has two parts: a name and a list of one or more filters that are used to evaluate if a feature's state is on (that is, when its value is True). A filter defines a use case for when a feature should be turned on.


When a feature flag has multiple filters, the filter list is traversed in order until one of the filters determines the feature should be enabled. At that point, the feature flag is on, and any remaining filter results are skipped. If no filter indicates the feature should be enabled, the feature flag is off.


By convention, the feature-management section of this YML document is used for feature flag settings. The prior example shows three feature flags with their filters defined in the EnabledFor property:


When an MVC controller or action is blocked because the controlling feature flag is off, a registered DisabledFeaturesHandler interface is called. The default DisabledFeaturesHandler interface returns a 404 status code to the client with no response body.


You can set up MVC filters so that they're activated based on the state of a feature flag. The following code adds an MVC filter named FeatureFlagFilter. This filter is triggered within the MVC pipeline only if feature-a is enabled.


In this tutorial, you learned how to implement feature flags in your Spring Boot application by using the spring-cloud-azure-feature-management-web libraries. For further questions see the reference documentation, it has all of the details on how the Spring Cloud Azure App Configuration library works.For more information about feature management support in Spring Boot and App Configuration, see the following resources:


Slow performance is a recurring and complex problem that developers are often faced with. One of the most common approaches to address such a problem is through caching. Indeed, this mechanism allows achieving a substantial improvement in the performance of any type of application. The problem is that dealing with caching is not an easy task. Luckily, caching is provided by Spring Boot transparently thanks to the Spring Boot Cache Abstraction, which is a mechanism allowing consistent use of various caching methods with minimal impact on the code. Let's see everything you should know to start dealing with it.


First, we will introduce the concept of caching. Then, we will study the most common Spring Boot cache-related annotations, understanding what the most important ones are, where, and how to use them. Next, it will be time to see what are the most popular cache engines supported by Spring Boot at the time of writing. Finally, we will see Spring Boot caching in action through an example.


Caching is a mechanism aimed at enhancing the performance of any kind of application. It relies on a cache, which can be seen as a temporary fast access software or hardware component that stores data to reduce the time required to serve future requests related to the same data. Dealing with caching is complex, but mastering this concept is practically unavoidable for any developer. If you are interested in delving into caching, understanding what it is, how it works, and what are its most important types, you should follow this link first.


The Spring Boot Cache Abstraction does not come with the framework natively but requires a few dependencies. Thankfully, you can easily install all of them by adding the spring-boot-starter-cache to your dependencies.


In detail, spring-boot-starter-cache brings the spring-context-support module, which transitively depends on spring-context. The latter allows Spring to deal with contexts, also called Spring IoC (Inversion of Control) containers, which are responsible for managing objects in a Spring application. In particular, they are in charge of configuring, instantiating, and assembling Beans by reading configuration files and employing annotations. While the spring-context-support module provides support for integrating third-party cache engines, which will be presented later, into a Spring application.Follow this link from the Spring official documentation for further reading on the Spring IoC containers and the bean management.


After adding the dependencies required to start using the Spring Cache Abstraction mechanism, it is time to see how to implement the desired caching logic. This can easily be achieved by marking Java methods with specific caching-related annotations. Let's delve into the most important ones, showing where to use them and how.


To enable the Spring Boot caching feature, you need to add the @EnableCaching annotation to any of your classes annotated with @Configuration or to the boot application class annotated with @SpringBootApplication.


@EnableCaching automatically sets up a valid instance of the CacheManager interface, which is needed to enable caching. In particular, this annotation look for one of the many cache engines we will analyze later in the article. If not found, it creates a ConcurrentMapCacheManager, which provides a default implementation of an in-memory cache based on a ConcurrentHashMap object.

3a8082e126
Reply all
Reply to author
Forward
0 new messages