springcloud之Ribbon

springcloud之Ribbon

Ribbon primarily provides client-side load balancing algorithms.

Other features:

  • Service Discovery Integration – Ribbon load balancers provide service discovery in dynamic environments like a cloud. Integration with Eureka and Netflix service discovery component is included in the ribbon library
  • Fault Tolerance – the Ribbon API can dynamically determine whether the servers are up and running in a live environment and can detect those servers that are down
  • Configurable load-balancing rules – Ribbon supports RoundRobinRule, AvailabilityFilteringRule, WeightedResponseTimeRule out of the box and also supports defining custom rules

Dependency: spring-cloud-starter-netflix-ribbon

load balance strategy:

Screen Shot 2020-12-30 at 11.44.33 AM

Configuration

  • rule - load balancing rule
  • ping - mechanism we use to determine the server's availability in real-time
  • ServerList - can be dynamic or static. I
public class RibbonConfiguration {

@Autowired
IClientConfig ribbonClientConfig;

@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}

@Bean
public IRule ribbonRule(IClientConfig config) {
return new WeightedResponseTimeRule();
}
}
spring:
application:
name: spring-cloud-ribbon

server:
port: 8888

ping-server:
ribbon:
eureka:
enabled: false
listOfServers: localhost:9092,localhost:9999
ServerListRefreshInterval: 15000

Notes: Disabled Eureka service discovery component, by setting eureka: enabled to falseDefined the list of servers available for load balancing, in this case, 2 servers

code

@SpringBootApplication
@RestController
@RibbonClient(
name = "ping-a-server",
configuration = RibbonConfiguration.class)
public class ServerLocationApp {

@LoadBalanced
@Bean
RestTemplate getRestTemplate() {
return new RestTemplate();
}

@Autowired
RestTemplate restTemplate;

@RequestMapping("/server-location")
public String serverLocation() {
return this.restTemplate.getForObject(
"http://ping-server/locaus", String.class);
}

public static void main(String[] args) {
SpringApplication.run(ServerLocationApp.class, args);
}
}

annotated the RestTemplate with @LoadBalanced which suggests that we want this to be load balanced and in this case with Ribbon.

Failure Resiliencey

Ribbon API can determine the server's availability through the constant pinging of servers at regular intervals and has a capability of skipping the servers which are not live.

How to disable circuit breaker: niws.loadbalancer.availabilityFilteringRule.filterCircuitTripped to false.


https://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon