使用 Hystrix Spring Cloud 进行单元测试回退的任何样本

     2023-03-16     260

关键词:

【中文标题】使用 Hystrix Spring Cloud 进行单元测试回退的任何样本【英文标题】:Any samples to unit test fallback using Hystrix Spring Cloud 【发布时间】:2015-05-16 08:32:24 【问题描述】:

我希望测试以下场景:

    hystrix.command.default.execution.isolation.thread.timeoutInMillisecond 值设置为较低的值,然后查看我的应用程序的行为。 检查我的后备方法是使用单元测试调用的。

请有人提供样品链接。

【问题讨论】:

@Spencergibb 请你帮忙 【参考方案1】:

在您致电客户之前,请先在您的单元测试用例中打开电路。确保调用了回退。您可以从回退中返回一个常量或添加一些日志语句。 重置电路。

@Test
public void testSendOrder_openCircuit() 
    String order = null;
    ServiceResponse response = null;

    order = loadFile("/order.json");
    // use this in case of feign hystrix
    ConfigurationManager.getConfigInstance()
            .setProperty("hystrix.command.default.circuitBreaker.forceOpen", "true");
   // use this in case of just hystrix
   System.setProperty("hystrix.command.default.circuitBreaker.forceOpen", "true");

    response = client.sendOrder(order);

    assertThat(response.getResultStatus()).isEqualTo("Fallback");
    // DONT forget to reset
    ConfigurationManager.getConfigInstance()
            .setProperty("hystrix.command.default.circuitBreaker.forceOpen", "false");
    // use this in case of just hystrix
    System.setProperty("hystrix.command.default.circuitBreaker.forceOpen", "false");


【讨论】:

【参考方案2】:

一个真正的用法可以在下面找到。在测试类中启用 Hystrix 的关键是这两个注解: @EnableCircuitBreaker @EnableAspectJAutoProxy

class ClipboardService 

    @HystrixCommand(fallbackMethod = "getNextClipboardFallback")
    public Task getNextClipboard(int numberOfTasks) 
        doYourExternalSystemCallHere....
    

    public Task getNextClipboardFallback(int numberOfTasks) 
        return null;
    



@RunWith(SpringRunner.class)
@EnableCircuitBreaker
@EnableAspectJAutoProxy
@TestPropertySource("classpath:test.properties")
@ContextConfiguration(classes = ClipboardService.class)
public class ClipboardServiceIT 

    private MockRestServiceServer mockServer;

    @Autowired
    private ClipboardService clipboardService;

    @Before
    public void setUp() 
        this.mockServer = MockRestServiceServer.createServer(restTemplate);
    

    @Test
    public void testGetNextClipboardWithBadRequest() 
        mockServer.expect(ExpectedCount.once(), requestTo("https://getDocument.com?task=1")).andExpect(method(HttpMethod.GET))
        .andRespond(MockRestResponseCreators.withStatus(HttpStatus.BAD_REQUEST));
        Task nextClipboard = clipboardService.getNextClipboard(1);
            assertNull(nextClipboard); // this should be answered by your fallBack method
        
    

【讨论】:

出色的答案为我省去了很多工作和挫折。

Spring Cloud Hystrix 仪表板不与 OAuth 一起使用

】SpringCloudHystrix仪表板不与OAuth一起使用【英文标题】:SpringCloudHystrixDashboardnotworkingwithOAuth【发布时间】:2015-06-1919:10:50【问题描述】:我正在尝试使用HystrixDashboard运行一个简单的Spring应用程序。我还在这个应用程序上启用了Sp... 查看详情

Spring-Cloud、Hystrix 和 JPA - LazyInitializationException

】Spring-Cloud、Hystrix和JPA-LazyInitializationException【英文标题】:Spring-Cloud,HystrixandJPA-LazyInitializationException【发布时间】:2015-05-3104:09:16【问题描述】:我在尝试将Hystrix集成到现有的SpringBoot应用程序时遇到以下问题。我正在使用带... 查看详情

使用 Hystrix Spring Cloud 进行单元测试回退的任何样本

】使用HystrixSpringCloud进行单元测试回退的任何样本【英文标题】:AnysamplestounittestfallbackusingHystrixSpringCloud【发布时间】:2015-05-1608:32:24【问题描述】:我希望测试以下场景:将hystrix.command.default.execution.isolation.thread.timeoutInMilliseco... 查看详情

Spring Cloud Gateway 中的 Hystrix 命令

】SpringCloudGateway中的Hystrix命令【英文标题】:HystrixCommandinSpringCloudGateway【发布时间】:2019-01-2121:35:58【问题描述】:我正在使用Springcloudstartergateway2.0.1.RELEASE和Starternetflixhystrix。是否可以像下面这样在路由定义中提供HystrixCommand... 查看详情

将 Zuul、Hystrix(和 Feign)与 Spring Cloud HATEOAS 一起使用时如何转发标头?

】将Zuul、Hystrix(和Feign)与SpringCloudHATEOAS一起使用时如何转发标头?【英文标题】:HowtoforwardheaderswhenusingZuul,Hystrix(andFeign)withSpringCloudHATEOAS?【发布时间】:2019-08-1619:54:51【问题描述】:上下文我的微服务应用基于spring-cloud:在... 查看详情

spring-cloud系列-hystrix源码解析

这一篇我们主要来看下Hystrix是怎样在OpenFiegn上做进一层封装的,主要梳理Hystrix封装逻辑。一、执行的熔断判断1、执行案例​我们配置了Hystrix的使用逻辑后@RequestMapping(value="simple",method=RequestMethod.GET)publicStringsi... 查看详情

spring-cloud hystrix CompletableFuture

】spring-cloudhystrixCompletableFuture【英文标题】:【发布时间】:2016-06-1819:39:16【问题描述】:我正在使用spring-cloudbrixton.m5做一些工作,并试图让AsyncRestTemplate工作以异步地将多个微服务调用组合成一个协调服务响应。我发现spencergi... 查看详情

8.spring-cloud-hystrix之异常处理

...调用服务执行HsytrixCommand实现的run()方法抛出异常时,除HystrixBadRequestException之外,其他异常都会认为是Hystrix命令执行失败并触发服务降级处理逻辑.异常处理当Hystrix命令因为异常(除了HystrixBadRequestException异常)进入服务降级逻... 查看详情

Spring Cloud Gateway 路由不起作用,Hystrix Dashboard 崩溃加载

】SpringCloudGateway路由不起作用,HystrixDashboard崩溃加载【英文标题】:SpringCloudGatewayroutesdonotworkandHystrixDashboardcrashedloading【发布时间】:2021-06-2720:17:48【问题描述】:在来自youtube的tutorial中,作者使用SpringCloudGateway、Hystrix、Netflix... 查看详情

0502-hystrix保护应用-简介,使用,健康指标等(代码片段)

一、概述  参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_circuit_breaker_hystrix_clients  源码wiki:https://github.com/Netflix/Hystrix/wiki  Netflix创建了一个名为Hystrix的库,实现 查看详情

Spring cloud Camden.SR1 hystrix.stream 挂起

】SpringcloudCamden.SR1hystrix.stream挂起【英文标题】:SpringcloudCamden.SR1hystrix.streamhangs【发布时间】:2017-03-0906:10:37【问题描述】:我正在尝试在SpringCloud服务中启用hystrix流,在SpringBoot应用程序中使用Camden.SR1。我通过编译时依赖来启... 查看详情

无法使用 Spring Cloud 连接到 Hystrix Dashboard 的 Command Metric Stream

】无法使用SpringCloud连接到HystrixDashboard的CommandMetricStream【英文标题】:UnabletoconnecttoCommandMetricStreamforHystrixDashboardwithSpringCloud【发布时间】:2018-09-2208:31:27【问题描述】:我有SpringCloud的微服务项目,来自父级的sn-p:<parent>&l... 查看详情

spring-cloud之hystrix(代码片段)

Hystrix是Netflix旗下的一个延迟和容错库,旨在隔离对远程系统、服务和第三方库的访问,停止级联故障并在故障不可避免的复杂分布式系统中启用弹性配置。下面我们通过几个示例来让你快速了解它的使用原文地址:https://blog.vch... 查看详情

Spring Cloud Feign 客户端调用是不是在 hystrix 命令中执行?

】SpringCloudFeign客户端调用是不是在hystrix命令中执行?【英文标题】:DoesSpringCloudFeignclientcallexecuteinsidehystrixcommand?SpringCloudFeign客户端调用是否在hystrix命令中执行?【发布时间】:2015-12-0514:36:38【问题描述】:我正在使用此示例... 查看详情

10.spring-cloud-hystrix之熔断监控hystrixdashboard单个应用(代码片段)

SpringCloud完美的整合Hystrix-dashboard,Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过HystrixDashboard我们可以在直观地看到各HystrixCommand的请求响应时间,请求成功率等数据。可以实时反馈信息帮助我们快速发现系统中,但是... 查看详情

带有 Hystrix 断路器超时的 Spring Cloud Feign 客户端默认为 2 秒

】带有Hystrix断路器超时的SpringCloudFeign客户端默认为2秒【英文标题】:SpringCloudFeignclientwithHystrixcircuit-breakertimeoutdefaultsin2seconds【发布时间】:2020-11-2715:43:42【问题描述】:可通过GitHub上的项目重现:spring-cloud-feign-hystrix-timeout-pro... 查看详情

无法在 Spring Cloud 中获取 /hystrix.stream

】无法在SpringCloud中获取/hystrix.stream【英文标题】:Unabletoget/hystrix.streaminSpringCloud【发布时间】:2017-03-1918:39:39【问题描述】:我创建了一个微服务,它具有以下SpringCloud版本Camden.SR2的依赖项。春季启动1.4.1。http://localhost:8080/hyst... 查看详情

Spring Cloud Turbine AMQP 不适用于 Hystrix 仪表板

】SpringCloudTurbineAMQP不适用于Hystrix仪表板【英文标题】:SpringCloudTurbineAMQPdoesn\'tworkingwithHystrixDashboard【发布时间】:2018-07-0707:21:02【问题描述】:我正在尝试设置spring云涡轮amqp和hystrix仪表板。但是当我启动所有应用程序时,hystr... 查看详情