2017년 6월 4일 일요일

Spring cloud ribbon 설정

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

댓글 없음 :

댓글 쓰기