Spring Cloud Ribbon
04 June 2017
Spring cloud ribbon을 통해서, 클라이언트 단에서 로드밸런싱이 가능합니다.
가장 먼저, eureka server를 세팅하고, 그 후 eureka client를 통해 실제 rest call을 받을 서버를 셋팅하도록 합니다.
해당 준비를 통해서, eureka server에는 eureka server 자신의 서버와 eureka client라는 두개의 서버가 등록되게 됩니다.

spring cloud ribbon을 통해서, 이렇게 등록된 서버의 name을 통해서 rest call이 가능합니다.
package org.blog.test;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RibbonClients
@RestController
@Slf4j
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/client/info")
public String getClientInfo() {
return restTemplate.getForObject("http://eureka-client/info", String.class);
}
@RequestMapping("/server/info")
public String getServerInfo() {
return restTemplate.getForObject("http://eureka-server/info", String.class);
}
}
위와 같은 설정을 통해서, eureka-server로 등록된 모든 서버 및 eureka-client로 등록된 모든 서버에 클라이언트 측 로드밸런싱을 통해 rest call을 할 수 있습니다.- 실행 결과

전체 예제는 아래 링크에서 다운로드 가능합니다.
https://gitlab.com/shashaka/ribbon-project