Step-by-Step Guide to Building a Spring Cloud Microservices Architecture with Eureka, Ribbon, and Feign
This tutorial walks through the fundamentals of microservices, setting up a Spring Cloud project with Eureka service registry, configuring server and client modules, implementing load‑balanced calls using Ribbon and Feign, and achieving high availability with multiple Eureka instances, all illustrated with code snippets and configuration files.
Microservice Basics
The article introduces microservice concepts using a school‑department analogy, explaining how Eureka acts as a service‑registry (the teacher list), Zuul as a gateway (the school gatekeeper), and Hystrix as a circuit‑breaker (volunteers handling failures).
Project Initialization
It details creating a Maven project in IntelliJ IDEA (File → New → Project), selecting an empty project, configuring Maven settings, and adding Spring Boot 1.5.7 and Spring Cloud Edgware.SR5 dependencies.
Eureka Server Configuration
Configuration files for the Eureka server are provided, including application.yml (port 8700, registration disabled) and pom.xml with Spring Boot parent and spring-cloud-starter-netflix-eureka-server dependency. The Java entry class is:
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);
}
}Running this class starts the Eureka dashboard, accessible at the configured port.
Service Provider (Eureka Service) Setup
The provider module includes application.yml (port 8701, Eureka client pointing to the server) and a simple REST controller:
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("传入的值为:" + s);
return "传入的值为:" + s;
}
}The main class enables discovery with @EnableDiscoveryClient and starts the service.
Service Consumer Configuration
The consumer module adds the Ribbon dependency, configures application.yml (port 8702, Eureka client pointing to the server), and defines a load‑balanced RestTemplate bean:
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();
}
}The consumer controller demonstrates three invocation styles; the preferred one uses the load‑balanced RestTemplate:
package com.yun.controller;
import org.springframework.beans.factory.annotation.Autowired;
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) {
System.out.println("传入的值为:" + s);
String forObject = restTemplate.getForObject("http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);
return forObject;
}
}Running the consumer and invoking http://localhost:8702/Hello/Consumer?s=example demonstrates service discovery and client‑side load balancing.
Load‑Balancing and Call Methods
The three call methods covered are: (1) direct URL access, (2) using LoadBalancerClient to pick an instance, and (3) using a
@LoadBalanced RestTemplatewhich resolves the service name automatically.
High Availability of Eureka Server
The guide shows how to deploy three Eureka instances on ports 8699, 8698, and 8697, each with identical application.yml (same application name) and adjusted server ports. After starting all instances, the registry shows all services, and clients continue to discover providers even if one or two Eureka nodes fail, illustrating fault‑tolerant service discovery.
Finally, the article encourages readers to like the post and join the author’s architecture 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.
