6、Spring Cloud -熔断器Hystrix
6.1、什么是Hystrix
6.2、Hystrix解决了什么问题
6.3、Hystrix的设计原则
设计原则如下:
6.4、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-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上使用熔断器
.png)
地址: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应用服务都至少会开启一个线程来提供服务,换句话说,有 ...
随机推荐
- 计算细胞数【BFS】
问题描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数. 输入格式 2行:第1行为两个整数 mm, nn, 代表矩阵 ...
- 基于.Net下整合IBatis
一. 准备工作 1. 点击此下载支持.Net4.0的 iBatis.Net,工程中引用release文件夹下的dll 最新版(目前已不再更新),有稍作修改使其支持.NET4.0 2. 点击此可查看 i ...
- EF框架CodeFirst the model backing the 'PModelEntities' context has changed since the database was created. Consider using Code First Migrations to update the database
1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the 'Produc ...
- JDBC入门(5)--- 时间类型、大数据
一.时间类型 数据库类型与Java中类型的对应关系: DATE->java.sql.Date:表示日期,只有年月日,没有时分秒,会丢失时间. TIME->java.sql.Time:表示时 ...
- js生成qq客服在线代码
说到QQ客服在线代码,随便那么百度谷歌一下就会出来,一般都是 <a target="blank" href="http://wpa.qq.com/msgrd?V=1 ...
- Designers, please follow the guidelines
Skype released big update for its iOS application last week. It brought in a major overhaul of not o ...
- Python爬虫教程-09-error 模块
Python爬虫教程-09-error模块 今天的主角是error,爬取的时候,很容易出现错,所以我们要在代码里做一些,常见错误的处,关于urllib.error URLError URLError ...
- Appium+java移动端项目测试问题整理
一.每次打开APP都要重新安装.充值账号密码 解决:打开appium,设备,Use Browser ,勾选“No Reset” 二.一个页面包含相同文字,打开页面路径错误 问题描述:APP处于[ ...
- mac安装软件提示没有权限
Mac 安装软件基本是各种爽,自动更新啥. 但是有一种提示没有权限的错误,很不爽,还要sudo管理员权限 有一个修复 /usr/local目录权限的命令 sudo chown -R 'whoami' ...
- Android数据统计
开发效率可以用这些方式提升: 1 . 构建公用工具类,方便大家使用 2 . 使用开源的一些包,例如ORM思想的数据库等 3 . 可以很快的找到问题.开发中,找bug的时间,往往是很多的.我用的方法有3 ...