hystrix由来:服务器宕机或者依赖关系失败。

hystrix:

Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
雪崩:
服务器A调用服务器B,服务器B调用服务器C.......一台服务可能会调用多台服务器,一台服务器也可能会被多态服务器调用,如果一台服务器宕机,会对整个系统造成灾难性的后果。
 
在ribbon中使用hystrix:
1:修改pom文件(添加依赖):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
 
<!-- eureka客户端 -->
<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-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2:修改配置文件(application.yml):

server:
port: 6003
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: consumer6003
prefer-ip-address: true
spring:
application:
name: consumer3

3:启动类(App):

@SpringBootApplication
@EnableEurekaClient
@Configuration
@EnableHystrix
public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

4:控制器(HelloController):

@SuppressWarnings("unchecked")
@RestController
public class HelloController {

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/hi")
@HystrixCommand(fallbackMethod="helloFallback")
public Map<String,Object> hello(){
return restTemplate.getForObject("http://provider-one/hello", Map.class);
}

public Map<String,String> helloFallback(){
Map<String,String> map = new HashMap<String, String>();

map.put("info", "失败");
return map;
}

}

比较ribbo:

  在方法上添加注解@HystrixCommand(fallbackMethod="helloFallback")

  属性fallbackMethod的值是你要调用的方法

 
在feign中使用hystrix:
1:修改配置文件pom.xml(添加依赖):

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>

2:修改配置文件(application.yml):

server:
port: 6004
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: consumer6004
prefer-ip-address: true
spring:
application:
name: consumer4

feign:
hystrix:
enabled: true

3:启动类(App):

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@Configuration
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}

}

4:接口

@FeignClient(value = "provider-one",fallback=HelloServiceFallBach.class)

public interface HelloService {

@GetMapping("hello")
public Map<String,Object> h();

}

5:控制器

@RestController
public class HelloController {

@Autowired
private HelloService service;

@RequestMapping("jj")
public Map<String,Object> hello(){
return service.h();
}

}

6:错误返回控制器:

@Component
public class HelloServiceFallBach implements HelloService{

@Override
public Map<String, Object> h() {

Map<String, Object> map = new HashMap<String, Object>();
map.put("info", "错误");
return map;
}
}

@Component别忘了加

 
 

spingcloud--hystrix(断路器)的更多相关文章

  1. SpringCloud 进阶之Hystrix(断路器)

    1. Hystrix 断路器 Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败, 比如超时,异常等,Hystrix能够保证在一个依赖出问题的情况 ...

  2. SpringCloud的入门学习之概念理解、Hystrix断路器

    1.分布式系统面临的问题,复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 2.什么是服务雪崩? 答:多个微服务之间调用的时候,假设微服务A调用微服务B和微服务 ...

  3. SpringCloud(五)之Spring Cloud 中 Feign结合Hystrix断路器开发实战

    1.先讲hystrx(断路器) 在springcloub 中的使用 1.1  加入依赖 注意:网上新旧版本问题,所以要以官网为主,不然部分注解会丢失最新版本 2.0 <dependency> ...

  4. 小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下

    笔记 4.Feign结合Hystrix断路器开发实战<下>     简介:讲解SpringCloud整合断路器的使用,用户服务异常情况     1.feign结合Hystrix       ...

  5. 小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上

    笔记 3.Feign结合Hystrix断路器开发实战<上>     简介:讲解SpringCloud整合断路器的使用,用户服务异常情况 1.加入依赖          注意:网上新旧版本问 ...

  6. SpringCloud学习笔记(七):Hystrix断路器

    概述 什么时候需要断路器?熔断? 举个简单的例子:小明喜欢小美,可是小美没有电话,小美给了小明家里的座机,小明打给座机,这个时候小美的妈妈接到了,小明怕妈妈知道自己喜欢小美,就跟小美妈妈说让小美哥接电 ...

  7. Springcloud 整合Hystrix 断路器,支持Feign客户端调用

    1,在这篇博文中,已经大致说过了Springcloud服务保护框架 Hystrix在服务隔离,服务降级,以及服务熔断中的使用 https://www.cnblogs.com/pickKnow/p/11 ...

  8. SpringCloud(三)Hystrix断路器

    Hystrix断路器 概述 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败 服务雪崩 多个微服务之间调用的时候,假设微服务A调用微服务B和 ...

  9. Hystrix断路器中的服务熔断与服务降级

    一.Hystrix断路器 微服务架构特点就是多服务,多数据源,支撑系统应用.这样导致微服务之间存在依赖关系.如果其中一个服务故障,可能导致系统宕机,这就是所谓的雪崩效应. 1.为什么需要断路器 服务雪 ...

  10. 微服务架构 | 5.1 使用 Netflix Hystrix 断路器

    目录 前言 1. Hystrix 基础知识 1.1 Hystrix 断路器强调调用 1.2 两大类别的 Hystrix 实现 1.3 舱壁策略 1.4 Hystrix 在远程资源调用失败时的决策过程 ...

随机推荐

  1. 图片切换.----so屌

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 设计模式课程 设计模式精讲 15-2 桥接模式Coding

    1 代码演练 1.1 代码演练1 1.2 代码演练2   1 代码演练 1.1 代码演练1 需求: 打印出从银行获取的账号类 优点: a 假如我只用用一个银行接口 去获取账号的内容,银行实现类要有定期 ...

  3. tf-idf、朴素贝叶斯的短文本分类简述

    朴素贝叶斯分类器(Naïve Bayes classifier)是一种相当简单常见但是又相当有效的分类算法,在监督学习领域有着很重要的应用.朴素贝叶斯是建立在“全概率公式”的基础下的,由已知的尽可能多 ...

  4. android7.0关于TelephonyManager.getDeviceId()返回null的问题

    在android7.0的系统下发现TelephonyManager.getDeviceId()在权限允许的情况下取得返回值也为null,解决方法如下: public static String get ...

  5. Day9 - A - Apple Catching POJ - 2385

    Description 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速移动到另外一棵APP树下接APP(移动时间可 ...

  6. 分页--pagination.js

    var pagination = function (thispage, totalpage, ulele, firstlast) { ulele.html(''); var prevCss, nex ...

  7. 使用 CAS 在 Tomcat 中实现单点登录 http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

    developerWorks 中国 技术主题 Open source 文档库 使用 CAS 在 Tomcat 中实现单点登录 单点登录(Single Sign On , 简称 SSO )是目前比较流行 ...

  8. C++服务器与java进行socket通信案例

    分类: [java]2012-10-08 12:03 14539人阅读 评论(46) 收藏 举报 注:本代码版权所有!!!转载时请声明源地址:http://blog.csdn.net/nuptboyz ...

  9. 非阻塞IO与异步IO

    一.非阻塞IO的轮询读写 ---如果当前进程有多个输入终端和多个输出终端呢?while((n=read(STDIN_FILENO,buf,buf_size))>0){    if(write(S ...

  10. docker-compose 快速部署Prometheus,监控docker 容器, 宿主机,ceph -- cluster集群

    话不多说上菜: 现在环境是这样: ceph 4台: 192.168.100.21  ceph-node1 192.168.100.22  ceph-node2 192.168.100.23  ceph ...