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. node.js中通过stream模块实现自定义流

    有些时候我们需要自定义一些流,来操作特殊对象,node.js中为我们提供了一些基本流类. 我们新创建的流类需要继承四个基本流类之一(stream.Writeable,stream.Readable,s ...

  2. maven打包不包含配置文件[z]

    如果使用maven-jar-plugin和maven-dependency-plugin打包,排除配置文件的方法: 上面的配置是把resources下的配置文件不打进项目的jar包中,主要使用的是&l ...

  3. autoMapper dotnetcore webapi 自动添加映射 abp

    在ef的xxxxApplicationModule的Initialize方法中,已经添加了自动映射的配置,如下图: 写的很明白了,我们只需要写一个类继承Profile就可以了.如下图所示: 这样就可以 ...

  4. journalctl 清理journal日志

    在CentOS 7开始使用的systemd使用了journal日志,这个日志的管理方式和以往使用syslog的方式不同,可以通过管理工具维护. 使用df -h检查磁盘文件,可以看到/run目录下有日志 ...

  5. ES6 扩展运算符

    ES6的扩展运算符则可以看作是rest参数的逆运算.可以将数组转化为参数列表. 如:console.log(1,...[2,3,4],5) //1 2 3 4 5 用于合并数组: [1,2, ...m ...

  6. 基于springboot的restful接口的单元测试示例

    一.知识点 代码中对应的知识点 1.jsonPath github网址 1)操作符见文档 2)方法见文档 3)例子见文档 2.MockMvc(org.springframework.test.web. ...

  7. AX_InventDim

    static void Job1(Args _args) { ; info(InventDim::find("D00000001").preFix()); } public voi ...

  8. 结合OPENSIFT源码详解SIFT算法

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 参考博客:https://www.cnblogs.com/cql ...

  9. nigx

    1.反向代理 2.负载均衡(weight:设置权重) 3.高可用(场景:当主服务器死掉后 拥有备用服务器承接后续的访问) 安装 Keepalived实现

  10. python模块:random

    """Random variable generators. integers -------- uniform within range sequences ----- ...