在正式实例之前,可以看一下之前对熔断的介绍,学起来更轻松。

https://blog.csdn.net/wangyunzhao007/article/details/105768936

实例

熔断是在注册中心的客户端加的,所以我们此次是在sonsumer的基础上改造的,下面是两个博客的改造之前的,大家可以看看。

eureka :https://blog.csdn.net/wangyunzhao007/article/details/105776190

ribbon  :https://blog.csdn.net/wangyunzhao007/article/details/105786976

增加的pom依赖

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>

全部的pom依赖是

<?xml version="1.0" encoding="UTF-8"?>
<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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.test</groupId><artifactId>consumer</artifactId><version>1.0-SNAPSHOT</version><properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR3</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId><version>2.2.2.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

yml配置中不用增加任何配置

其次是在引导类上加注解@EnableCircuitBreaker

所以引导类就成了下面这样:

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableEurekaClient
@EnableCircuitBreaker //开启熔断器
public class ServiceConsumerApplication {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ServiceConsumerApplication.class, args);}
}

局部熔断

然后在controller方法中增加熔断方法。之后通过注解使得被调用方法熔断后执行熔断方法,熔断方法参数和被调用方法的参数应该一致。

@RestController
@RequestMapping("consumer/test")
public class TestController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;  //拉取所有的服务信息@GetMapping@ResponseBody@HystrixCommand(fallbackMethod = "testFallback")  //通过此注解使得此方法熔断后执行熔断方法public String test(@RequestParam("id") String id){//可能会有多个同名服务,故使用listList<ServiceInstance> instances = discoveryClient.getInstances("service-provider");ServiceInstance instance = instances.get(0);return this.restTemplate.getForObject("http://"+instance.getHost()+":"+ instance.getPort() +"/test/" + id,String.class);
//        return this.restTemplate.getForObject("http://localhost:8079/test/"+id , String.class);}/*** 熔断之后执行的方法* @param id* @return*/public String testFallback(String id){return "熔断--服务正忙,请求稍后再试!";}}

接下里我们测试一下,启动eureka和注册中心客户端,不要启动注册中心服务端。然后调用客户端的方法。本文章中的调用接口是:http://localhost/consumer/test?id=456  (等号后边的是传入参数,可以随意改!)

执行结果如下:

当服务没有执行成功时候,触发熔断,返回熔断的自定义结果。

全局熔断

需要在controller的类上加注解,然后与熔断方法联系在一起。全局的熔断方法参数为空就可以。

@RestController
@RequestMapping("consumer/test")
@DefaultProperties(defaultFallback = "testFallback") //定义全局的熔断方法
public class TestController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;  //拉取所有的服务信息@GetMapping@ResponseBody@HystrixCommand //(fallbackMethod = "testFallback")  //通过此注解使得此方法熔断后执行熔断方法public String test(@RequestParam("id") String id){//可能会有多个同名服务,故使用listList<ServiceInstance> instances = discoveryClient.getInstances("service-provider");ServiceInstance instance = instances.get(0);return this.restTemplate.getForObject("http://"+instance.getHost()+":"+ instance.getPort() +"/test/" + id,String.class);
//        return this.restTemplate.getForObject("http://localhost:8079/test/"+id , String.class);}//    /**
//     * 熔断之后执行的方法
//     * @param id
//     * @return
//     */
//    public String testFallback(String id){
//        return "熔断--服务正忙,请求稍后再试!";
//    }/*** 熔断之后执行的方法* @param* @return*/public String testFallback(){return "全局熔断--服务正忙,请求稍后再试!";}}

之后刷新调用的地址,我们看到了全局熔断。

除此之外我们还可以在yml中配置熔断超时时间,当请求时间超过6秒后才会触发熔断,相当于延时,有兴趣的可以尝试一下。

hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 6000 # 设置hystrix的超时时间为6000ms

【SpringCloud】Hystrix-实例相关推荐

  1. SpringCloud Hystrix熔断器

    SpringCloud Hystrix熔断器 15.Hystrix熔断器:简介及作用 目标:理解Hystrix的作用 介绍:Hystrix,英文意思是豪猪,全身是刺,看起来就不好惹,是一种保护机制. ...

  2. 深入了解SpringCloud Hystrix

    雪崩效应即在多个服务节点当中,如果有一个服务不可用而这个不可用的服务导致整个应用资源都耗在这里,进而影响整个系统的崩溃.在分布式环境中,不可避免地会出现雪崩效应.Hystrix是一个netflix实现 ...

  3. springcloud Hystrix Dashboard微服务监控

    springcloud  Hystrix Dashboard微服务监控简介 Hystrix监控 除了隔离依赖服务的调用以外,Hystrix还提供了近实时的监控,Hystrix会实时.累加地记录所有关于 ...

  4. springcloud hystrix实战(二)

    我们前面介绍完了springcloud hystrix的相关作用,大家也有了一个认识,这个熔断器的作用这个就不在重复. 下面我们就接着进行代码实战,我们是接着之前的微服务的工程继续的,如果有什么不明白 ...

  5. Dubbo(十五)springboot工程dubbo整合SpringCloud Hystrix

    本章将编写一个使用SpringBoot工程集成dubbo使用hystrix组件实现服务熔断示例.包含服务提供者工程和服务消费者工程.主要在实现整合springcloud hystrix过程步骤如下: ...

  6. 一 springcloud hystrix源码深度分析

    spring cloud hystrix原理 每周会定时更新spring cloud hystrix 对netflix公司的产品封装实现原理,在这里,你会学到实实际际的干货,如果文中有不正确的地方,还 ...

  7. SpringCloud入门实例

    SpringCloud微服务 概述 ​ Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线 ...

  8. SpringCloud Hystrix初体验

    文章目录 简介 实验步骤 bookstore应用 初始化应用 主程序 应用配置 访问应用 reading应用 初始化应用 主程序 BookService 应用配置 运行应用 参考资料 简介 Hystr ...

  9. SpringCloud Hystrix介绍以及基于RestTemplate与Feign的改造

    一.Hystrix介绍 Hystrix 是Netfix的一个开源的延迟和容错库,用于隔离访问远程系统.服务或者第三方库,防止服务级联失败,从而提升系统的可用性与容错性.Hystrix主要通过以下几点实 ...

  10. 【Java从0到架构师】SpringCloud - Hystrix、Zuul

    SpringCloud 基本概念 熔断和降级 服务雪崩效应 服务熔断与降级 - Hystrix SpringBoot 集成 Hystrix 熔断降级服务异常报警通知 重点属性 - 熔断隔离策略.超时时 ...

最新文章

  1. Y项目逸事之中国人设计的全球模板
  2. oracle分页查询数据(收藏)
  3. SQL Server返回当前实例系统日期和时间的函数
  4. ECC密钥结构和密码学基础
  5. 计算机一级发邮件发送多个抄送,计算机一级考试IE题和收发邮件模拟题.docx
  6. ubuntu6.06容易死机的一种解决方法
  7. CSAPP-计算机漫游
  8. collector_使用Data Collector进行SQL Server性能监视–第3部分–阅读报告
  9. 2013应届毕业生“东方通”校招应聘总结
  10. 第2节 mapreduce深入学习:15、reduce端的join算法的实现
  11. asp 基础操作之增删改查
  12. Android开发之实现多次点击事件
  13. 【Mac系统】下载与安装Matlab2019b
  14. 计算机电子商务o2o,电商o2o模式
  15. Android MTK log分析
  16. 自然语言处理基于java实现(1) 之 中文分词
  17. POJ 1979 Red and Black(红与黑)
  18. 比SQL还好用,又一门国产数据库语言诞生了
  19. 51单片机密码锁(含确认键、清零键、删除键)
  20. 通过文献计量学助您发表高影响因子论文—基于Citespace和vosviewer文献计量学可视化SCI论文高效写作方法

热门文章

  1. linux内核调试技术 kprobe使用与实现
  2. linux编程之GDB调试
  3. 用Font Awesome代替网页icon小图标
  4. 汇编中的字符串操作指令
  5. GetLastError编号含义
  6. Linux C编程--网络编程1--字节顺序和字节处理函数
  7. 排序算法--睡眠排序
  8. 机器人x展架制作_门型展架80*180易拉宝x展架海报架广告架立牌展示架地推海报2元优惠券券后价20元...
  9. 电子计算机是一种精准的仪器,常见的八种无损检测仪器
  10. java大量的print影响性能吗_printStackTrace()造成的性能瓶颈