0. 개요
- 2개의 서버가 LoadBalance(라운드 로빈)되는 설정
- localhost:8082,localhost:7777
- localhost:7777은 없는 주소 이므로 Exception 발생. 그러나 Ribbon Retry로 항상 성공
- Round Robin Client Load Balancing & Retry.
01. RestTemplate에 Ribbon 적용하기
git checkout tags/step-3-ribbon-retry -b step-3-ribbon-retry
1. [display] build.gradle에 dependency 추가
compile('org.springframework.cloud:spring-cloud-starter-netflix-ribbon') compile('org.springframework.retry:spring-retry:1.2.2.RELEASE')
2. [display] DisplayApplication의 RestTemplate 빈에 @LoadBalanced 추가
@LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
3. [display] ProductRemoteServiceImpl에서 주소 제거하고 `product` 로 변경
public class ProductRemoteServiceImpl implements ProductRemoteService { //private static final String url = "http://localhost:8082/products/"; private static final String url = "http://product/products/"; private final RestTemplate restTemplate; }
4. [display] application.yml에 ribbon 설정 넣기
product: ribbon: listOfServers: localhost:7777, localhost:8082 MaxAutoRetries: 0 MaxAutoRetriesNextServer: 1
5. 실행결과
2020-02-25 17:06:30.875 INFO 79048 --- [teServiceImpl-1] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client product initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=product,current list of Servers=[localhost:7777, localhost:8082],Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:2; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;] },Server stats: [[Server:localhost:7777; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 09:00:00 KST 1970; First connection made: Thu Jan 01 09:00:00 KST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0] , [Server:localhost:8082; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 09:00:00 KST 1970; First connection made: Thu Jan 01 09:00:00 KST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0] ]}ServerList:com.netflix.loadbalancer.ConfigurationBasedServerList@5d174c88 2020-02-25 17:06:31.823 INFO 79048 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: product.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
6. 정리
- Ribbon은 여러 Component에 내장되어있으며, 이를 통해 Client Load Balancing이 수행 가능하다.
- Ribbon에는 매우 다양한 설정이 가능하다 (서버선택, 실패시 Skip 시간, Ping 체크)
- Ribbon에는 Retry기능이 내장 되어있다.
- Eureka와 함께 사용될 때 강력하다 (뒤에 실습)