0
votes

How to loadbalance micro services via Ribbon (Not feign). I have 3 micro services "M1", "M2" and "M2_duplication", "M1" is communicating with "M2" via feign. I want if "M2" get too much traffic, the requests will be forwarded to the "M2_duplication". How is this possible via @ribbonclient ?

POM M1:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

The feign call in M1:

//name is taken from Eureka(service registry)
    @FeignClient(name = "M1")
    public interface M1ServiceClient {
        @RequestMapping(method = RequestMethod.GET, value = "/getAllM2")
        Map<String, String> getAllM2(); 
    }

Application M1:

@EnableConfigurationProperties()
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class PortefeuilleApplication {
    public static void main(String[] args) {
        SpringApplication.run(PortefeuilleApplication.class, args);
    }
}
1
the micro services can register and communicate via Eureka , but if add on the spring boot app class @RibbonClient(name="administration") // no more communication between M1 and M2xGenius

1 Answers

2
votes

Your question is rather vague ... for example, you specifically state 'not feign' and then show a FeignClient. Nevertheless it looks like you are asking how to implement an availability pattern for your ribbon load balancer. To do this, you create a Ribbon Configuration class and then override the load balancer strategy rule. For example:

@Configuration
public class MyConfiguration {
  @Bean
  public IRule ribbonRule() {
    return new RoundRobinRule();
  }
}

There are a number of existing Ribbon Rule strategies around availability e.g. AvailabilityFilteringRule or BestAvailableFilter, but if none of these suit you could also write your own Rule. Once you have your class, amend your RibbonClient annotation to reference it, e.g:

@RibbonClient(name = "myClient", configuration = MyConfiguration.class)

You can find more information here: https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers