hystrix 单独使用_springcloudstarternetflixhystrix使用详解

前面文中我们学习了hystrix的原生使用(hystrix入门-原生API使用)和注解使用(hystrix进阶-注解hystrix-javanica使用),本文来看下hystrix在spring-boot中的使用。

首先还是先看个最简单的demo

第一步:添加依赖

<dependency>  <groupId>org.springframework.cloudgroupId>  <artifactId>spring-cloud-starter-netflix-hystrixartifactId>dependency>

第二步:开启配置

在配置类上添加@EnableCircuitBreaker注解

@SpringBootApplication@EnableCircuitBreaker//添加这个注解public class HystrixBootApplication {
      public static void main(String[] args) {
        SpringApplication.run(HystrixBootApplication.class, args);  }}

第三步:在需要开启熔断降级的方法上添加@HystrixCommand

@RestControllerpublic class DemoController {
        @HystrixCommand(fallbackMethod = "helloFallback")    @GetMapping("/hello")    public String hello(String msg){
            throw new RuntimeException("hello throw RuntimeException");    }    public String helloFallback(String msg, Throwable t) {
            return "helloFallback:" + msg + "," + t.getMessage();    }}

浏览器访问:http://localhost:8080/hello?msg=world,输出结果:helloFallback:world,hello throw RuntimeException

异常传播

@HystrixCommand(fallbackMethod = "helloFallback")@GetMapping("/hello")public String hello(){
        throw new HystrixBadRequestException("参数校验异常");}

只要抛出HystrixBadRequestException即可,就不会进入到fallback中。但是这样不太友好,业务代码跟hystrix耦合到了一起,两种解决办法:

第一种办法:可以让我们自定义的业务异常继承HystrixBadRequestException:

public static class BizException extends HystrixBadRequestException{
        public BizException(String message) {
            super(message);    }    public BizException(String message, Throwable cause) {
            super(message, cause);    }}

第二种办法:可以在@HystrixCommand上配置ignoreExceptions:

@HystrixCommand(fallbackMethod = "helloFallback",   ignoreExceptions = ParamException.class)@GetMapping("/world")public String world(){
        throw new ParamException("参数不能为空");}

熔断

@Res
  • 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、付费专栏及课程。

余额充值