我已经为 Zuul 配置了到其他微服务的静态路由。有没有办法在调用其他服务时启用 CircuitBreaker?
问问题
885 次
1 回答
1
正如您所说,Zuul 会自动将每条路线包装在Ribbon
和Hystrix
. 但它也很容易与微服务集成Ribbon
以及Hystrix
在微服务之间集成。您还可以使用它Feign
来处理 REST 调用。
想象一下,您有两个服务serviceA
and serviceB
,并且您想使用andserviceA
来调用它们。假设您在默认端口 ( )上运行服务器serviceB
Ribbon
Hystrix
Eureka
localhost
8761
服务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 回答