Building a Microservice Architecture with Spring Cloud, Docker, and Netflix OSS
This article explains how to construct a full‑stack microservice system using Spring Boot, Spring Cloud, Docker, and Netflix OSS tools, covering functional services, infrastructure components such as configuration, authentication, API gateway, service discovery, load balancing, circuit breaking, monitoring, logging, security, and continuous delivery pipelines.
The original article demonstrates how to use Spring Boot, Spring Cloud, Docker, and several Netflix OSS projects to build a conceptual microservice architecture, providing a runnable example (PiggyMetrics) that can be started with a single command.
Functional Services
The application is split into three core microservices that can be deployed independently:
Account Service
Handles user input and validation for income/expense records, savings, and account settings.
Statistics Service
Computes statistical parameters and stores time‑series data for each account, normalising values by currency and period.
Notification Service
Stores contact information and notification preferences, aggregates data from other services, and sends e‑mail notifications to subscribed users.
Each microservice has its own database (polyglot persistence) – MongoDB is used in the example.
Inter‑service communication is primarily synchronous REST, with optional asynchronous messaging for create/update operations, leading to eventual consistency.
Infrastructure Services
Configuration Service
Spring Cloud Config provides a centralized configuration repository (native profile, classpath files). Clients fetch shared/notification-service.yml and shared/application.yml at startup.
Example bootstrap.yml for a client:
spring:
application:
name: notification-service
cloud:
config:
uri: http://config:8888
fail-fast: trueDynamic refresh is possible with @RefreshScope; changes can be applied via a POST to /notifications/refresh or a webhook.
Authorization Service
Provides OAuth2 tokens for resource servers. The example uses password grant for user login and client‑credentials grant for inter‑service calls. Spring Cloud Security supplies annotations such as @PreAuthorize to protect endpoints.
@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(value = "accounts/{name}", method = RequestMethod.GET)
public List<DataPoint> getStatisticsByAccountName(@PathVariable String name) {
return statisticsService.findByAccountName(name);
}API Gateway
Zuul is used as a single entry point, routing requests to the appropriate backend services and handling cross‑cutting concerns like authentication and canary testing.
zuul:
routes:
notification-service:
path: /notifications/**
serviceId: notification-service
stripPrefix: falseService Discovery
Netflix Eureka registers service instances and enables client‑side load balancing. Services announce themselves on startup and send heartbeats; missing heartbeats cause deregistration.
spring:
application:
name: notification-serviceLoad Balancer, Circuit Breaker, and HTTP Client
Ribbon provides client‑side load balancing, Hystrix implements the circuit‑breaker pattern, and Feign offers a declarative HTTP client that integrates with Ribbon and Hystrix.
@FeignClient(name = "statistics-service")
public interface StatisticsServiceClient {
@RequestMapping(method = RequestMethod.PUT, value = "/statistics/{accountName}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
void updateStatistics(@PathVariable("accountName") String accountName, Account account);
}Monitoring Dashboard
Hystrix metrics are streamed via Spring Cloud Bus to Turbine, and visualised with the Hystrix Dashboard.
Log Analysis
Centralised logging can be achieved with the ELK stack (Elasticsearch, Logstash, Kibana).
Security
Production‑grade security should include HTTPS and encrypted configuration properties using JCE keystores.
Infrastructure Automation
Continuous delivery is demonstrated with Docker Compose and Travis CI, building Docker images for each service and tagging them with Git commit hashes.
How to Run the Whole System
Start eight Spring Boot applications, four MongoDB instances, and RabbitMQ. Ensure at least 4 GB of RAM. The system includes a gateway, Eureka registry, Config Server, Auth Server, and the three functional services.
Prerequisites
Install Docker and Docker Compose.
Set environment variables for passwords (CONFIG_SERVICE_PASSWORD, NOTIFICATION_SERVICE_PASSWORD, etc.).
Production Mode
Pull the latest images from Docker Hub and run docker-compose up -d.
Development Mode
Clone the repositories, build artifacts with Maven, and run
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -dto build local images and expose ports.
Important Endpoints
http://localhost:80 – API Gateway
http://localhost:8761 – Eureka Dashboard
http://localhost:9000 – Hystrix Dashboard
http://localhost:8989 – Turbine stream
http://localhost:15672 – RabbitMQ Management
All Spring Boot applications require the Config Server to start; the fail‑fast property and Docker restart policies ensure containers keep trying until the Config Server is available. Service discovery may need a few heartbeats before clients can locate services.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
