0

我已经为 Zuul 配置了到其他微服务的静态路由。有没有办法在调用其他服务时启用 CircuitBreaker?

4

1 回答 1

1

正如您所说,Zuul 会自动将每条路线包装在RibbonHystrix. 但它也很容易与微服务集成Ribbon以及Hystrix在微服务之间集成。您还可以使用它Feign来处理 REST 调用。

想象一下,您有两个服务serviceAand serviceB,并且您想使用andserviceA来调用它们。假设您在默认端口 ( )上运行服务器serviceBRibbonHystrixEurekalocalhost8761

服务A

@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!";
    }
}

服务B

@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();
    }
}

现在,如果您对 serviceB 执行 GET 操作,foo/bar它将使用:

  • 尤里卡找到主机和端口serviceA
  • 功能区在多个实例之间进行负载平衡serviceA
  • 整个事情被包装成一个Hystrix命令

由于@EnableCircuitBreaker注释,您serviceB将公开一个Hystrix流。如果你运行一个HystrixDashboard服务器,你可以连接到这个流,你会helloFromServiceA在仪表板上看到一个命令。

您可以在通常的配置文件中配置Ribbon和,或者在和注释Hystrix中使用单独的配置类。你可以在这里找到更多信息@FeignClient@RibbonClient

重要提示:如果您想Ribbon在超时期间重试不同的实例,请确保Hystrix超时时间高于超时Ribbon时间。看到这个答案

于 2016-05-27T08:17:24.480 回答