SpringCloud之Hystrix简介及示例

官档地址:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_circuit_breaker_hystrix_clients

Circuit Breaker: Hystrix Clients

断路器:Hystrix客户端

Netflix创建了一个名为Hystrix的库,它实现了断路器模式。在微服务体系结构中,通常有多个服务调用层,如下面的示例所示:

Hystrix

较低级别的服务(the lower level中服务失败会导致级联失败(cascading failure,一直持续到用户。当对特定服务的呼叫超过断路器阀值circuitBreaker.requestVolumeThreshold(默认:20个请求),故障率大于断路器 circuitBreaker.errorThresholdPercentage(默认值:>50%),在滚动窗口中定义的时间内metrics.rollingStats.timeInMilliseconds (默认: 10 秒)。断路器打开,不调用服务。在错误和开路的情况下,开发人员可以提供回退fallback

图13.2。Hystrix回退可以防止级联失败

如何包含Hystrix

pom.xml

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

 下面的例子显示了一个最小的Eureka服务器与Hystrix断路器:

启动类:@EnableCircuitBreaker  

调用方法:@HystrixCommand(fallbackMethod = "与所用该注解方法名称一致的方法名称")

@SpringBootApplication
@EnableCircuitBreaker
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

@Component
public class StoreIntegration {

    @HystrixCommand(fallbackMethod = "defaultStores")
    public Object getStores(Map<String, Object> parameters) {
        //do stuff that might fail
    }

    public Object defaultStores(Map<String, Object> parameters) {
        return /* something useful */;
    }
}

我的例子:

com.itmuch.cloud.ConsumerMovieRibbonApplication

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class ConsumerMovieRibbonApplication {

  @Bean
  @LoadBalanced
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

  public static void main(String[] args) {
    SpringApplication.run(ConsumerMovieRibbonApplication.class, args);
  }
}

com.itmuch.cloud.controller.MovieController

package com.itmuch.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.itmuch.cloud.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class MovieController {
  @Autowired
  private RestTemplate restTemplate;

  @GetMapping("/movie/{id}")
  @HystrixCommand(fallbackMethod = "findByIdFallback")
  public User findById(@PathVariable Long id) {
    return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
  }

  public User findByIdFallback(Long id) {
    User user = new User();
    user.setId(0L);
    return user;
  }
}

@HystrixCommand由Netflix的一个名为“javanica”的扩展库提供。Spring Cloud自动将带有该注释的Spring bean包装在连接到Hystrix断路器的代理(proxy)中。断路器计算什么时候打开和关闭断路器,以及在发生故障时应该做什么

要配置@HystrixCommand,可以使用commandProperties属性和@HystrixProperty注释列表。

更多细节参考:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration

有关可用属性的详细信息,请参见Hystrix wiki:https://github.com/Netflix/Hystrix/wiki/Configuration

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值