一、雪崩效应

在微服务架构中,通常有多个服务层调用,如果某个服务不可用,造成调用的服务也不可用,造成整个系统不可用的情况,叫做雪崩效应

二、Hystrix介绍

防雪崩利器Hystrix,基于Netflix对应的Hystrix。

Hystrix功能: 服务降级,服务熔断,依赖隔离, 监控(Hystrix Dashboard)

Hystrix是线程池隔离,自动实现了依赖隔离。

1、服务降级

优先核心服务,非核心服务不可用或弱可用

通过HystrixCommand注解指定

fallbackMethod(回退函数)中具体实现降级逻辑

三、Order工程中使用RestTemplate调用Product中的方法

 @GetMapping("/getProductInfoList")
public String getProductInfoList(){
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class); }

  返回结果

当Product服务关闭后,再次访问,将返回连接拒绝

然后使用Hystrix进行服务降级

在Order服务中增加引用

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency> <dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
</dependency>

  

增加注解EnableCircuitBreaker

或者用@SpringCloudApplication替换另外三个注解

增加HystrixController 类

@RestController
public class HystrixController { @HystrixCommand(fallbackMethod = "fallback")
@GetMapping("/getProductInfoList")
public String getProductInfoList(){
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class); } private String fallback(){
return "太拥挤了,请稍后再试~~";
}
}

  

再次调用,Product服务此时是关闭的。

说明服务降级了。

上面是单独写了一个fallback方法,那如何统一处理呢?

调用接口:

四、超时设置

超时时间设置为3秒
   @HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //超时时间设置为3秒
})
@GetMapping("/getProductInfoList")
public String getProductInfoList(){
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class); }

  

五、熔断

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController { //@HystrixCommand(fallbackMethod = "fallback")
//2、超时设置
/*@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //超时时间设置为3秒
})*/
//3.
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
})
@GetMapping("/getProductInfoList")
public String getProductInfoList(@RequestParam("number") Integer number){
if(number % 2 == 0){
return "success";
}
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class); } private String fallback(){
return "太拥挤了,请稍后再试~~";
} private String defaultFallback(){
return "默认提示:太拥挤了,请稍后再试~~";
}
}

  

Product工程中的方法

    @PostMapping("/listForOrder")
public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return productService.findList(productIdList);
}

  

调用:

number为2时成功返回

number为1时,触发熔断

熔断:

不停的调用 http://localhost:8081/getProductInfoList?number=1 。

然后调用http://localhost:8081/getProductInfoList?number=2, 也出现拥挤提示

然后再次调用http://localhost:8081/getProductInfoList?number=2 就正常了。

服务容错和Hystrix的更多相关文章

  1. 服务容错保护断路器Hystrix之七:做到自动降级

    从<高可用服务设计之二:Rate limiting 限流与降级>中的“自动降级”中,我们这边将系统遇到“危险”时采取的整套应急方案和措施统一称为降级或服务降级.想要帮助服务做到自动降级,需 ...

  2. 服务容错保护断路器Hystrix之五:配置

    接着<服务容错保护断路器Hystrix之二:Hystrix工作流程解析>中的<2.8.关于配置>再列举重要的配置如下 一.hystrix在生产中的建议 1.保持timeout的 ...

  3. 服务容错保护断路器Hystrix之三:断路器监控(Hystrix Dashboard)-单体监控

    turbine:英 [ˈtɜ:baɪn] 美 [ˈtɜ:rbaɪn] n.汽轮机;涡轮机;透平机 一.Hystrix Dashboard简介 在微服务架构中为了保证程序的可用性,防止程序出错导致网络阻 ...

  4. 服务容错保护断路器Hystrix之二:Hystrix工作流程解析

    一.总运行流程 当你发出请求后,hystrix是这么运行的 红圈 :Hystrix 命令执行失败,执行回退逻辑.也就是大家经常在文章中看到的“服务降级”. 绿圈 :四种情况会触发失败回退逻辑( fal ...

  5. Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】

    Spring Cloud(四):服务容错保护 Hystrix[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  分布式系统中经常会出现某个基础服务不可用 ...

  6. 白话SpringCloud | 第五章:服务容错保护(Hystrix)

    前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...

  7. Spring Cloud (8) 服务容错保护-Hystrix依赖隔离

    依赖隔离 docker使用舱壁模式来实现进程的隔离,使容器与容器之间不会互相影响.而Hystrix则使用该模式实现线程池的隔离,它会为每一个Hystrix命令创建一个独立的线程池,这样就算在某个Hys ...

  8. 服务容错保护断路器Hystrix之一:入门示例介绍(springcloud引入Hystrix的两种方式)

    限流知识<高可用服务设计之二:Rate limiting 限流与降级> 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的 ...

  9. Spring Cloud (7) 服务容错保护-Hystrix服务降级

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以互相调用,在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务通常会集群 ...

随机推荐

  1. java ----> 注解/反射

    注解 一个例子,摘自Junit-4.12.jar源码. 1 @Retention(RetentionPolicy.RUNTIME) 2 @Target({java.lang.annotation.El ...

  2. form表单的提交方式

    开发中表单提交是很常见的,表单的提交方式也多种方式. 1.使用submit按钮提交表单  <input type="submit"/> <!DOCTYPE htm ...

  3. php 中swoole安装

    1.下载扩展包: 网址:http://pecl.php.net/ 中(http://pecl.php.net/get/swoole-4.3.1.tgz) 2.解压安装包. 3.进入解压好的安装包. 4 ...

  4. sublim 配置 用户默认绑定的格式化文本快捷键

      //在绑定用户绑定配置中 加入 激活每个字母提示! "auto_complete": true,"auto_match_enabled": true, &q ...

  5. Cassandra.yaml 配置详解

    cluster_name 设置Cassandra集群的名称. 在Cassandra集群中,每一台服务器都必须具备相应的集群的名称.如果名称不一致,则当前Cassandra服务器无法加入集群. init ...

  6. 浅谈树状数组(为什么lowbit(x)=x&(-x)

    树状数组是一种支持单点修改和查询前缀和的数据结构 网上很多讲解它的博客了 这里重点讲一下为什么lowbit(x)=x&(-x) 树状数组代码量相对于线段树基本可以不计(太好写了) 因此NOIp ...

  7. 如何设置IntelliJ IDEA智能感知支持Jsp内置对象

    一.问题: 在IntelliJ IDEA 新建jsp页面,想通过内置对象获取查询参数,比如:request.getParameter("id"),虽然手动输入后能够运行成功,但是智 ...

  8. Go used as value问题

    练习Go变参时遇到一个报错:used as value 代码如下: // 错误代码 func myfunc(arg ...int) { for _, n := range arg { fmt.Prin ...

  9. <转载> 从算法上解读自动驾驶是如何实现的?

     科技新闻小鹏汽车2016-03-28 10:42 [摘要]车辆路径规划问题中路网模型.路径规划算法和交通信息的智能预测为关键点. 由于驾驶员的驾驶工作繁重,同时随着汽车拥有量的增加,非职业驾驶员的数 ...

  10. centos安装ruby

    下面开始安装Ruby 一.下载Ruby源码包 wget ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.7.tar.gz 二.解压安装Ruby tar zx ...