第一:关于服务调用和熔断安全:

ribbon和Feign:

1. 相当于nigx+doubbe,微服务间的服务调用,API网关的请求转发等内容
2. Feign整合了Ribbon和Hystrix

 Hystrix:

1. 断路器:
2. 熔断模式.
3. 隔离模式
4. 限流模式
在微服务场景中,通常会有很多层的服务调用。如果一个底层服务出现问题,故障会被向上传播给用户。
当底层服务不可用时,可以阻断故障的传播。

为什么需要 Hystrix?

hystrix主要是用来防止服务雪崩效应的。

Hystrix 是一个帮助解决分布式系统交互时超时处理和容错的类库, 它同样拥有保护系统的能力。

分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为了应对服务雪崩, 一种常见的做法是手动服务降级. 而Hystrix的出现,给我们提供了另一种选择.

1.流量控制

2.改进缓存模式

3.服务自动扩容

4.服务调用者降级服务

引用自:  https://www.cnblogs.com/xyhero/p/53852cf0245c229fe3e22756a220508b.html

二、Ribbon+Hystrix(第一种)

pom:

  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

application.properties:

spring.application.name=hello-consumer-ribbon-hystrix
server.port=8041
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/

HelloService:

package cn.demo.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class HelloService { @Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "fallback", commandProperties = {
@HystrixProperty(name= "execution.isolation.thread.timeoutInMilliseconds", value="2000")
})
public String hello (String name) {
return restTemplate.getForEntity("http://USER-SERVICE/hello?name=" + name, String.class).getBody();
} public String fallback (String name) {
return "hello, hystrix熔断启动=== fail name:" + name;
} }

HelloConsumerRibbonHystrixApplication:

package cn.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import cn.demo.service.HelloService; @SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@RestController
public class HelloConsumerRibbonHystrixApplication { public static void main(String[] args) {
SpringApplication.run(HelloConsumerRibbonHystrixApplication.class, args);
} @Bean
@LoadBalanced
public RestTemplate restTemplate () {
return new RestTemplate();
} @Autowired
private HelloService helloService; @RequestMapping("hello")
public String hello (String name) {
return helloService.hello(name);
} }

测试:

启动eureka-server:8001, hello-service:8012,8002,hello-consumer-ribbon-hystrix:8041

访问:http://localhost:8041/hello?name=ribbon_hystrix

返回:hello, ribbon_hystrix

访问正常,接下来我们把hello-service服务停了,再次访问:

返回:hello, hystrix熔断启动=== fail name:ribbon_hystrix  

------成功触发熔断。

然后我们再次启动hello-service服务,然后访问:

hello, ribbon_hystrix

没有触发熔断,正常。

同样我们测试访问超时触发熔断的情况,我们在hello-service接口加上线程等待1s:

@RequestMapping("hello")
public String hello (String name) throws InterruptedException {
Thread.sleep(1000);
System.out.println("hello, " + name);
return "hello, " + name;
}

访问,发现同样触发熔断,因为hystrix默认超时1s触发熔断,我们可以通过修改属性来改变超时时间。

这里我们把超时时间修改为2s:

@HystrixCommand(fallbackMethod = "fallback", commandProperties = {
@HystrixProperty(name= "execution.isolation.thread.timeoutInMilliseconds", value="2000")
})
public String hello (String name) {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello?name=" + name, String.class).getBody();
}

再次访问,发现没有触发熔断。

三、Feign With Hystrix(第二种)

Feign默认是自带Hystrix的,所以依赖Jar的时候无需再依赖hystrix

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

application.properties:

spring.application.name=hello-consumer-feign-hystrix
server.port=8051
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
## 开启hystrix
feign.hystrix.enabled=true
## hystrix熔断触发默认超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000

HelloService:

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "hello-service", fallback = HelloServiceFallBack.class)
public interface HelloService { @RequestMapping("hello")
String hello(@RequestParam(value = "name") String name) ;
}

熔断触发类HelloServiceFallBack:

import org.springframework.stereotype.Component;

@Component
public class HelloServiceFallBack implements HelloService{

@Override
public String hello(String name) {
  return "hello, hystrix 触发熔断 == fail name : " + name;
}
}

启动类:

import cn.saytime.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@RestController
public class HelloConsumerFeignHystrixApplication { public static void main(String[] args) {
SpringApplication.run(HelloConsumerFeignHystrixApplication.class, args);
} @Autowired
private HelloService helloService; @RequestMapping("hello")
public String hello(String name){
return helloService.hello(name);
}
}

测试:

启动eureka-server:8001, hello-service:8011,hello-consumer-feign-hystrix:8051

访问:http://localhost:8051/hello?name=feign_hystrix

接下来关闭hello-service服务,再次访问:

将hello-service服务重新启动,访问正常,没有触发熔断。

将hello-service服务接口加上线程等待3s,重启hello-service服务,再次调用,同样成功触发熔断

修改application.properties里面熔断超时时间为4s,再次调用,没有触发熔断。

四、Hystrix Dashboard (Hystrix 仪表盘

ribbon-hystrix 与 feign-hystrix 两个项目的pom文件都添加以下依赖:

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

启动类都加上如下注解: @EnableHystrixDashboard

然后访问

http://localhost:8041/hystrix
http://localhost:8051/hystrix

输入链接:http://localhost:8041/hystrix.stream

同理,如果是feign-hystrix项目,输入 http://localhost:8051/hystrix.stream

点击Monitor Stream

然后我们访问一下 http://localhost:8041/hello?name=ribbon_hystrix

会出现如下监控页面:

这个微服务监控如何查看监控结果。

图中实心圆共有两种含义,他通过颜色的变化代表了实例的健康程度,它的健康度颜色从绿色<黄色<橙色<红色递减。

该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示可以在大量的实例中快速的发现故障实例和高压力实例。

SpringCloud(五):断路器(Hystrix)和hystrixdashboard图实现链路追踪的更多相关文章

  1. spring cloud学习(五)断路器 Hystrix

    断路器 Hystrix 断路器模式 (云计算设计模式) 断路器模式源于Martin Fowler的Circuit Breaker一文. 在分布式环境中,其中的应用程序执行访问远程资源和服务的操作,有可 ...

  2. springcloud之断路器(Hystrix)

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

  3. SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...

  4. SpringCloud 进阶之Hystrix(断路器)

    1. Hystrix 断路器 Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败, 比如超时,异常等,Hystrix能够保证在一个依赖出问题的情况 ...

  5. SpringCloud IDEA 教学 (四) 断路器(Hystrix)

    写在开始 在SpringCloud项目中,服务之间相互调用(RPC Remote Procedure Call —远程过程调用),处于调用链路底层的服务产生不可用情况时,请求会产生堆积使得服务器线程阻 ...

  6. SpringCloud断路器(Hystrix)和服务降级案列

    断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...

  7. 断路器Hystrix与Turbine集群监控-Spring Cloud学习第三天(非原创)

    文章大纲 一.Hystrix基础介绍二.断路器Hystrix简单使用三.自定义Hystrix请求命令四.Hystrix的服务降级与异常处理五.Hystrix的请求缓存与请求合并六.Hystrix仪表盘 ...

  8. springcloud 入门 11 (Hystrix Dashboard)

    hystrix: 断路器我在前面已经介绍,不了解的可以参考 :springcloud 入门 6 (断路器hystrix)  关于搭建,测试我都在这里面进行说明了,这章介绍的是  Hystrix Das ...

  9. SpringCloud学习之Hystrix

    一.为什么要有断路器 在分布式系统当中,服务之间调用关系会随着业务的发展而变的复杂,一个服务可能依赖多个服务,服务之间层层依赖也是家常便饭的事情,如果一个服务的瘫痪很有可能导致整个系统的崩溃.比如说, ...

随机推荐

  1. JS---案例:点击按钮设置div背景色渐变

    案例:点击按钮设置div背景色渐变 背景色渐变:设置透明度 <div id="dv"></div> <input type="button& ...

  2. reselect是怎样提升react组件渲染性能的?

    reselect是什么? reselect是配合redux使用的一款轻量型的状态选择库,目的在于当store中的state重新改变之后,使得局部未改变的状态不会因为整体的state变化而全部重新渲染, ...

  3. kvm磁盘管理

    kvm磁盘管理 kvm虚拟机虚拟磁盘格式转换 各种格式说明介绍 row:裸格式,占用空间较大,不支持快照功能,性能较好,不方便传输(顺序读写) 50G 2G 传输50G qcow2:cow 占用空间小 ...

  4. Axure5.1.0.1699 RP汉化版

    Axure是一款产品原型(Prototype)设计软件,可通过这个软件编辑一些常见的事件和在特点条件下才会触发的情况,可以快速的生成HTML Demo页面. 使用Axure的用户很多,据淘宝UED消息 ...

  5. Ubuntu1804中重新认识docker

    这又是一篇充数的笔记……之前在Ubuntu中折腾过好几次了,但是关于他俩之间的故事总是参杂着第三者,不太纯粹,这一次只说她两之间的故事. 上一篇笔记弄好了Ubuntu环境.之后就是准备迎娶docker ...

  6. mongodb-API

    mongodb-API 连接mongo(该操作一般在初始化时就执行) 出现 由于目标计算机积极拒绝,无法连接的错误时 查看是否进行虚拟机的端口转发 将 /etc/ 目录下的mongodb.conf 文 ...

  7. k8s采坑记 - 解决二进制安装环境下证书过期问题

    前言 上一篇k8s采坑记 - 证书过期之kubeadm重新生成证书阐述了如何使用kubeadm解决k8s证书过期问题. 本篇阐述使用二进制安装的kubernetes环境,如何升级过期证书? k8s配置 ...

  8. webpack生成的css文件background-image url图片无法加载

    之前在使用webpack3构建基于less预处理的项目时,在对指定的元素使用background-image: url(xxx)来设置背景图片时,本地开发是ok的,但是在项目编译产出后背景图片就找不到 ...

  9. Hive 性能测试工具 hive-testbench

    下载: yum -y install gcc gcc-c++ maven 下载地址Github:https://github.com/hortonworks/hive-testbench/git cl ...

  10. Java对象 POJO和JavaBean的区别

    转载自https://www.jianshu.com/p/224489dfdec8 这篇博客很通俗易懂的讲明白了什么是POJO,对于刚开始学开发做java项目的菜鸟来说,很有帮助,网课老师是不会讲这些 ...