0

I have configured Zuul with static routes to other micro services. Is there a way to enable CircuitBreaker when calling out to other services?

blindstack
  • 251
  • 3
  • 10

1 Answers1

1

As you said Zuul will automatically wrap every routes inside Ribbon and Hystrix. But it's also quite easy to integrate with Ribbon and Hystrix between microservices. You can also use Feign to handle REST calls.

Imagine you have two services serviceA and serviceB, and that you want serviceA to call serviceB using Ribbon and Hystrix. Let's assume you have Eureka server running on the localhost and default port (8761)

serviceA

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceAapplication{
    public static void main(String[] args) {
        SpringApplication.run(ServiceAapplication.class, args);
    }
}

@RestController()
@RequestMapping("/rest")
class DummyRestController{

    @RequestMapping(method = RequestMethod.GET, path = "/hello")
    String hello(){
        return "Hello World!";
    }
}

ServiceB

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients
public class ServiceBapplication{
    public static void main(String[] args) {
        SpringApplication.run(ServiceBapplication.class, args);
    }
}

@FeignClient(value = "serviceA") //must be same name as the service name in Eureka
@RibbonClient(name = "serviceA") //must be same name as the service name in Eureka
interface ServiceAClient {
    @RequestMapping(method = RequestMethod.GET, value = "/rest/hello")
        String helloFromServiceA();
 }

@RestController()
@RequestMapping("/foo")
class DummyRestController{

    private final ServiceAclient client;

    @Autowired
    DummyRestController(ServiceAclient client){
        this.client = client;
    }

    @RequestMapping(method = RequestMethod.GET, path = "/bar")
    String hello(){
        return client.helloFromServiceA();
    }
}

Now if you do a GET on serviceB with foo/bar it will use:

  • Eureka to find the host and port for serviceA
  • Ribbon to load balance between multiple instances of serviceA
  • The whole thing is wrapped into a Hystrix command

Because of the @EnableCircuitBreaker annotation, your serviceB will expose a Hystrix stream. If you run a HystrixDashboard server you can connect to this stream and you will see a helloFromServiceA command on the dashboard.

You can configure Ribbon and Hystrix in your usual configuration file or using a separate configuration class in the @FeignClient and @RibbonClient annotations. You can find more info here

Important: If you want Ribbon to retry on a different instance during a timeout, make sure the Hystrix timeout is higher than the Ribbon time out. See this answer

Community
  • 1
  • 1
phoenix7360
  • 2,807
  • 6
  • 30
  • 41