SpringCloud Tutorial 05: Load Balancing with Feign

SpringCloud Tutorial 05: Load Balancing with Feign

In our previous tutorial, SpringCloud Tutorial 03: Load Balancing with Ribbon, we explored how to use the Ribbon to achieve load balancing for clients. While using Ribbon with RestTemplate is straightforward, it still requires writing boilerplate code for each remote service call. Spring Cloud Feign, building upon the Ribbon, provides a more elegant solution. With Feign, you can create a client by simply defining an interface and configuring it using annotations. This article will guide you through the process of using Feign for client load balancing, divided into the following steps:

Step 1: Create a Project

Create a new project using Spring Boot, named SpringBootFeignServer.

Step 2: Introduce Dependencies

In your project’s pom.xml file, add the following dependencies:

<dependencies>
    <!-- Eureka Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- Feign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- Spring Cloud Dependencies -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.SR2</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>

Step 3: Modify the Configuration File

In your application.properties file, add the following configuration:

server:
  port: 8015

spring:
  application:
    name: feign-server
  eureka:
    client:
      service-url:
        default: http://localhost:8761/eureka

feign:
  hystrix:
    enabled: true

Step 4: Add Annotations and Enable Feign Clients

In your main class, add the following annotations:

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

Step 5: Cross-Call Service

Create a simple interface method call:

@RestController
public class FeignController {
    @Autowired
    private FeignService feignService;

    @GetMapping("getCompany")
    public String getCompany(String id) {
        String company = feignService.getCompany(id);
        System.out.println(company);
        return company;
    }
}

Define the service interface using the @FeignClient annotation:

@FeignClient(value = "company-server")
public interface FeignService {
    @GetMapping(value = "/company/get")
    String getCompany(@RequestParam(value = "id") String id);
}

Step 6: Start Project and Test

Start your project and access the interface http://localhost:8015/getCompany?id=36. You should see the service automatically load-balance between two servers.

This article has demonstrated how to use Feign for client load balancing in Spring Cloud. By following these steps, you can create a more elegant and efficient client using Feign.