spring cloud(断路器——初学四)
在分布式架构中,当某个服务单元发生故障后,能通过断路器的故障监控,向调用方返回一个错误响应,而不是长时间的等待。
Netflix Hystrix
在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。
一 准备工作
依次启动eureka-server、compute-service、eureka-ribbon工程;
访问http://localhost:1111/可以看到注册中心的状态;
访问http://localhost:3333/add,调用eureka-ribbon的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30;
关闭compute-service的服务,再次访问http://localhost:3333/add,会得到以下错误
二、ribbon引入Hystrix
1、ribbon工程的pom.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2、在启动类中添加注解开启断路器功能
package com.daqsoft; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
//用来发现注册服务
@EnableDiscoveryClient
//开启断路器功能
@EnableCircuitBreaker
public class CustomerDemoApplication { @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(CustomerDemoApplication.class, args);
}
}
3、改造原来的服务消费方式,新增ComputeService
类,在使用ribbon消费服务的函数上增加@HystrixCommand
注解来指定回调方法
package com.daqsoft; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @Description Created by liaoxx on 2017-6-13.
*/
@Service
public class ComputeService { @Autowired
RestTemplate restTemplate; /**
* 调用服务发生故障时回调addServiceFallback方法
* @return
*/
@HystrixCommand(fallbackMethod = "addServiceFallback")
public String addService(){
return restTemplate.getForEntity("http",String.class).getBody();
} public String addServiceFallback(){
return "error";
} }
4、提供rest接口的Controller改为调用ComputeService的addService
package com.daqsoft; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@RestController
public class CustomController { @Autowired
private ComputeService computeService; @RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(){
return computeService.addService(); }
}
5、启动服务,访问 http://localhost:3333/add 进行验证(成功拦截并进行回调)
spring cloud(断路器——初学四)的更多相关文章
- spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback
spring cloud: Hystrix(四):feign使用hystrix @FeignClient支持回退的概念:fallback方法,这里有点类似于:@HystrixCommand(fallb ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud第十四篇 | Api网关Zuul
本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...
- Spring Cloud断路器Hystrix
在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...
- Spring Cloud 微服务四:熔断器Spring cloud hystrix
前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...
- Feign详细使用-Spring Cloud学习第四天(非原创)
文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章 一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...
- Spring Cloud gateway 网关四 动态路由
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- spring cloud深入学习(四)-----eureka源码解析、ribbon解析、声明式调用feign
基本概念 1.Registe 一一服务注册当eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如IP地址.端口.运行状况指标的Uri.主页地址 ...
- Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...
随机推荐
- 手机连得上WIFI,电脑连不上的情况
可以搜到,密码也对,但就是连不上,这时候可能就是你的设置错了. 操作步骤以下: 右击我的电脑-->管理-->设备管理器-->网络适配器-->找到你wifi对应的那个名称(如果不 ...
- jqgrid获取选中行指定列的值
部分js如下: var id = $("#grid-table").jqGrid('getGridParam','selrow');//根据点击行获得点击行的id(id为jsonR ...
- Linux驱动之中断处理体系结构简析
S3C2440中的中断处理最终是通过IRQ实现的,在Linux驱动之异常处理体系结构简析已经介绍了IRQ异常的处理过程,最终分析到了一个C函数asm_do_IRQ,接下来继续分析asm_do_IRQ, ...
- python历史与基本类型
前言 我自学的方式主要是看文档,看视频,第一次做写博客这么神圣的事情,内心是忐忑的,写的东西比较杂,路过的小伙伴不要嘲笑我,主要是记录一日所学,顺便锻炼一下语言组织能力吧,anyway,这些都不重要, ...
- sql2012包含数据库,快速生成用户tsql脚本
今天太忙(下班时,发现一个考试网站的不算BUG的BUG,这个BUG刚好能让我找到想要的数据,现在正辛苦的编码中...) 不多说,今天的技术文章,简单一点,帖一段昨天写的SQL代码 用于SQL2012中 ...
- DVR NVR
1.NVR: 是(Network Video Recorder即网络硬盘录像机)的缩写.NVR最主要的功能是通过网络接收IPC(网络摄像机)设备传输的数字视频码流,并进行存储.管理,从而实现网络化带来 ...
- Win 10 安装手机驱动
直接上图,看图操作即可.
- 编译搭建lnmp+zabbix
搭建nginx 1)基础依赖包安装 yum -y install gcc gcc-c++ vim tree make cmake autoconf yum -y install openssl ope ...
- drf4 视图与路由组件
APIView和View的区别 不管是View还是APIView最开始调用的都是as_view() APIView继承了View, 并且执行了View中的as_view()方法,最后把view返回了, ...
- UVa 1426 Discrete Square Roots (扩展欧几里德)
题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...