springCloud的使用04-----熔断器hystrix的使用
1. restTemplate+ribbon使用hystrix
1.1 引入依赖
<!-- 配置hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
1.2 在需要熔断的方法上添加注解
@Service
public class HiService { @Autowired
RestTemplate restTemplate; //需要熔断的方法
@HystrixCommand(fallbackMethod="hiError")//熔断后执行的方法
public String sayHi() {
return restTemplate.getForObject("http://SERVICE-HI/info", String.class);
} //熔断后执行的方法
public String hiError() {
return "sorry hi error";
}
}
1.3 在启动类中声明使用hystrix
@SpringBootApplication
@EnableDiscoveryClient//向服务中心注册
@RestController
@EnableHystrix//启用熔断机制
public class ConsumerRibbon { @Autowired
private HiService hiService; public static void main(String[] args) {
SpringApplication.run(ConsumerRibbon.class, args);
} @Bean
@LoadBalanced//使用这个restTemplate开启负载均衡
RestTemplate initRestTemplate(){
return new RestTemplate();
} @RequestMapping("info")
public String hiConsumer() {
String response=hiService.sayHi();
return response;
}
}
1.4 启动注册中心和cloud-consumer-ribbon,访问http://localhost:8764/info 返回sorry hi error
启动service-hi,访问http://localhost:8764/info 返回hello eureka client 8762
2 feign使用hystrix
2.1 feign自带熔断器,无需导入hystrix的依赖,但是需要导入以下依赖,否则回报java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect错误
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</dependency>
2.2 在配置文件中启用hystrix,默认是关闭的
feign:
hystrix:
enabled: true
2.3 指定熔断后要执行的类
@FeignClient(value="service-hi",fallback=HiServiceHystric.class)//指定调用哪个服务提供者,指定熔断后的执行的类
public interface IHiService { @RequestMapping(value="/info",method=RequestMethod.GET)//指定调用服务提供者的哪个接口
String info(); @RequestMapping(value="/info",method=RequestMethod.GET)//指定调用服务提供者的哪个接口
String hi();
}
2.4 指定熔断后要执行对应的方法
@Component
public class HiServiceHystric implements IHiService { //熔断后执行相应的方法
public String info() {
return "sorry info feign";
} public String hi() {
return "sorry hi feign";
}
}
2.5 在启动类中声明启动hystrix
@EnableHystrix
2.6 启动注册中心和cloud-consumer-feign,访问http://localhost:8765/info 返回sorry info feign
启动service-hi,访问http://localhost:8765/info 返回hello eureka client 8762
3 使用熔断器监控(hystrix dashboard)
3.1 引入相应的jar依赖
<!-- 配置hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3.2 在启动类中声明启动hystrix dashboard
@SpringBootApplication
@EnableEurekaClient//向服务中心注册
@EnableHystrix//启用熔断机制
@EnableHystrixDashboard //启用熔断器监控页面
public class ConsumerRibbonApp { public static void main(String[] args) {
SpringApplication.run(ConsumerRibbonApp.class, args);
} @Bean
@LoadBalanced//使用这个restTemplate开启负载均衡
RestTemplate initRestTemplate(){
return new RestTemplate();
}
}
3.3 启动项目


4 熔断器聚合监控
如果多个项目都配置了hystrix和hystrix dashboard,想要在一个项目中的hystrix dashboard上看到其他项目的hystrix dashboard情况,就需要使用turbine进行聚合监控
4.1 创建springboot项目,引入jar依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.beifeng.hadoop</groupId>
<artifactId>beifeng-spring-cloud-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>beifeng-spring-cloud-turbine</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- 声明为web项目 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- 配置eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- 引入hystrix turbine 熔断器聚合监控 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
</dependencies> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.2 在配置文件中配置那些项目要进行聚合监控
spring:
application:
name: service-turbine
server:
port: 8770
security:
basic:
enabled: false
turbine:
aggregator:
clusterConfig: default #指定聚合那些集群,多个使用 , 分隔,默认为default
appConfig: cloud-consumer-ribbon,cloud-consumer-feign #配置要监控的服务,多个使用 , 分隔
clusterNameExpression: new String("default")
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/
4.3. 在启动类中声明启动turbine
@SpringBootApplication
@EnableTurbine //开启turbine,该注解包含了@EnableDiscoveryClient
public class TurbineApp {
public static void main(String[] args) {
SpringApplication.run(TurbineApp.class, args);
}
}
4.4 启动项目,在任何一个配置了hystrix dashboard的项目中查看聚合监控


springCloud的使用04-----熔断器hystrix的使用的更多相关文章
- SpringCloud(4)熔断器 Hystrix
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...
- SpringCloud学习笔记:熔断器Hystrix(5)
1. Hystrix简介 在分布式系统中,服务与服务之间相互依赖,一种不可避免的情况是某些服务会出现故障,导致依赖于它们的其他服务出现远程调度的线程阻塞. Hystrix提供熔断器功能,能够阻止分布式 ...
- SpringCloud无废话入门04:Hystrix熔断器及监控
1.断路器(Circuit Breaker)模式 在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的.一旦发生这种情 ...
- 跟我学SpringCloud | 第四篇:熔断器Hystrix
跟我学SpringCloud | 第四篇:熔断器Hystrix 1. 熔断器 服务雪崩 在正常的微服务架构体系下,一个业务很少有只需要调用一个服务就可以返回数据的情况,这种比较常见的是出现在demo中 ...
- 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)
在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...
- SpringCloud 在Feign上使用Hystrix(断路由)
SpringCloud 在Feign上使用Hystrix(断路由) 第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中 ...
- spring cloud ----> RibbonClient设置的熔断器Hystrix不起作用
Ribbon spring.io 官网的简介: Ribbon is a client side load balancer which gives you a lot of control over ...
- 微服务—熔断器Hystrix
前言在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与发现的方式互相依赖. 由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...
- SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据
简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...
- springcloud费话之断路器(hystrix in feign)
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
随机推荐
- git 命令图解
git 命令图解 初始化版本库 git config user.name "lsgx" git config user.email "lsgxthink@163.co ...
- shell脚本从入门到精通(中级)之提高篇
shell 脚本入门到精通(中级) 一.shell 脚本的执行 二.输出格式化 三.数据类型 四.重定向 五.变量 一.shell 脚本的执行 1. 脚本执行的4种方法 $ ls /tmp/test. ...
- 20180209-shutil模块
下面讲解shutil模块的相关操作: 1.shutil.copyfileobj(fsrc, fdst, length=16*1024) 将fsrc文件内容拷贝到fdst文件中,length是指一次拷贝 ...
- Spring Boot 项目 Maven 配置
在配置基于Maven的Spring Boot项目的过程中,打包运行出现了一系列错误. 比如: mvn 中没有主清单属性.java.lang.NoClassDefFoundError: org/spri ...
- Vue-cli3 环境的搭建
Vue-cli3 环境的搭建 准备 浏览器插件:Vue.js devtools VsCode 和 VsCode 插件 WebStorm Nodejs vue-cli git 起飞 安装vue-cli3 ...
- Django 配置 qq 邮箱发送邮件
目录 一.实验环境 二.获取QQ邮箱授权码 1.什么是授权码? 2.怎么获取授权码? 三.Django中配置 setting.py中添加如下代码 文本邮件 HTML 邮件 一.实验环境 Python3 ...
- 洛谷4206/NOI2005T4 聪聪和可可 期望DP+记忆化搜索
题意:给出n个点m条边的无向图,两个主角聪聪和可可开始分别在S点和T点.聪聪想吃掉可可,每次由匆匆先行动后来可可行动.聪聪的行动是选他到可可的最短路上的点走最多两步(如果最短路有几条就选编号最小的走) ...
- 前端学习(三十六)promise(笔记)
一个页面: 头部.用户信息.新闻列表 jquery ajax: 1.$.ajax({ url:'', dataType:'json', }).then(res=>{ //r ...
- Python3.5-20190519-廖老师-自我笔记-面向对象中slots变量--@property的使用
python是动态语言,可以随时随地给实例对象添加属性和方法,但是我们想限制属性的名字,可以使用__slots__特殊变量来限制 使用__slots__要注意,__slots__定义的属性仅对当前类实 ...
- [BZOJ2341][Shoi2011]双倍回文 manacher+std::set
题目链接 发现双倍回文串一定是中心是#的回文串. 所以考虑枚举#点.发现以\(i\)为中心的双倍回文的左半部分是个回文串,其中心一定位于\(i-\frac{pal[i]-1}2\)到\(i-1\)之间 ...