6、Spring Cloud -熔断器Hystrix
6.1、什么是Hystrix
6.2、Hystrix解决了什么问题



6.3、Hystrix的设计原则
设计原则如下:

6.4、Hystrix的工作机制
如图:

搭建工程
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFMAAAAWCAYAAAC8J6DfAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIPSURBVFhH7Za9bsIwEID7XFH52yrUDVGkLoDUARq2MuNsVacOydSdSH2F8BJ9o2tscuFsLsEFkxLJw6c2F+cOf7kz3N33BuBxg5fpEC/TIV6mQ7xMh3iZDqmU+R5/aYiPT3ievbBr6wjGEWyzDLI0glG3z665FkF3BbGsnSWwbKC2tUxktlix66u4lsyRSHNJKWzG1TldyAy6jzBdC4iiqESIkN3Ln2WewsxzLZnLREqql+kClPlK6gTjUEmlMUmzMsMEdrudYism+QedwCbVpZixgDwjicO+EkljWbI61Mn/34tOYNEJtc7E5+QaVavIXfeiOZkqPpzDm9GhDcpM1d9SQLFBOa7yWsrV1hNB+IzkpMyiDifzMPbyRdkdAZUySXwvdt2gTOw0bUP5dSlh3x0oV0rDzkHRFHPMzToqxpyZXKdjTo7blElGyRSBY7ksR1zvJNx4XY6yTjHCKsbJLGqY+aqol7mG6fAQP1sm9wyHjUzsllgcC6H3lYDi3tkyjc7kup5SKTP/EhLrOTyQl3GRzN73DwvNYyWz2PQ2P+84yRQqk8ZsZOrXx6I5OJlKZD7W2JUXj7lcw4mU0Dw2MjGmxJB1pkztXpHXVuai81SON3Yjns80rwnK1H5nGh3ZmExbzG/1NlIp0wZOpIRbWwf3e7ON/LtM8+zj1rSFm5FZd261hYtkenS8TGcM4BcKag06NYf+VAAAAABJRU5ErkJggg==" alt="" />
pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
RibbonConfig.java
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
hystrixService.java
@Service
public class hystrixService {
@Autowired
RestTemplate restTemplate; public String port(){
return restTemplate.getForObject("http://CLINET/port",String.class);
}
}
hystrixController.java
@RestController
public class hystrixController { @Autowired
hystrixService hystrixService; @GetMapping("/hi")
public String hi(){
return hystrixService.port();
} }
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
}
}
6.5、在RestTemplate和Ribbon 上使用熔断器
1、首先引入相关的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、主配置类加上@EnzbleHystrix注解开启Hystrix的熔断功能
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
public class HystrixApplication { public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
}
}
hystrixService.java
@Service
public class hystrixService {
@Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "isError")
public String port(){
return restTemplate.getForObject("http://CLINET/port",String.class);
} public String isError(){
return "request is error!";
} }

观察浏览器会显示:

6.6、在Feign上使用熔断器
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAI4AAAAXCAYAAADdlHeDAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAALLSURBVGhD7ZuxbtswEIbzYAUKr+47JEuXAnHRrJkKFAaM7q6XTp68OHFqwwbkR+nDsD5JR52OdySlKnbg3PANFo93P48/RWXIzYePI2cYXbn59PXZGUZXzDhGL8w4Ri/MOEYvzDhGL7xxfv7etPjx69l9/h5OeEuMH3duVRSuWG7F8T48LAt3PB5dUezdw+RJjAHGk61bQO1E3Gvg113XvoQW1TjIl+kmmPRWGNo4d/NDaZr3bhzoQ1Ec3PRRz5U0Tgqa7NwMbRx82yxm5zVCV7hxpJj/AfpwEePgCcDTu5q/VM/ZRo8nL266bhpAxyvx7ROVyuevmfXO3d5vytw4J9UInOvjTznuYrWFU456yvl+DVVdHw95Z/sgnwavD3m5cUQtKd2KjqAPdW85gxuHC0bgFGcbZ33wjfl2/ycr37hugrbh5RhpLEcyzm2sNtssbd2hcaq1NeO6JnENGcbRtLR0KzouZxzcQDRHvUhwdLZxyNshL9++bpz+VqGnXxoHsGl4VUVr881SjBsYx//GteuaeH3/3PdJMU6Wbl1HTq8GNw79wKTAIpqNThiHNConH32G8zB3a45vVtU8+gzmcONEa7PNwlh85WM+XhONpY2X+etnmBP1ICnjZOlWdEi/JbKNQyfFkBqI+AUHpzJtnGi+5c6bhG5612ZADM+h1lY2C7U341XNnA3j8JxIrnGius9pnNHTXxHMAaAwEI7wBdKx1rhgnGg+Es+bic1rz+tmnJy1cC1N3OkbglwBPr6DccT6wloDLTm6E8ah9agmZHDjAGETK9HlWH3/AotZ+qqK5ePx/oSexulHNTRpmnGKuHEAtTbbrDKWrG0137a+HXI2TCKon2EccR6Pjeigc89qHKMxEd3Ma8IbJwfJNIAU+97Ak4wnHJG+M64BM85ASMbhfw1dE2YcoxedjGMYiBnH6IX9l4PRg5H7B71blzEUVFIoAAAAAElFTkSuQmCC" alt="" />地址:https://www.cnblogs.com/Mrchengs/p/10646137.html
新建一个类:

portHystrix.java
需要实现接口中的方法
@Component
public class portHystrix implements EurekaClientFeign {
@Override
public String port() {
return "request is wrong!!";
}
}
EurekaClientFeign.java
@FeignClient(value = "CLINET",configuration = feignconfig.class,
fallback = portHystrix.class)
public interface EurekaClientFeign {
@GetMapping("/port")
String port();
}
配置文件中:
spring.application.name=feign
server.port=
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
#开启Hystrix的功能
feign.hystrix.enabled=true
正常开启:

此时可以进行访问!!!

6.7、使用Hystrix Dashboard监控熔断器的状态
6.7.1、在RestTemplate中使用Hystrix Dashboard

添加所需要的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
public class HystrixApplication { public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
} @Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
坑位......
此时把服务提供者和注册中心均开启:


当访问:




此时发现:

在该页面显示了熔断器的各种数据指标
6.7.2、Feign 中使用 Hystrix Dashboard

1、pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
主配置类:
@EnableHystrix
@EnableHystrixDashboard
@EnableFeignClients(basePackages = "com.cr.eurekafeignclient.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignClientApplication { public static void main(String[] args) {
SpringApplication.run(EurekaFeignClientApplication.class, args);
} @Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
其余的测试都和之前相似
6、Spring Cloud -熔断器Hystrix的更多相关文章
- Spring Cloud 熔断器
目录 Spring Cloud 熔断器 Hystrix ribbon中使用hystrix feign中使用hystrix Spring Cloud 熔断器 在微服务架构中,根据业务来拆分成一个个的服务 ...
- Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?
导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...
- Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失问题分析
最近spring boot项目中由于使用了spring cloud 的hystrix 导致了threadLocal中数据丢失,其实具体也没有使用hystrix,但是显示的把他打开了,导致了此问题. 导 ...
- Spring Cloud断路器Hystrix
在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...
- Spring Cloud之Hystrix服务保护框架
服务保护利器 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的 ...
- Spring Cloud 之 Hystrix.
一.概述 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依 ...
- Spring Cloud 学习--Hystrix应用
上一篇介绍了Hystrix基本功能和单独使用的方式,今天继续学习如何将Hystrix融入SpringCloud组件中去. 在Ribbon上使用熔断器 在 pom.xml 文件中引入 hystrix 的 ...
- 笔记:Spring Cloud Feign Hystrix 配置
在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...
- 架构师系列文:通过Spring Cloud组件Hystrix合并请求
在前文里,我们讲述了通过Hystrix进行容错处理的方式,这里我们将讲述通过Hystrix合并请求的方式 哪怕一个URL请求调用的功能再简单,Web应用服务都至少会开启一个线程来提供服务,换句话说,有 ...
随机推荐
- SLF4+Logback 使用及配置
在SpringBoot项目中使用slf4+logback ①在pom.xml中添加依赖 <dependency> <groupId>ch.qos.logback</gro ...
- 认识Groovy
什么是groovy: Groovy 是 JVM 的一个替代语言 —替代 是指可以用 Groovy 在 Java 平台上进行 Java 编程,使用方式基本与使用 Java 代码的方式相同. 在编写新应用 ...
- 使用 maven 创建 java web 工程
本文主要讲述使用 maven 命令行的形式来创建 java web 工程 开发环境 jdk 1.7 maven 3.5.0 spring 3.2 tomcat 7 eclipse Mars Rele ...
- java 自定义 LRU(最近最少使用)策略 实现 缓存机制
1. java提供了一个简单的方式实现LRU: LinkedHashMap 2. 自定义实现 LRU至少需要两个主要操作: 添加(add)和搜索(search) public class LRU ...
- VBScript开发Excel常见问题
VBS基础 基本概念:VB & VBS & VBA VB.VBScript和VBA(Visual Basic For Application)这三种语言,既有联系又有区别.三种语言的语 ...
- [学习] nofollow
[来源:百度百科 http://baike.baidu.com/view/1584081.htm] 简介 nofollow[1]是一个HTML标签的属性值.它的出现为网站管理员提供了一种方式,即告诉搜 ...
- 需求分析之WBS
我们的产品就是当你把废旧的塑料瓶和电池投入回收箱中会产生能量,那么这能量可以干嘛呢那就通过我们的APP进行扫码记录所产生的能量这写能量可以在我们的APP的换吧中进行兑换用户所需要的东西比如:共享单车的 ...
- 学习笔记:Nginx反射代理使用缓存和删除其缓存文件的方法
使用nginx做cache服务器 需求就是缓存android的软件包,后缀名是apk.话不多说,直接上配置,供参考: a-->nginx.conf user www www; worker_pr ...
- course & time
- 五大常用算法之四:回溯法[zz]
http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html 1.概念 回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试 ...