Step-by-Step Guide to Building a Spring Cloud Microservices System with Eureka, Ribbon, and Feign
This tutorial walks through creating a Spring Cloud microservices architecture using Spring Boot, Eureka service registry, Ribbon load balancing, and Feign clients, covering project initialization in IntelliJ IDEA, Maven configuration, YAML setup, service implementation, high‑availability Eureka server clustering, and testing the end‑to‑end workflow.
The article introduces the fundamentals of Spring Cloud and explains its role in transforming a monolithic server into a collection of independent microservices, using analogies such as school departments, a registration center (Eureka), a gateway (Zuul), and a circuit breaker (Hystrix).
Project Initialization : In IntelliJ IDEA, create a new Maven project (File → New → Project → Empty Project). Configure Maven settings (path to settings.xml) and set the project SDK. Add the Spring Boot parent and Spring Cloud dependencies (Edgware.SR5) to the pom.xml of the registration server module:
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.yun</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>The server’s application.yml configures the Eureka registration center:
server:
port: 8700
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka
instance:
hostname: localhost
spring:
application:
name: eureka-serverThe entry class enables the server:
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}After running this class, the Eureka dashboard is accessible at http://localhost:8700/.
Service Provider Module : Create a second module, add the same parent and a dependency on spring-cloud-starter-netflix-eureka-client. Its application.yml registers the service with Eureka:
server:
port: 8701
eureka:
client:
service-url:
defaultZone: http://localhost:8700/eureka
instance:
hostname: localhost
spring:
application:
name: eureka-serviceImplement a simple REST controller:
@RestController
@RequestMapping("/Hello")
public class Controller {
@RequestMapping("/World")
public String helloWorld(@RequestParam String s) {
System.out.println("Received: " + s);
return "Received: " + s;
}
}And the main class:
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}Running this module registers the provider with the Eureka server.
Service Consumer Module : Create a third module for the client. Its application.yml points to the same Eureka server:
server:
port: 8702
eureka:
client:
service-url:
defaultZone: http://localhost:8700/eureka
instance:
hostname: localhost
spring:
application:
name: eureka-consumerDefine a @Bean that provides a load‑balanced RestTemplate:
@Configuration
public class Beans {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}Consume the provider using Ribbon (client‑side load balancing):
@RestController
@RequestMapping("/Hello")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/Consumer")
public String helloWorld(@RequestParam String s) {
String result = restTemplate.getForObject(
"http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);
return result;
}
}Accessing http://localhost:8702/Hello/Consumer?s=example triggers a lookup of EUREKA-SERVICE from the registry, selects an instance (default random algorithm), and forwards the request.
High‑Availability Eureka Server : To avoid a single point of failure, configure three Eureka instances on ports 8697, 8698, and 8699. Each instance’s application.yml sets eureka.client.service-url.defaultZone to the other two instances, forming a cluster. After starting all three, the dashboard shows all instances and any registered services are replicated across them.
When a provider registers with one Eureka node, the other nodes automatically receive the registration, allowing clients to continue operating even if one or two Eureka servers go down. The article demonstrates this by stopping servers, restarting the consumer, and observing that service discovery still works thanks to the replicated registry.
Finally, the author encourages readers to like and share the tutorial.
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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
