7 Ocak 2021 Perşembe

SpringCloud Netflix Ribbon

Giriş 
Açıklaması şöyle
Netflix Ribbon is a client-side library for load balancing. So BookMyHotel WebApp will use the Ribbon to load balance between these microservice instances, choose a microservice instance-based other on the load balancing algorithm, and connect to it. 

In Eureka Clients, a background thread runs at the client-side, connecting to the Eureka Server and retrieving the complete registry. The complete registry is cached at the client-end, so WebApp will look at its local cache to retrieve registration. Background instances thread will sync with Discovery server at the interval of 30 seconds.

If WebApp tries to connect the Registration microservice instance which is down, it will get an error. But we don’t want WebApp to fail; we want it to retry to next available instance anRibbon can configure this retryRibbon can configure this retry. We can also configure the count of retry and timeout for the same i.e. it will help BookMyHotel WebApo keep retying until some count until it finds a working instance if not found, then time out.
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@LoadBalanced anotasyonu yazısına bakabilirsiniz

Eureka kullanarak
Şeklen şöyle


Açıklaması şöyle
This information can be leveraged by tools for various purposes.

Client-side load balancers like Ribbon to make load balancing decisions
Ribbon reads the status attribute and considers only the instances with UP status for load balancing. Ribbon, however, does not invoke the healthCheckUrl but relies on published instance status available in the registry.

Örnek - Eureka kullanmadan
application.yml şöyle olsun.  "http://ping-server/..." adresine erişince listofServers listesindeki adreslerden birisine yönlendirilir.
ping-server:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:9092,localhost:9999
    ServerListRefreshInterval: 15000
Şöyle yaparızz
@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);
  }
}

Hiç yorum yok:

Yorum Gönder