Cloud Native 19 min read

Step‑by‑Step Guide to Building Spring Cloud Microservices with Eureka, Ribbon, and Feign

This article provides a comprehensive tutorial on creating Spring Cloud microservices, covering the fundamentals of microservice architecture, project initialization with Maven and IntelliJ, configuring Eureka registration servers, implementing service providers and consumers, using Ribbon and Feign for load‑balanced calls, and setting up high‑availability Eureka clusters.

Top Architect
Top Architect
Top Architect
Step‑by‑Step Guide to Building Spring Cloud Microservices with Eureka, Ribbon, and Feign

The author, a senior architect, introduces microservice basics by comparing monolithic servers to specialized teaching groups, explaining how Eureka acts as a service registry, Zuul as a gateway, and Hystrix as a circuit breaker.

1. Project Initialization – Create a Maven project in IntelliJ (File → New → Project), select an empty project, configure the Maven settings (path to Maven home and settings.xml), and add the required modules.

2. Server (Eureka) Setup

pom.xml for the Eureka server:

<?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>
    <name>springcloud-eureka-server</name>
    <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>

application.yml for the server (port 8700, no registration to itself):

server:
  port: 8700

eureka:
  client:
    service-url:
      defaultZone: http://\${eureka.instance.hostname}:\${server.port}/eureka
    register-with-eureka: false
    fetch-registry: false
  instance:
    hostname: localhost

spring:
  application:
    name: eureka-server

Main class:

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 (e.g., http://localhost:8700/).

3. Service Provider (Eureka Service) Setup

application.yml (port 8701, registers to the Eureka server):

server:
  port: 8701

eureka:
  client:
    service-url:
      defaultZone: http://\${eureka.instance.hostname}:8700/eureka
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: localhost

spring:
  application:
    name: eureka-service

Main class:

package com.yun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

Controller providing a simple endpoint:

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;
    }
}

After starting the provider, the service appears in the Eureka dashboard.

4. Service Consumer (Eureka Consumer) Setup

application.yml (port 8702, also registers to Eureka):

server:
  port: 8702

eureka:
  client:
    service-url:
      defaultZone: http://\${eureka.instance.hostname}:8700/eureka
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: localhost

spring:
  application:
    name: eureka-consumer

Main class:

package com.yun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerApplication.class, args);
    }
}

Consumer controller using Ribbon (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) {
        // Using load‑balanced RestTemplate with service name
        String result = restTemplate.getForObject("http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);
        return result;
    }
}

Bean configuration to expose a @LoadBalanced RestTemplate:

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();
    }
}

With the server, provider, and consumer running, accessing http://localhost:8702/Hello/Consumer?s=example demonstrates client‑side load balancing via Ribbon or Feign.

5. Service Call Methods

RestTemplate + Ribbon (manual load balancing).

Feign (declarative REST client).

Both approaches rely on Eureka for service discovery and can be switched by adding the appropriate dependencies.

6. High‑Availability Eureka Cluster

The article shows how to configure three Eureka instances (ports 8699, 8698, 8697) with identical application.yml files, start them as separate modules, and register service providers to all instances. Clients can list multiple Eureka servers in their defaultZone URLs to achieve fault tolerance. Even if one server goes down, the remaining servers continue to provide the service list, and cached entries are cleared when the consumer restarts.

Overall, the guide walks readers through the complete lifecycle of building, registering, discovering, and consuming Spring Cloud microservices, including clustering for high availability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMicroservicesSpring BootfeigneurekaSpring CloudRibbon
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.