hystrix简单使用——fallback

本文主要介绍fallback,目的是能够在程序中看到fallback的处理。

主要参考:

官网上的原话是这么说的
Hystrix will execute this fallback for all types of failure such as run() failure, timeout, thread pool or semaphore rejection, and circuit-breaker short-circuiting.

现在实现一个默认就是出错的然后看效果是什么样子的。正常应该直接执行getFallback()里面的内容。

1、添加maven依赖

<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core -->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.12</version>
        </dependency>

2、创建一个继承hystrixCommand的类

package com.xueyou.hystrixdemo.v2;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;


public class CommandHelloworldWithFallBack extends HystrixCommand<String> {
    private String name;

    public CommandHelloworldWithFallBack(HystrixCommandGroupKey group, String name) {
        super(group);
        this.name = name;
    }

    @Override
    protected String getFallback() {
        return "fall back " + name;
    }

    @Override
    protected String run() {
        throw new RuntimeException("this command always fails");
    }
}

3、添加测试类进行测试

package com.xueyou.hystrixdemo.v2;

import com.netflix.hystrix.HystrixCommandGroupKey;

public class HelloWorldFallBack {
    public static void main(String[] args) {
        String result = new CommandHelloworldWithFallBack(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"), "aaabbbccc").execute();
        String result2 = new CommandHelloworldWithFallBack(HystrixCommandGroupKey.Factory.asKey("ExampleGroup2"), "123sss").execute();
        System.out.println(result);
        System.out.println(result2);
    }
}

4、运行结果:



根据官网上的解释,每次执行run()方法的时候都会出现异常,但是这里没有返回异常,而是通过执行getFallback()方法然后返回getFallback()的返回值即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hystrix是一个用于处理分布式系统间的错误和延迟的库。它提供了一些机制来保护你的系统免受故障的影响,并且帮助你优雅地处理这些故障。 以下是使用Hystrix的步骤: 1. 添加依赖 在你的项目中添加Hystrix的依赖,例如: ```xml <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>1.5.18</version> </dependency> ``` 2. 定义命令 Hystrix的核心是命令模式。你需要定义一个继承自HystrixCommand的类,例如: ```java public class MyCommand extends HystrixCommand<String> { private final String name; public MyCommand(String name) { super(HystrixCommandGroupKey.Factory.asKey("MyGroup")); this.name = name; } @Override protected String run() throws Exception { // Do something that might fail return "Hello, " + name; } @Override protected String getFallback() { return "Fallback"; } } ``` 在这个例子中,我们定义了一个MyCommand类,它接收一个字符串作为参数,并返回一个字符串。在run()方法中,我们可以执行一些可能失败的操作,并在getFallback()方法中定义降级逻辑。 3. 执行命令 执行MyCommand命令的代码如下: ```java String result = new MyCommand("World").execute(); ``` 这将执行MyCommand命令,并返回结果。如果命令执行失败或超时,Hystrix将触发降级逻辑并返回fallback值。 4. 配置Hystrix 你可以使用Hystrix的配置来调整不同参数的值,例如: ```java HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(1000) .withCircuitBreakerRequestVolumeThreshold(10) .withCircuitBreakerErrorThresholdPercentage(50) .withCircuitBreakerSleepWindowInMilliseconds(5000) ``` 这些配置参数可以控制Hystrix的行为,例如设置命令的超时时间、熔断器的触发条件等。 以上就是使用Hystrix的基本流程,你可以根据自己的需求来使用Hystrix来保护你的分布式系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值