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 ...
随机推荐
- NGUI技能CD效果制作(sprite的type:filled)
一,我们先添加一个sprite,改名为skill.给当前skill添加图片,然后再sprite下添加一个sprite和一个label,结果如下 二现在我们来设置skill下的sprite,给他设置一个 ...
- 英伟达GPU 嵌入式开发平台
英伟达GPU 嵌入式开发平台 1. JETSON TX1 开发者组件 JETSON TX1 开发者组件是视觉计算的全功能 开发平台,旨在让您能够快速地安装和运行. 该组件带有 Lin ...
- OkHttp源码剥离导入到eclipse中
1.里面有两个类关于Android版本的我稍微修改过了,没有用的. 2.可以直接导入eclipse中,maven里面的jar包可能还有不需要的冗余,可以自己去剔除. https://github.co ...
- demo board boot mode
demo扩展板 QSPI0_IO0_MIO2--A13--PS-MIO2 QSPI0_IO0_MIO3--A14--PS-MIO3 QSPI0_IO0_MIO4--B11--PS-MIO4 QSPI0 ...
- 线程安全与非线程安全集合说一下,底层怎么实现的(hashmap,concurrenthashmap)
Hashmap本质是数组加链表.根据key取得hash值,然后计算出数组下标,如果多个key对应到同一个下标,就用链表串起来,新插入的在前面. ConcurrentHashMap:在hashMap的基 ...
- java正则表达式移除网页中注释代码
/** * 移除网页中注释掉的代码 * * @param str * @return */ public static String removedisablecode(String str) { P ...
- windows命令行运行mysql
在cmd中输入时一定要保证英文环境. 1. windows命令行运行mysql: 我是将MYSQL安装在C:\Program Files\MySQL所以 C:\Program Files\MySQL\ ...
- TCP协议的简单应用一
TCP协议的应用,实现一个服务器处理多个客户端请求(向服务器发送消息,服务器接收到后以原消息返回给客户端.) 客户端类 import java.io.*; import java.net.Socket ...
- 2.Javascript 函数(主要)
定义函数 在JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述 ...
- php htmlentities()函数 语法
php htmlentities()函数 语法 作用:把字符转换为 HTML 实体 语法:htmlentities(string,flags,character-set,double_encode) ...