Spring Cloud Comprehensive Guide: Building a Simple Microservices Application
This guide introduces Spring Cloud, explains its core features such as configuration management, service discovery, circuit breaking, and distributed tracing, and walks through creating a simple microservices system with a config server, Eureka server, user service, and order service using Java and Spring Boot.
The rise of microservice architecture has changed how developers build and deploy applications. Spring Cloud, part of the Spring ecosystem, aims to simplify the development and management of microservices. This comprehensive guide explores Spring Cloud and its features by building a simple microservice application.
What is Spring Cloud?
Spring Cloud is a collection of tools and libraries that provide solutions for common patterns and challenges in distributed systems, such as configuration management, service discovery, circuit breakers, and distributed tracing. Built on Spring Boot, it enables the creation of scalable, fault‑tolerant microservices.
Key Features of Spring Cloud
Configuration Management : Spring Cloud Config provides centralized configuration for distributed applications.
Service Discovery : Spring Cloud Netflix Eureka supports service registration and discovery for better load balancing and resilience.
Circuit Breaker : Spring Cloud Netflix Hystrix isolates service calls to prevent cascading failures.
Distributed Tracing : Spring Cloud Sleuth and Zipkin enable request tracing across multiple services for observability and debugging.
Building a Simple Microservice Application with Spring Cloud
In this example we will create a simple microservice application consisting of two services: user-service and order-service. Spring Cloud Config and Eureka will be used for centralized configuration and service discovery.
Prerequisites
Make sure the following software is installed on your machine:
Java 8 or higher
Maven or Gradle
Your preferred IDE
<!-- maven -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>Or
// Gradle
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-web'Step 1: Set Up the Spring Cloud Config Server
Create a new Spring Boot project with Spring Initializr and add the Config Server and Eureka Discovery dependencies. Name the project config-server.
Add the following properties to application.yml:
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/your-username/config-repo.git # Replace with your Git repository URL
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/Enable the Config Server and Eureka Client in the main class:
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}Step 2: Set Up the Spring Cloud Eureka Server
Create another Spring Boot project with the Eureka Server dependency and name it eureka-server. Add these properties to its application.yml:
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
registerWithEureka: false
fetchRegistry: falseEnable the Eureka Server in the main class:
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}Step 3: Create the User Service
Generate a new Spring Boot project with Config Client, Eureka Discovery, and Web dependencies. Name it user-service. Add the following to bootstrap.yml:
spring:
application:
name: user-service
cloud:
config:
uri: http://localhost:8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/Create a simple REST controller:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/users/{id}")
public String getUser(@PathVariable("id") String id) {
return "User with ID: " + id;
}
}Step 4: Create the Order Service
Similarly, generate a project named order-service with the same dependencies. Add this to its bootstrap.yml:
spring:
application:
name: order-service
cloud:
config:
uri: http://localhost:8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/REST controller for the order service:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@GetMapping("/orders/{id}")
public String getOrder(@PathVariable("id") String id) {
return "Order with ID: " + id;
}
}Step 5: Run the Applications
Start the services in the following order: config-server, eureka-server, user-service, and order-service. Once all services are running, you can access the Eureka dashboard at http://localhost:8761 to see the registered instances.
You can then call the user service at http://localhost:<user-service-port>/users/1 and the order service at http://localhost:<order-service-port>/orders/1.
Conclusion
This comprehensive guide demonstrated Spring Cloud’s capabilities by building a simple microservice application. Leveraging Spring Cloud simplifies development and management of microservices, making them more resilient, scalable, and maintainable, and helps you embrace the microservice world.
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.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.
