1、导入依赖spring-cloud-starter-hystrix

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

2、消费启动类开启@EnableCircuitBreaker

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
//开启Ribbon
@RibbonClient(name="cloud-producer")
//启动断路器支持(Hystrix)
@EnableCircuitBreaker
public class RibbonConsumerApplication { @Bean
@LoadBalanced //负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
} }

3、TestService——设置断路器核心类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Repository
public class TestService { @Autowired
private RestTemplate restTemplate; //设置断路器,当此方法无法应答时(把mima-cloud-producer服务停掉),调用getError方法
@HystrixCommand(fallbackMethod="getError")
public String get(String id) {
System.out.println(Thread.currentThread().getName()+".get before...");
String result = restTemplate.getForObject("http://cloud-producer/get/"+id, String.class);
System.out.println(Thread.currentThread().getName()+".get end...result="+result);
return result;
} public String getError(String id) {
System.out.println(Thread.currentThread().getName()+"断路器启动");
return "断路器fallback返回error";
}
}

4、TestController——测试类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.mimaxueyuan.consumer.robbin.service.TestService; @RestController
public class TestController { @Autowired
private TestService testService; @GetMapping("/ribbon/get/{id}")
public String get(@PathVariable String id) {
return testService.get(id);
}
}

以上代码在cloud-consumer-ribbon-hystrix服务中,模拟远程调用cloud-producer服务。

5、模拟测试
5.1、启动服务
启动cloud-consumer-ribbon-hystrix、cloud-producer服务,保证服务正常。

5.2、正常请求

5.3、服务挂掉请求,触发断路器
把cloud-producer服务关闭,这时http://localhost:8807/ribbon/get/123456请求无法应答,会调用getError方法,触发断路器

5.4、多次请求控制台会打印如下信息:

Ribbon使用Hystrix的更多相关文章

  1. 003客户端负载均衡Ribbon & 短路器Hystrix

    1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka.Ribbon.Hystrix依赖和Spring Cloud依赖管理 <dependencies> <!- ...

  2. Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)

    Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...

  3. Feign整合Ribbon和Hystrix源码解析

    在上篇文章Feign自动装配中,我们提到了Feign的自动装配的原理,以及Feign整合Ribbon和Hystrix的核心在类FeignClientFactoryBean中,那么本篇文章就来揭开这个类 ...

  4. Spring Cloud Ribbon 整合 Hystrix

    在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...

  5. 一文读懂SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系

    概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...

  6. spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?

    Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...

  7. 微服务:Eureka+Zuul+Ribbon+Feign+Hystrix构建微服务架构

    原文地址:http://blog.csdn.net/qq_18675693/article/details/53282031 本案例将打架一个微服务框架,参考来源官方参考文档 微服务:是什么?网上有一 ...

  8. SpringCloud及其五大常用组件之Feign、Ribbon和Hystrix

    1.Feign 我们已经将Eureka和Zuul开发完毕,而且上面注册了两个微服务,现在我们实现两个微服务之间的调用. String baseUrl = "http://127.0.0.1: ...

  9. SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系

    Eureka:各个服务启动时,Eureka Client都会将服务注册到Eureka Server,并且Eureka Client还可以反过来从Eureka Server拉取注册表,从而知道其他服务在 ...

随机推荐

  1. jsp相关笔记(二)

    在jsp中将数据库表格内容读出为一个表格,并在表格中添加超链接: <%@ page language="java" contentType="text/html; ...

  2. css初始

    css概念及作用 css即层叠样式表的英文缩写 作用:1 渲染页面   2 页面布局 css语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 格式: selector{ prope ...

  3. laravel config 配置无效

    修改了配置文件config  发现逻辑代码中并无生效. 猜测缓存,所以执行下: php artisan config:cache 缓存文件默认会存在bootstrap/cache 中,并不在stora ...

  4. rabbit初学之连接测试2

    com.rabbitmq.client.ShutdownSignalException: connection error 发现,port是5672,不是15672(15672是后台管理平台的端口)

  5. AX_Dialog

    Dialog                      dialog  = new Dialog("@SYS1052");  DialogField                 ...

  6. CSS3美化网页!!

    一.span标签:能让某几个文字或者某个词语凸显出来        <p>           今天是11月份的<span>第一天</span>,地铁卡不打折了  ...

  7. CSS3知识!

    一.引入样式 1.行内样式表 <h1 style="color: red;font-size: 18px;">10-30</h1> 2.内部样式表(在hea ...

  8. Unity、C#读取XML

    有如下XML文件: <?xml version="1.0" encoding="UTF-8"?> <DataNode> <prov ...

  9. MySQL-5.7安装

    2.1 下载mysql 网址:https://www.mysql.com/ [root@localhost ~]# mkdir -p /root/soft/MySQL [root@localhost ...

  10. Linux(以centos7为例)下自动挂载NTFS硬盘

    Linux(以centos7为例)下自动挂载NTFS硬盘 作者:子敬叔叔 编写时间:2017年5月3日星期三 需求:     为了熟悉Linux的使用,在自己的笔记本上新安装一个centos7, 但我 ...