Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Microservice Frameworks: SpringBoot and SpringCloud Essentials

Tech 1

Microservice Architecture Concepts

The evolution of software architecture has progressed through several stages: monolithic architecture → clustering → verticalization → service-oriented architecture (SOA) → microservices (granular implementation of SOA).

SOA vs. Microservices

SOA (Service-Oriented Architecture) focuses on:

  • Service decoupling
  • Solving information silos
  • Ensuring data interoperability

Microservices represent a more granular approach to SOA:

  • Breaking down services into smaller components (e.g., user service split into account service, points service, etc.)

Service Registry Functions

  • Dynamic service discovery: Tracks service registration and deregistration
  • Efficient service address management: Clients access services by name rather than managing addresses directly

Common service registries include: Zookeeper, Eureka, Nacos, Consul

Gateway Functions

  • Routing
  • Authentication and authorization
  • Logging
  • Rate limiting
  • Circuit breaking

Rate Limiting, Degradation, and Circuit Breaking

  • Rate Limiting: Controls request flow using algorithms like token bucket, leaky bucket, and sliding window. Frameworks include Alibaba's Sentinel (sliding window) and Spring Cloud's Hystrix (semaphore).
  • Degradation: Disabling non-essential services to maintain core functionality. Passive degradation results from rate limiting or circuit breaking, while active degradation involves manually disabling features like comments or ads.
  • Circuit Breaking: Temporarily stops calls to malfunctioning services to prevent cascading failures. For example, Hystrix triggers circuit breaking after 20 failures within 5 seconds.

Advantages and Disadvantages of Microservices

Advantages:

  • High modularity with clear boundaries
  • High cohesion and low coupling
  • Independent deployment without affecting other services

Disadvantages:

  • Increased complexity from distributed systems
  • Data consistency challenges
  • Complex operations requiring high standards for reliability, stability, monitoring, and capacity planning

SpringBoot

What is SpringBoot?

SpringBoot is a microservice framework built on the Spring ecosystem that enables rapid microservice development with various starters for quick onboarding.

Core SpringBoot Annotation

@SpringBootApplication combines three key annotations:

  • @SpringBootConfiguration: Marks the class as a SpringBoot configuration
  • @EnableAutoConfiguration: Enables auto-configuration
  • @ComponentScan: Enables Spring component scanning

SpringBoot Auto-configuration

Auto-configuration automatically loads configuration classes and bean objects into the Spring container during application startup.

Principle: The @EnableAutoConfiguration annotation in SpringBootApplication scans spring.factories and .imports files (in version 2.7.x and later) to load xxxAutoConfiguration classes and their @Bean-annotated objects into the Spring container.

SpringBoot follows the "convention over configuration" principle, providing pre-configured options through starters. Each starter typically has a corresponding auto-configuration.

  • xxxAutoConfiguration: Auto-configuration classes
  • xxxProperties: Encapsulation of configuration file attributes

Configuration File Loading Order: xxx.properties, yml, yaml

Core Configuration Files: application(.yml or .properties) or bootstrap(.yml or .properties). The application file is commonly used in SpringBoot projects, while bootstrap is typically used in SpringCloud for loading remote configuration files.

Custom Starters

A starter manages dependencies and auto-configuration and consists of two modules:

  • Starter module: Manages dependencies and includes the auto-configuration module
  • Auto-configuration module: Contains auto-configuration files (spring.factories or .imports) and auto-configuration classes with @Bean annotations

SpringBoot Actuator

SpringBoot Actuator provides monitoring and management capabilities through endpoints accessible at http://localhost:8080/actuator/{endpoint}.

Common endpoints:

  • /health: Application health status (UP/DOWN)
  • /info: Basic application information
  • /beans: All beans in the Spring container
  • /loggers: Logger configuration and levels
  • /threaddump: Thread information
  • /heapdump: JVM heap dump
  • /metrics: Application metrics
  • /shutdown: Graceful application shutdown

Application Security

SpringBoot security (spring-boot-starter-security) provides:

  • Authentication
  • Authorization
  • Attack protection

Session Sharing in Microservices

Spring Session with Redis resolves session isolation in microservices by storing sessions in Redis.

Scheduled Tasks

Use @Scheduled annotation for single-node scheduling or distributed frameworks like XXL-Job for distributed scheduling.

SpringCloud and SpringCloud Alibaba

What is SpringCloud?

SpringCloud is a comprehensive microservice solution built on SpringBoot.

Spring, SpringBoot, and SpringCloud Differences

  • Spring: Bean-centered, providing IOC, AOP, etc.
  • SpringBoot: Application-centered, providing starters, auto-configuration, monitoring
  • SpringCloud: Service-centered, providing gateway, load balancing, service registration and discovery, service invocation, fault tolerance

SpringCloud vs. SpringCloud Alibaba Components

Function SpringCloud Implementation SpringCloud Alibaba Implementation
Service Governance Eureka, Zookeeper Nacos
Service Invocation Feign, OpenFeign Dubbo (HSF)
Load Balancing Ribbon Ribbon
Gateway SpringCloud Zuul Gateway
Fault Tolerance Hystrix Sentinel
Distributed Scheduling (XXL-J alternative) ScheduleX
Configuration Center Spring Cloud Config Diamond
Messaging (RabbitMQ, Kafka alternative) RocketMQ
Service Bus Bus Nacos
Deployment Docker, Kubernetes, OpenStack Docker
Distributed Transactions (2PC alternative) Seata
Monitoring AliMonitor, Arthas AliMonitor, Arthas
Tracing Spring Cloud Sleuth SkyWalking

Gateway - Gateway and Zuul

Frontend requests pass through the Gateway, which routes requests to appropriate services based on routing policies.

Gateway vs. Zuul

  • Zuul: Netflix's legacy gateway in maintenance mode
  • Gateway:新一代API网关,提供更多功能、性能和灵活性优势

Gateway Functions

  • Unified entry point
  • Dynamic routing
  • Authentication and validation
  • Rate limiting

Gateway Route Configuration

Routes are configured with id, predicates, and uri. Route conditions are defined using predicates based on path, parameters, headers, etc.

Predicates

Predicates define route conditions based on request attributes. Common route factories:

Name Description Example
Path Request path must match specified pattern - Path=red/{segment},/blue/**
Query Request must contain specified parameters - Query=name,Jack
Header Request must contain specified headers - Header=X-Request-Id,\d+

Filters

Filters process requests and responses. Custom filters implement the GlobalFilter interface.

Gateway Rate Limiting

  • Use Gateway's RequestRateLimit filter with Redis (token bucket algorithm)
  • Use Sentinel with Gateway for rate limiting

Gateway Load Balancing Algorithms

Random, round-robin, weighted round-robin, least connections, IP Hash

Gateway Monitoring

Use SpringBoot Actuator with endpoint exposure:

management:
  endpoints:
    web:
      exposure:
        include: gateway

Nginx vs. Gateway

  • Nginx: Traffic gateway for reverse proxy and load balancing
  • Gateway: Business gateway for routing to microservices

Service Registration and Discovery

Service registration maintains a registry of all service addresses. New services register with the center, and consumers retrieve service information from it.

Eureka

CP or AP? Eureka is AP, prioritizing availability over consistency through eventual consistency.

Implementation:

  1. Set up Eureka Server with dependencies and configuration
  2. Register services with eureka-client dependency
  3. Discover services using @LoadBalance for load balancing

Principles:

  • Service registration: Maintains a service registry
  • Service discovery: Clients query the registry for services
  • Heartbeat and health checking: Services send periodic heartbeats
  • Load balancing: Works with Ribbon to select services
  • Service synchronization: Multiple Eureka Servers replicate data

Multi-level Caching: Eureka uses three-level caching: read-only cache, read-write cache, and registry to improve read performance while maintaining write isolation.

Self-preservation Mode: When >85% of services don't send heartbeats within 15 minutes, Eureka enters self-preservation to prevent service removal during network issues.

Zookeeper

What is Zookeeper? A distributed coordination framework for distributed coordination, configuration center, distributed locks, etc. Zookeeper nodes have a 1MB data limit.

CP or AP? Zookeeper is CP, prioritizing consistency over availability.

ZAB Protocol: Zookeeper uses ZAB (Zookeeper Atomic Broadcast) protocol with two modes:

  • Recovery mode: Leader election when leader is unavailable
  • Broadcast mode: Leader handles write requests and replicates to followers

Watch Mechanism: Clients register watchers for node changes, receiving notifications when monitored nodes change.

Leader Election: Based on zxid (transaction ID) and myid (server ID). Servers vote for the server with highest zxid, or myid if zxids are equal.

Distributed Locks: Implemented using ephemeral sequential nodes to ensure lock uniqueness and automatic release.

Nacos

What is Nacos? SpringCloud Alibaba component serving as both service registry and configuration center.

Service Registration and Discovery:

  1. Install Nacos and access management console
  2. Register services with nacos-discovery dependency and @EnableDiscoveryClient
  3. Discover services using Ribbon for load balancing

Configuration Management:

  1. Add configurations in Nacos console
  2. Use @Value or @ConfigurationProperties for configuration access
  3. Enable hot updates with @RefreshScope

Data Interaction Modes:

  • Pull mode: Clients periodically fetch data (used in configuration updates)
  • Push mode: Server actively sends data to clients (used in service discovery)

CP or AP? Nacos supports both modes:

  • AP mode for service registry
  • CP mode for configuration center

High Performance: Nacos uses blocking queues and thread pools for asynchronous service registration to handle high loads.

Service Invocation

Feign

What is Feign? A declarative HTTP client for elegant HTTP request handling.

Feign vs. OpenFeign: OpenFe enhances Feign with better SpringBoot and SpringCloud integration.

Implementation:

  1. Add OpenFeign dependency
  2. Add @EnableFeignClients to main class
  3. Create Feign client interface

Principle: Feign uses JDK dynamic proxy to generate proxy objects, which use Ribbon for service discovery and load balancing before making HTTP calls.

Feign vs. Dubbo:

  • Feign uses HTTP with short connections, simpler implementation
  • Dubbo uses RPC with long connections, better for high concurrency

Dubbo

What is Dubbo? An RPC framework for service address management, registration, discovery, and monitoring, typically used with Zookeeper.

Workflow:

  1. Provider starts service in Container
  2. Provider registers service with Register
  3. Consumer subscribes to services
  4. Consumer invokes Provider using load balancing
  5. Monitor tracks service calls

Provider Configuration:

  • Timeout: @Service(timeout=3000)
  • Retry attempts: @Service(retries=2)
  • Load balancing: @Service(loadbalance="random")
  • Cluster fault tolerance: @Service(cluster="failfast")

Protocols:

  • Dubbo: High performance, lightweight
  • HTTP: Standard, universal
  • Hessian: Simple, cross-language
  • RMI: Java standard, cross-platform

Registry Failure Handling: Dubbo consumers cache service addresses locally, allowing continued operation evenif the registry is unavailable.

Load Balancing

Ribbon

What is Ribbon? A client-side load balancer that selects service instances for consumers.

Implementation: Add @LoadBalanced to RestTemplate bean:

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Load Balancing Strategies: Random, round-robin, weighted, best available, least concurrent

Configuration:

  1. In configuration files:
service-name:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
  1. Using @RibbonClient annotation

Custom Strategies: Implement IRule interface and choose() method

Principle: Ribbon intercepts RestTemplate requests, retrieves service lists, selects instances using algorithms, and modifies request addresses.

Service Fault Tolerance

Hystrix

Hystrix provides:

  • Isolation: Thread pool isolation to prevent thread exhaustion
  • Circuit Breaking: Stops calls to malfunctioning services
  • Degradation: Provides fallback functionality

Sentinel

What is Sentinel? Alibaba's open-source fault tolerance component providing rate limiting, circuit breaking, and degradation.

Why Sentinel? Prevents service cascading failures by controlling traffic and protecting service stability.

Sentinel vs. Hystrix: Sentinel offers more comprehensive features including rate limiting, circuit breaking, degradation, and traffic shaping, with a user-friendly control panel.

Flow Control Modes:

  • Direct: Limits the current resource
  • Associated: Limits current resource based on another resource
  • Chain: Limits current resource based on request source

Flow Control Effects:

  • Fast failure: Rejects requests immediately
  • Warm up: Gradually increases threshold
  • Queuing: Requests wait in queue based on threshold

Rate Limiting Algorithms:

  • Fixed window: Simple but prone to request spikes at boundaries
  • Sliding window: Smoother flow but requires more resources
  • Leaky bucket: Constant output rate
  • Token bucket: Controls token generation rate and bucket size

Circuit Breaking: Sentinel uses a state machine with three states:

  • Closed: All requests pass, statistics collected
  • Open: Requests rejected, fallback logic executed
  • Half-open: Single request allowed to test service recovery

Circuit Breaking Strategies: Slow calls, exception ratio, exception count

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.