Spring Cloud  Hystrix 

容错保护就是当请求的服务报错或者超时时,可以优雅降级.介绍两种实现:

  • RestTemplate 容错
  • FeignClient 容错

1.RestTemplate 容错

pom.xml

<!-- hystrix 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

application.yml

spring:
application:
name: hystrix-client server:
port: 8091

application.java

@EnableCircuitBreaker
@SpringBootApplication
public class HystrixClientApplication { @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(HystrixClientApplication.class, args);
} }

@EnableCircuitBreaker 开启断路器功能

HelloService.java

@Service
public class HelloService { @Autowired
private RestTemplate template; @HystrixCommand(fallbackMethod = "errorCallback")
public String hello(){
return template.getForObject("http://HELLO-SERVICE/hello",String.class);
} public String errorCallback(){
return "error";
} }
  • @HystrixCommand(fallbackMethod = "errorCallback") fallbackMethod 指定报错回调
  • errorCallback 错误回调方法

Controller.java

@RestController
@RequestMapping("hystrix")
public class HystrixHelloController { @Autowired
private HelloService helloService; @GetMapping("hi")
public String hi(){
return helloService.hello();
}
}

访问: http://localhost:8091/hystrix/hi
Hello World!
关闭服务在访问
访问: http://localhost:8091/hystrix/hi
error

2.FeignClient 容错

pom.xml

<!-- feign 声明式服务调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

application.yml

spring:
application:
name: hystrix-client server:
port: 8091 feign:
hystrix:
enabled: true

Application.java

@EnableFeignClients
@EnableCircuitBreaker
@SpringBootApplication
public class HystrixClientApplication { @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(HystrixClientApplication.class, args);
} }

必须同时开启断路器@EnableCircuitBreaker和feign客户端@EnableFeignClients

ServiceClient.java

@FeignClient(value = "hello-service", fallback = HystrixClientFallback.class)
public interface HelloServiceClient { @RequestMapping(method = RequestMethod.GET, value = "/hello")
String hello(); }

fallback 指定回调的类

Fallback.java

@Component
public class HystrixClientFallback implements HelloServiceClient { @Override
public String hello() {
return "error-feign";
} }

访问: http://localhost:8091/hystrix/he
Hello World!
关闭服务再访问:
访问: http://localhost:8091/hystrix/he
error-feign

Spring Cloud 2-Hystrix 断路容错保护(四)的更多相关文章

  1. Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控

    目录 1.环境介绍 2.服务监控 2.1 加入依赖 2.2 修改配置文件 2.3 修改启动文件 2.4 监控服务 2.5 小结 3. 利用hystrix实现消费服务熔断 3.1 加入服务熔断 3.2 ...

  2. Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?

    导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...

  3. Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失问题分析

    最近spring boot项目中由于使用了spring cloud 的hystrix 导致了threadLocal中数据丢失,其实具体也没有使用hystrix,但是显示的把他打开了,导致了此问题. 导 ...

  4. Spring Cloud Hystrix 服务容错保护

    目录 一.Hystrix 是什么 二.Hystrix断路器搭建 三.断路器优化 一.Hystrix 是什么 ​ 在微服务架构中,我们将系统拆分成了若干弱小的单元,单元与单元之间通过HTTP或者TCP等 ...

  5. Spring Cloud Hystrix 服务容错保护 5.1

    Spring Cloud Hystrix介绍 在微服务架构中,通常会存在多个服务层调用的情况,如果基础服务出现故障可能会发生级联传递,导致整个服务链上的服务不可用为了解决服务级联失败这种问题,在分布式 ...

  6. 笔记:Spring Cloud Hystrix 服务容错保护

    由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加 ...

  7. Spring Boot + Spring Cloud 构建微服务系统(四):容错机制和熔断(Hystrix)

    雪崩效应 在微服务架构中,由于服务众多,通常会涉及多个服务层级的调用,而一旦基础服务发生故障,很可能会导致级联故障,进而造成整个系统不可用,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者 ...

  8. Spring Cloud之Hystrix服务保护框架

    服务保护利器 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的 ...

  9. 架构师系列文:通过Spring Cloud组件Hystrix合并请求

    在前文里,我们讲述了通过Hystrix进行容错处理的方式,这里我们将讲述通过Hystrix合并请求的方式 哪怕一个URL请求调用的功能再简单,Web应用服务都至少会开启一个线程来提供服务,换句话说,有 ...

随机推荐

  1. MySQL数据库引擎类别和更换方式

    MySQL数据库引擎类别 能用的数据库引擎取决于mysql在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEA ...

  2. koa-router 源码由浅入深的分析(7.4.0版本的)

    首先简单的介绍下什么koa-router,为什么要使用它,可以简单看下上一篇文章. 了解koa-router 首先我们来看下koa-router的源码的基本结构如下,它是由两部分组成的: ------ ...

  3. koa2源码解读及实现一个简单的koa2框架

    阅读目录 一:封装node http server. 创建koa类构造函数. 二:构造request.response.及 context 对象. 三:中间件机制的实现. 四:错误捕获和错误处理. k ...

  4. (PAT)L2-004 这是二叉搜索树吗?(数据结构)

    题目链接:https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的 ...

  5. mybatis中使用到的设计模式

    Mybatis中使用到了哪些设计模式呢?下面就简单的来介绍下: 1.构造者模式: 构造者模式是在mybatis初始化mapper映射文件的过程中,为<cache>节点创建Cache对象的方 ...

  6. javascript高级排序算法之快速排序(快排)

    javascript高级排序算法之快速排序(快排)我们之前讨论了javascript基本排序算法 冒泡排序 选择排序 插入排序 简单复习: 冒泡排序: 比较相邻的两个元素,如果前一个比后一个大,则交换 ...

  7. JPA的merge对联合唯一索引无效(代码库)

    问题 JPA的merge()操作 是合并的意思,就是当保存的实体时,根据主键id划分,如果已存在,那么就是更新操作,如果不存在,就是新增操作 但是这个仅针对 主键id 划分,对联合唯一索引 无效,两次 ...

  8. rest framework 视图,路由

    视图 在上面序列化的组件种已经用到了视图组件,即在视图函数部分进行逻辑操作. 但是很明显的弊端是,对每个表的增删改查加上 单条数据,需要用到 2个类 5个方法(增删改查,单数据查)才可以完整的实现,当 ...

  9. 打印并输出 log/日志到文件(C++)

    #include <stdarg.h> #define MAX_LEN 1024 bool debug_mode; // 使用方法同 printf void lprintf(const c ...

  10. 题解:[APIO2007]风铃

    你需要选一个满足下面两个条件的风铃:(1) 所有的玩具都在同一层(也就是说,每个玩具到天花板之间的杆的个数是一样的)或至多相差一层.(2) 对于两个相差一层的玩具,左边的玩具比右边的玩具要更靠下一点. ...