Mastering Microservices: From Architecture Basics to Spring Cloud & Dubbo
This comprehensive guide explains microservice fundamentals, RPC frameworks, serialization, distributed transaction models (ACID, CAP, BASE, TCC), system monitoring, high‑availability strategies, load balancing, configuration management, service registration/discovery, Spring Cloud components, Dubbo fault‑tolerance clusters, and compares Spring Boot with Spring MVC, providing practical code examples and diagrams.
0. What Is a Microservice?
Microservices are an architectural style for building distributed systems with a core idea of decentralization.
A distributed system is a set of coordinated services that work together, and a cluster is multiple machines performing the same task.
1. RPC and RPC Frameworks
RPC (Remote Procedure Call) enables remote method invocation; common frameworks include Dubbo, RMI, Hessian, Thrift, gRPC, and Motan. An RPC framework abstracts communication protocols, addressing, and serialization, relieving developers from implementing these details.
2. Serialization and Its Role
Serialization converts in‑memory objects to a transmittable format (e.g., JSON, JDK Serializable, Hessian, Dubbo, Protobuf) for network transfer or disk storage; deserialization reverses the process. It supports compression, persistence, and cross‑network transmission.
3. Distributed Transaction Handling
3.1 Essential Concepts
ACID : Atomicity, Consistency, Isolation, Durability.
CAP Theory : Consistency, Availability, Partition tolerance (P cannot be sacrificed).
BASE Theory : Basically Available, Soft state, Eventually consistent.
3.2 Transaction Implementation Strategies
XA (database‑level two‑phase commit)
Periodic background data reconciliation
Message‑queue based eventual consistency
TCC (Try‑Confirm‑Cancel) compensation mechanism
TCC registers a confirm and a cancel operation for each business step, ensuring compensation when needed.
4. System Monitoring
Link Tracing
Spring‑Cloud‑Sleuth + Zipkin collect inter‑service call data; traces (Trace, Span) show client‑sent, server‑received, server‑sent, and client‑received events.
Log Monitoring
ELK stack (Elasticsearch, Logstash, Kibana) provides centralized log collection, filtering, and visualization.
Code Performance Monitoring
Metrics (throughput, response time, concurrency) can be gathered via code instrumentation and visualized with Grafana.
5. High Availability
Achieved through multiple instances, health checks, self‑recovery (circuit breakers, Eureka protection), and rapid scaling (Docker orchestration, cloud resources).
6. Load Balancing
Server‑Side
External load balancers (F5, Nginx, LVS) hide service addresses and provide unified entry points.
Client‑Side
Libraries like Dubbo or Spring Cloud Ribbon perform load balancing within the client, supporting dynamic routing.
7. Distributed Configuration Center
Spring Cloud Config stores configuration files in Git/SVN and serves them to all services, supporting encryption for sensitive data.
8. Service Registration & Discovery
Services register themselves with a registry (e.g., Eureka); consumers discover providers via the registry, receiving instance lists and handling changes through long‑polling.
9. How to Split a Distributed System
By business lines, technology stacks, and architectural layers (frontend, middle‑platform, backend, storage, cache, MQ, search).
10. Spring Cloud Essentials
Security
HTTPS encryption, single sign‑on (SSO), distributed session storage (Redis, Memcached), and token‑based authentication.
Gateway
Zuul provides dynamic routing, reverse proxy, and can handle authentication, aggregation, and logging.
Service Calls
Feign, RestTemplate, or manual HTTP clients can be used; they integrate with Ribbon for load balancing and Hystrix for fault tolerance.
Health Checks
Spring‑Boot‑Starter‑Actuator exposes health endpoints; Sleuth traces calls; ELK aggregates logs; Docker health checks auto‑restart failed containers.
Fault Tolerance
Ribbon for client‑side load balancing and retries
Hystrix for rate limiting, circuit breaking, and fallback
11. Dubbo Deep Dive
Service Provider
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />Service Consumer
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />Fault‑Tolerance Clusters
Failover (retry other servers)
Failfast (immediate error)
Failsafe (ignore errors)
Failback (record and retry later)
Forking (parallel calls, first success wins)
Broadcast (invoke all providers)
Timeout Settings
Configured at consumer or provider level, with hierarchy: method‑level > interface‑level > global.
Communication Protocol
Dubbo protocol header (magic number, flag, status, message ID, length) + body (version, interface, method, parameters, serialization).
12. Spring Boot vs Spring MVC
Spring MVC is a servlet‑based MVC framework requiring extensive XML/properties configuration; Spring Boot bundles starters (web, JPA, Redis, etc.) for auto‑configuration, producing executable JARs and reducing boilerplate.
Key Annotations
@Required – validates bean property injection
@Autowired – type‑based injection
@Qualifier – specify bean name when multiple candidates
@Controller – marks MVC controller
@RequestMapping – maps URLs to controller methods
AOP Overview
Join points are method executions; pointcuts select them; advice adds behavior; together they form an aspect.
Enabling Annotation Wiring
Include <context:annotation-config/> in Spring configuration.
JdbcTemplate & RestTemplate
JdbcTemplate simplifies JDBC operations (execute, update, query, call). RestTemplate is a client for RESTful services, supporting different HTTP request factories (SimpleClientHttpRequestFactory, HttpComponentsClientHttpRequestFactory).
Transaction Management
Supports programmatic (TransactionTemplate) and declarative (AOP, @Transactional) approaches across JTA, JDBC, Hibernate, JPA, JDO.
Spring Boot Starters
Starter dependencies bundle auto‑configuration; META‑INF/spring.factories lists all auto‑configuration classes that Spring Boot loads at startup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
