1

I am trying to set up micro service architecture using spring boot. I have created it by separating the components and using techniques like ribbon, eureka naming server, zuul API etc.

service1 reading inputs from a db and calling service2 through ribbon, eureka and zuul.

List<Object> inputs= ...
for(Object input: inputs){
    service2Proxy.process(input); // calling service 2
}

Is it possible to call the service2 based on the number of available instance of service2 ? ie, If there are 3 instance of service2 is available at a time, I need to call only 3 requests to service2 at a time.

What is the solution for this problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
din_oops
  • 698
  • 1
  • 9
  • 27
  • Any specific reason why you want to do that, as the Ribbon client side load balancer will keep on distributing the call with round robin policy. – Shailendra Aug 27 '18 at 14:30

1 Answers1

0

If you are using LoadBalancerClient directly (by autowiring) and then use LoadBalancerClient.choose(serviceId), then you can get hold of the ServiceInstance it returns. This contains the uri of the service. So after each invocation store this uri in a set of uri's and before making another call check if you have already used the server instance already.If all servers have been exhausted wait for further inputs. When you are ready to process again, clear the set and repeat. As explained in the docs here, the code would be somewhat like this

@Autowired
    private LoadBalancerClient loadBalancer;

    public void doStuff() {
        ServiceInstance instance = loadBalancer.choose("stores");
        URI serverUri = instance.getUri();
     // use you logic here
        URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));

    }
Shailendra
  • 8,874
  • 2
  • 28
  • 37