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应用服务都至少会开启一个线程来提供服务,换句话说,有 ...
随机推荐
- 判断php变量是否定义,是否为空,是否为真的一览表
分类: 使用 PHP 函数对变量 $x 进行比较 表达式 gettype() empty() is_null() isset() boolean : if($x) $x = ""; ...
- js中数组, JSON的与字符串的处理
1. JSON转字符串 var jsonStr = {"name": "xiaoming", "sex": "男"} J ...
- SJ定理——省选前的学习2
——博弈论?上SG定理!什么?不行?那就SJ定理吧. 原来还有这么个玩意... bzoj1022. 大意是Nim取石子游戏中取到最后一个石子就算输,即无法取了就获胜(原版是无法取了就输). 我们试图套 ...
- cf1060D. Social Circles(贪心)
题意 题目链接 Sol 我是这样考虑的:从大到小考虑每个\(l, r\),最大的\(l\)应该和最大的\(r\)匹配(不然就亏了),其次次大的\(r\)应该和次大的\(l\)匹配 然后就过了.. /* ...
- svg动画之日出
效果分析 一个太阳,从底部升起来,天空由黑变蓝.那么要画的东西确定为三个:1.太阳(圆形)2.太阳光芒 3.天空 代码如下 <!--画太阳--> <svg width="6 ...
- css多行文本省略号(...)
如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现单行文本省略 <!DOCTYPE html ...
- LeetCode算法题5----Longest Palindromic Substring
#5. Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You ...
- unity对象物体闪烁
Material mat; // float a=0; //控制闪烁速度 float speed=0.6f; float b=1; Update(){ if(a>0.3f){ b=1; } if ...
- GIT:如何管理本机的多个ssh密钥(多个远程仓库账号)
最近在学习git,首先推荐一个个人认为很不错的git教程,学习后,从git新手变成了git入门了,仍需继续努力. 廖老师的git学习教程(其他git基础知识点,本篇博客就不做介绍了,自己看教程学习吧, ...
- springboot监控
springboot版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...