0

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);
    }
}
Marek J
  • 1,364
  • 8
  • 18
  • 33
xGenius
  • 75
  • 3
  • 9
  • 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 M2 – xGenius Apr 24 '16 at 19:58

1 Answers1

2

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

RobP
  • 706
  • 1
  • 8
  • 20
  • Thank you RobP, this is almost what i want, i want to set a rule if M2 have 60% of charges, M2_duplication will handle the communication. is this possible with ribbon? – xGenius Apr 26 '16 at 09:23
  • I'm glad my answer has helped. What do you mean by charges? Calls? Do you want 60% of calls to M2 and 40% to M2_duplication? Or? The AvailabilityFilteringRule allows you to specify an active connections limit (see the link in the answer), perhaps this will help? – RobP Apr 26 '16 at 09:40
  • yes the link that you sent me was very helpful, but what if i wanna to send for each communication to a different Micro service each time ? – xGenius Apr 26 '16 at 17:10
  • Then you would use the RoundRobinRule, which hits each available Ribbon endpoint in turn. – RobP Apr 27 '16 at 07:42
  • RobP, i asked a question about the current executed instance/server by Ribbon, if you can help me please, i'm stuck on it for days now. Thanks in advance – xGenius May 13 '16 at 09:16