Step‑by‑Step Guide to Building a Spring Cloud Microservices Architecture with Eureka, Ribbon, and Feign
This article provides a comprehensive tutorial on the fundamentals of microservices, explains how to set up a Spring Cloud project with Maven and IntelliJ IDEA, configures Eureka registration and discovery, demonstrates service provider and consumer implementations using Spring Boot, and shows how to achieve high‑availability Eureka clusters with load‑balanced calls via Ribbon and Feign.
1. Microservice Basics
The article introduces microservice concepts using a school analogy: a monolithic server is like a single teacher for all subjects, while microservices split responsibilities into separate "departments" (service groups). Eureka acts as the registry (teacher list), Zuul as the gateway (gatekeeper), and Hystrix as a circuit‑breaker volunteer.
2. Building the Microservice System
2.1 Project Initialization
Steps to create a Maven project in IntelliJ IDEA, configure Maven settings, and add modules for server, service provider, and consumer.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<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>2.2 Spring Boot and Eureka Server Configuration
Configure application.yml for the Eureka server and create the Java entry class.
server:
port: 8700
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://<${eureka.instance.hostname}>:<${server.port}>/eureka
instance:
hostname: localhost
spring:
application:
name: eureka-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 // Enable Eureka server
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}2.3 Service Provider Configuration
Set up a service module, its application.yml, Maven dependencies, and a simple REST controller.
server:
port: 8701 # service provider port
eureka:
client:
service-url:
defaultZone: http://<${eureka.instance.hostname}>:8700/eureka
instance:
hostname: localhost
spring:
application:
name: eureka-service package com.yun;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/Hello")
public class Controller {
@RequestMapping("/World")
public String helloWorld(String s) {
System.out.println("Received: " + s);
return "Received: " + s;
}
} package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // Register as a service provider
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}2.4 Service Consumption
Three invocation methods are shown: RestTemplate + Ribbon , Feign , and LoadBalanced RestTemplate . The article provides the necessary Maven dependency, bean configuration, and consumer controller.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency> server:
port: 8702 # consumer port
eureka:
client:
service-url:
defaultZone: http://<${eureka.instance.hostname}>:8700/eureka
instance:
hostname: localhost
spring:
application:
name: eureka-consumer package com.yun.beans;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class Beans {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
} package com.yun.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/Hello")
public class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/Consumer")
public String helloWorld(String s) {
// Using LoadBalanced RestTemplate (third method)
String result = restTemplate.getForObject("http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);
return result;
}
}2.5 High Availability of Eureka Server
The guide explains how to run three Eureka instances on ports 8699, 8698, and 8697, modify each instance’s application.yml to point to the other peers, and verify that service discovery continues to work when one or two servers are stopped, demonstrating cluster fail‑over.
After configuring the three servers, the consumer can still obtain the service list from the remaining Eureka nodes, proving that the system remains operational even when some registry instances are unavailable.
Finally, the article reminds readers to like the post and provides QR‑code style calls‑to‑action for joining a “Top‑Level Architect” community.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
