0% found this document useful (0 votes)
45 views48 pages

Spring Microservice

Spring Cloud is a set of tools designed to simplify the development of cloud-native applications and microservices, providing features such as service discovery, configuration management, and resilience patterns. It offers a robust ecosystem for building scalable and resilient applications, with components like Eureka for service discovery and Spring Cloud Gateway for API management. The document outlines the benefits, use cases, and implementation examples of Spring Cloud, emphasizing its role in enhancing microservices architecture.

Uploaded by

khanhnthe182090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views48 pages

Spring Microservice

Spring Cloud is a set of tools designed to simplify the development of cloud-native applications and microservices, providing features such as service discovery, configuration management, and resilience patterns. It offers a robust ecosystem for building scalable and resilient applications, with components like Eureka for service discovery and Spring Cloud Gateway for API management. The document outlines the benefits, use cases, and implementation examples of Spring Cloud, emphasizing its role in enhancing microservices architecture.

Uploaded by

khanhnthe182090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 04.

Spring Cloud
Objectives
◆ What is Spring Cloud?
◆ Spring Cloud Features
◆ Benefits of using Spring Cloud
◆ Use Cases for Spring Cloud
◆ Spring Cloud Series
◆ Example of Microservices App Implementation with Spring Cloud
◆ Spring Cloud Gateway Architecture
◆ Spring Cloud Gateway Demonstration

2
Introducing Spring Cloud
◆ Spring Cloud provides tools for developers to quickly build some of the common
patterns in distributed systems. It's not a runtime platform but a set of libraries that
integrate well with Spring Boot.
◆ It provides solutions for the challenges we just discussed, helping you build
resilient, reliable, and coordinated applications.
◆ Think of it as the glue that holds your microservices together.

3
What is Spring Cloud?
◆ Spring Cloud extends Spring Boot to facilitate building cloud-native applications.
◆ Spring Cloud simplifies the development of cloud-native, distributed systems.
◆ Spring Cloud provides tools for developers to quickly build some of the common
patterns in distributed systems (e.g. configuration management, service discovery,
circuit breakers, intelligent routing, micro-proxy, control bus, short lived
Microservices and contract testing).

4
Core Spring Cloud Components
Component Problem It Solves

Eureka Service Discovery


Config Server Centralized, externalized configuration
Gateway API Gateway / Edge Service
Resilience4j Circuit Breaker / Fault Tolerance
Spring Cloud LoadBalancer Client-Side Load Balancing
Micrometer & Zipkin/Jaeger Distributed Tracing / Observability

5
Spring Cloud Features
◆ Spring Cloud focuses on providing good out of box experience for typical use
cases and extensibility mechanism to cover others.
 Distributed/versioned configuration
 Service registration and discovery
 Routing
 Service-to-service calls
 Load balancing
 Circuit Breakers
 Distributed messaging
 Short lived microservices (tasks)
 Consumer-driven and producer-driven contract testing

6
Spring Cloud Features (cont.)
◆ Microservices Support: Simplifies the complexities of building resilient and
scalable microservices.
◆ Service Discovery: Automatically register and discover services dynamically,
enhancing resilience and scalability.
◆ Load Balancing: Client-side load balancing strategies to distribute requests
efficiently among service instances.
◆ API Gateway: Centralized entry point for routing requests, handling cross-cutting
concerns like security and logging.
◆ Configuration Management: Centralized configuration management for
consistent application settings across environments.
◆ Resilience Patterns: Implements design patterns such as circuit breakers and
retries to enhance application stability and reliability.
7
Benefits of using Spring Cloud
◆ Developers focus on business logic - Spring Cloud provides all the boilerplate
code to implement common design patterns of the cloud. Developers thus can
focus on the business logic without the need to develop and maintain this
boilerplate code.
◆ Improved scalability and performance - Spring Cloud works well with
microservice architectures, where individual services can be scaled independently
based on demand.
◆ Easy to use − Spring Cloud projects can easily be integrated with existing Spring
Projects.
◆ Robust ecosystem and community support - Comprehensive guides, tutorials,
and reference documentation are readily available.
8
Use cases for Spring Cloud
◆ Microservices Architecture: Ideal for applications built on a Microservices
architecture, enabling easy management, discovery, and resilience of services.
◆ Cloud-Native Applications: Perfect for building applications designed to run in
cloud environments, enabling rapid scaling and flexibility.
◆ Dynamic Configuration: Applications needing runtime configuration updates
without requiring redeployment.

9
Spring Cloud Series
◆ Spring Cloud Config
 Centralizes the management of application configuration across environments.
 Provides both server and client implementations to dynamically retrieve
configuration from a central repository (Git, file system).
◆ Spring Cloud Netflix - A collection of tools based on Netflix OSS components to
handle microservices patterns.
 Eureka: Service discovery server for locating Microservices and enabling client-side
load balancing.
 Ribbon: Client-side load balancer for distributing requests across available service
instances. (Spring Cloud Gateway)
 Hystrix: Circuit breaker that enhances resilience by allowing fallback methods
during service failures (Resillience4j).
 Zuul: API gateway and reverse proxy that provides routing and filtering for requests
10
Spring Cloud Series (cont.)
◆ Spring Cloud Gateway
 A modern API gateway built on Spring WebFlux.
 Offers dynamic routing, filters for request manipulation, and integration with other
Spring Cloud components.
◆ Spring Cloud OpenFeign
 Simplifies HTTP client calls to other Microservices using declarative REST clients.
 Integrates seamlessly with Ribbon for load balancing and Eureka for service
discovery.
◆ Spring Cloud Sleuth
 Provides distributed tracing capabilities to track the flow of requests across
Microservices.
 Integrates with tools like Zipkin or OpenTelemetry for visualizing request traces.
11
Spring Cloud Series (cont.)
◆ Spring Cloud Bus
 Links nodes of a distributed system, allowing for broadcasting messages to
multiple services.
 Useful for propagating configuration changes or events across the system.
◆ Spring Cloud Kubernetes
 Enables building Spring applications that can run on Kubernetes, providing
access to Kubernetes features like service discovery, configuration, and secret
management.
◆ Spring Cloud Security
 Provides integration with Spring Security to manage authentication and
authorization across Microservices.
12
Example of Microservices App Implementation
Build a simple e-commerce system with three core services and the necessary
Spring Cloud infrastructure.
◆ EUREKA-SERVER: Our service registry (Spring Cloud Netflix).

◆ CONFIG-SERVER: Manages all external configurations (Spring Cloud Config).

◆ API-GATEWAY: The single entry point for all external requests (Spring Cloud

Gateway).
◆ PRODUCT-SERVICE: Manages product information.

◆ ACCOUNT-SERVICE: Manages account information.

◆ ORDER-SERVICE: Manages orders and communicates with PRODUCT-

SERVICE, ACCOUNT-SERVICE (The communication is using Spring Cloud


Open Freign).
13
Example of Microservices App Implementation
◆ In a dynamic environment (like the cloud), service instances have dynamic IP
addresses and ports. Hardcoding them is not an option.
 Service Discovery acts like a phone book for the services.
 A service instance registers itself with the Eureka Server on startup.
 It sends periodic heartbeats to let Eureka know it's still alive.
 When another service needs to talk to it, it asks Eureka for the current location.

14
Spring Cloud Gateway
◆ Spring Cloud Gateway is an API gateway project that provides a simple, effective
way to route to APIs and provides cross-cutting concerns such as security,
monitoring, and resiliency.
◆ It is built on top of the Spring Framework and offers a variety of features that make
it easy to create complex routing scenarios while managing requests effectively.

15
Key features of Spring Cloud Gateway
◆ Routing: Supports dynamic routing based on predicates that evaluate incoming requests.
◆ Filters: Allows modification of incoming requests and outgoing responses, aiding in
functionalities such as authentication, logging, and metrics.
◆ Webflux-based: Built on Project Reactor, enabling non-blocking APIs for high scalability.
◆ Integration with Spring Ecosystem: Seamlessly integrates with other Spring projects
like Spring Security, Spring Cloud Config, and Spring Cloud Discovery.
◆ Load Balancing: Can work with load balancing requests across multiple services.
◆ Rate Limiting: Implement rate limiting to protect backends from being overwhelmed.
◆ Path Rewriting: Ability to rewrite the path of incoming requests.

16
Spring Cloud Gateway Architecture

17
Spring Cloud Gateway Demonstration

18
Setting up Spring Cloud Gateway
◆ An API Gateway is a single entry point for all client requests. It decouples clients
from the internal service architecture.
◆ Responsibilities:
 Routing: Forwards requests to the appropriate downstream service.
 Cross-Cutting Concerns: Handles authentication, SSL termination, logging,
and rate limiting.
 Load Balancing: Distributes traffic across multiple instances of a service.

19
Setting up Spring Cloud Gateway
◆ Step 1: Set up the Projects:
 AccountService Project
 OrchidService Project
 OrderService Project
 SpringCloudGateway Project

20
Setting up Spring Cloud Gateway
◆ Step 2: Develop the core Microservices
 2 projects AccountService, OrchidService are the same with Chapter 2.
 OrderService Project change the [Link]

21
Setting up Spring Cloud Gateway
◆ Step 3: Configure Gateway for Routing

22
Setting up Spring Cloud Gateway
◆ Step 3: Configure Gateway for Routing

23
Setting up Spring Cloud Gateway
◆ Step 4: Configure Swagger in each Microservice
 Add the Dependency springdoc-openapi-starter-webmvc-ui
 Run and Access the UI
• Swagger UI: [Link] - This is the interactive
HTML page where you can explore and test your API endpoints.
• OpenAPI Definition (JSON): [Link] - This is the raw
OpenAPI 3.0 specification in JSON format, which other tools can consume.

24
Setting up Spring Cloud Gateway
◆ Step 5: Aggregate Swagger in the Gateway

25
Setting up Spring Cloud Gateway
◆ Step 6: Run and Test [Link]

26
Setting up Spring Cloud Gateway
◆ Step 7: Create an order in Swagger

27
Advantages of using Spring Cloud API Gateway
◆ Centralized Cross-Cutting Concerns: Simplifies management of features
common to all microservices.
◆ Simplified Client Interaction: The mobile app and web frontend only need to
know the address of the gateway.
◆ Loose Coupling & Refactoring: You can change or merge microservices without
impacting the client.
◆ Optimized for Performance: Built on a non-blocking, reactive foundation (Project
Reactor), it can handle many concurrent connections with high throughput.
◆ Rich Routing Capabilities: Allows for flexible and dynamic routing of requests.

28
Disadvantages of using Spring Cloud API Gateway
◆ Single Point of Failure: If the gateway goes down, the entire Orchid Store
management system becomes unavailable. All communication between the clients
and the microservices will be cut off. This requires implementing high availability
(running multiple instances of the gateway) and robust monitoring.
◆ Increased Complexity: It adds another component to the architecture that needs
to be developed, configured, deployed, and maintained. There is a learning curve
associated with its reactive nature (WebFlux) and extensive configuration options.
◆ Potential for a Bottleneck: While it's built for performance, if not scaled or
configured correctly, all traffic passing through the gateway can create a
bottleneck, slowing down the entire system.

29
Disadvantages of using Spring Cloud API Gateway
◆ Development Overhead: For simple systems, it might be overkill. If the Orchid
Store only has a couple of microservices, the benefits of a gateway might not
outweigh the added development and operational complexity.
◆ Debugging Challenges: A request now flows through an additional layer. If an
order fails, debugging involves tracing the request from the client, through the
gateway, and to the specific microservice, which can be more complex than a
direct service call.

30
Summary
Concepts were introduced:
◆ What is Spring Cloud?

◆ Spring Cloud Features

◆ Benefits of using Spring Cloud

◆ Use Cases for Spring Cloud

◆ Spring Cloud Series

◆ Example of Microservices App Implementation with Spring Cloud

◆ Spring Cloud Gateway Architecture

◆ Spring Cloud Gateway Demonstration

31
References
◆ [Link]
sample/tree/main/src/main
◆ [Link]
webmvc/[Link]
◆ [Link]
[Link]

32
References
◆ [Link]
◆ [Link]
webmvc/[Link]

33
Spring Cloud Gateway Server Web MVC (version: 2025.0.0)

◆ Add dependencies
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-gateway-server-webmvc</artifactId>
</dependency>

◆ Use Swagger to aggregate spring doc in other miscroservice, add dependencies


<dependency>
<groupId>[Link]</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.8</version>
</dependency>

[Link]=true
◆ Config [Link] [Link]=8080
[Link]=false
34
Config Routes for Gateway
◆ Use java code @Configuration
public class GatewayConfig {
@Bean
public RouterFunction<ServerResponse> gatewayRoutes() {
package [Link];
return route("account_route")
.route(path(“/api/accounts/**"), http())
import [Link];
.before(uri("[Link]
import [Link];
.build()
import [Link];
.and(route("orchid_route")
import [Link];
.route(path("/orchids/**", "/orchids/{segment}"), http())
.before(uri("[Link]
import static [Link];
.build())
import static [Link];
.and(route("order_route")
import static [Link];
.route(path("/orders/**", "/orders/{segment}"), http())
import static [Link];
.before(uri("[Link]
import static [Link].*;
.before(rewritePath("/orders/(?<segment>.*)", "/${segment}"))
.build());
}
}

35
Configure Routes for Gateway using Eureka Server
◆ Add dependencies

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

36
Configure Routes for Gateway using Eureka Server
◆ Configure for load balancing @Configuration
public class GatewayConfig {
@Bean
package [Link]; public RouterFunction<ServerResponse> gatewayRoutes() {
return route("account_route")
.route(path("/api/accounts/**"), http())
import [Link];.filter(lb("ACCOUNT-SERVICE"))
import [Link];//.before(uri("[Link] //No Eureka Server for loadbalancing
import [Link]; .build()
.and(route("orchid_route")
import [Link]; .route(path("/api/orchids/**"), http())
.filter(lb("ORCHIDS-SERVICE")) //loadblancing
import static [Link];
//.before(uri("[Link] //No Eureka Server for loadbalancing
.build())
import static [Link];
.and(route("order_route")
import static [Link];
.route(path("/api/orders/**", "/api/orders/{segment}"), http())
import static [Link];
.filter(lb("ORDER-SERVICE"))
//.before(uri("[Link]
import static [Link].*;
.before(rewritePath("/api/orders/(?<segment>.*)", "/${segment}"))
.build());
}
}

37
Config Route for Gateway use [Link]

◆ Config in [Link] (or yml)

#Route config
[Link][3].id=account_route
[Link][3].predicates[0]=Path=/accounts/**,/accounts/{segment}
[Link][3].uri=[Link]
[Link][3].filters[0]=AddRequestHeader=X-Service1, value1

38
Config to aggregate spring doc
#Service routes
◆ Config in [Link][0].id=orchids-service
[Link] [Link][0].uri=[Link]
ies [Link][0].predicates[0]=Path=/orchids-service/v3/api-docs/**
[Link][0].filters[0]=RewritePath=/orchids-service/(?<segment>.*), /$\{segment}
[Link][1].id=order-service
[Link][1].uri=[Link]
[Link][1].predicates[0]=Path=/order-service/v3/api-docs/**
[Link][1].filters[0]=RewritePath=/order-service/(?<segment>.*), /$\{segment}
[Link][2].id=account-service
[Link][2].uri=[Link]
[Link][2].predicates[0]=Path=/account-service/v3/api-docs/**
[Link][2].filters[0]=RewritePath=/account-service/(?<segment>.*), /$\{segment}

# Swagger UI config
[Link][0].url=/account-service/v3/api-docs
[Link][0].name=Account Service
[Link][1].url=/orchids-service/v3/api-docs
[Link][1].name=Orchid Service
[Link][2].url=/order-service/v3/api-docs
[Link][2].name=Order Service

39
Access Spring Doc in Gateway
◆ [Link]
◆ Note
 In each miscroservices, need to config spring doc

40
References
◆ Documents
 [Link]
[Link]

41
Spring Cloud Gateway Server WebFlux (version:
2025.0.0)
◆ Add dependencies to [Link]

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-gateway-server-webflux</artifactId>
</dependency>

◆ Add dependencies to [Link] for swagger to aggregate spring doc


<dependency>
<groupId>[Link]</groupId>
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
<version>2.8.13</version>
</dependency>

42
Configure Routes for Gateway No Load balancing
◆ Java codes
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@Configuration
public class GatewayConfig {
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return [Link]()
.route("account_route", r -> [Link]("/api/accounts/**")
.filters(f -> [Link]("X-Service1", "Service1HeaderValue")).uri("[Link]
.route("orchid_route", r -> [Link]("/api/orchids/**")
.filters(f -> [Link]("param", "value")) .uri("[Link]
.route("order_route", r -> [Link]("/api/orders/**")
.filters(f -> [Link]("/orders/(?<segment>.*)", "/${segment}")).uri("[Link]
.build();
}
}

43
Configure Routes for Gateway Load balancing
◆ Add dependencies

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

44
Configure Routes for Gateway Load balancing
◆ Java code
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return [Link]()
package [Link];.route("account_route", r -> [Link]("/api/accounts/**")
.filters(f -> [Link]("X-Service1", "Service1HeaderValue"))
.uri("lb://account-service"))//"[Link]
import [Link];
.route("orchid_route", r -> [Link]("/api/orchids/**")
import [Link];
.filters(f -> [Link]("param", "value"))
import [Link];
.uri("lb://orchids-service"))//"[Link]
.route("order_route", r -> [Link]("/api/orders/**")
import [Link];
.filters(f -> [Link]("/orders/(?<segment>.*)", "/${segment}"))
.uri("[Link]
.build();
◆ Config in [Link]
}
}
[Link]=[Link]
us:9002/eureka/,[Link]
45
Configure Routes for Gateway use [Link]

[Link]=true
[Link]-case-service-id=true
[Link]=false
#Service routes
[Link][0].id=orchids-service
[Link][0].uri=[Link]
[Link][0].predicates[0]=Path=/orchids-service/**
[Link][0].filters[0]=RewritePath=/orchids-service/(?<segment>.*), /$\{segment}
[Link][1].id=order-service
[Link][1].uri=[Link]
[Link][1].predicates[0]=Path=/order-service/**
[Link][1].filters[0]=RewritePath=/order-service/(?<segment>.*), /$\{segment}
[Link][2].id=account-service
[Link][2].uri=[Link]
[Link][2].predicates[0]=Path=/account-service/**
[Link][2].filters[0]=RewritePath=/account-service/(?<segment>.*), /$\{segment}

46
Config to aggregate spring doc

[Link]=true
[Link]=true
[Link]=/v3/api-docs
[Link]=/[Link]

# Swagger UI config
[Link][0].url=/account-service/v3/api-docs
[Link][0].name=Account Service
[Link][1].url=/orchids-service/v3/api-docs
[Link][1].name=Orchid Service
[Link][2].url=/order-service/v3/api-docs
[Link][2].name=Order Service

47
References
◆ [Link]
[Link]

48

You might also like