15SpringCloud - 断路器项目示例(Feign Hystrix)

简介: 15SpringCloud - 断路器项目示例(Feign Hystrix)

Feign中使用断路器

添加依赖

Feign是自带断路器的,如果在Dalston版本的Spring Cloud中,它没有默认打开。需要需要在配置文件中配置打开它,本项目我们是不需要打开的。

feign:
  hystrix:
    enabled: true

服务注册

修改HomeClient类 ,@FeignClient 注解,加上fallbackFactory指定新建的HystrixClientFallbackFactory 工厂类

在程序的启动类 RibbonConsumerApplication 通过 @EnableHystrix 开启 Hystrix

/**
 * 描述: 指定这个接口所要调用的 提供者服务名称 "eureka-provider"
 **/
@FeignClient(value ="eureka-provider",fallbackFactory = HystrixClientFallbackFactory.class)
public interface  HomeClient {
    @GetMapping("/")
    String consumer();
}

新加的类HystrixClientFallbackFactory.java

@Component
public class HystrixClientFallbackFactory implements FallbackFactory<HomeClient> {
    @Override
    public HomeClient create(Throwable throwable) {
        return () -> "feign + hystrix ,提供者服务挂了";
    }
}

测试断路器

依次启动项目:

spring-cloud-eureka-service

spring-cloud-eureka-provider-1

spring-cloud-eureka-provider-2

spring-cloud-eureka-provider-3

spring-cloud-feign-consumer-hystrix

启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/

在命令窗口curl http://localhost:9000/hello,发现一切正常

或者浏览器get 请求http://localhost:9000/helloF5 刷新

停止 spring-cloud-eureka-provider-1提供者,端口为:8081服务

再次访问命令窗口curl http://localhost:9000/hello ,断路器已经生效,提示:Feign + hystrix ,提供者服务挂了。

目录
相关文章
|
2月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
170 0
|
1天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
7天前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
10 0
|
20天前
|
传感器 数据采集 监控
基于Springcloud可视化项目:智慧工地可视化大数据云平台源码
终端层,充分利用物联网技术和移动应用提高现场管控能力。通过传感器、摄像头等终端设备,实现对项目建设过程的实时监控、智能感知、数据采集和高效协同,提高作业现场的管理能力。
28 5
SpringCloud Feign报错Method has too many Body parameters
SpringCloud Feign报错Method has too many Body parameters
|
23天前
|
Nacos
SpringCloud Feign使用
SpringCloud Feign使用
23 1
|
1月前
Springcloud-ribbon和hystrix配置
Springcloud-ribbon和hystrix配置
8 0
|
2月前
|
Java Spring
spring cloud 通过feign请求动弹设置请求头heades
spring cloud 通过feign请求动弹设置请求头heades
21 0
|
2月前
|
SpringCloudAlibaba Java 测试技术
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
43 1
|
2月前
|
开发工具 git 微服务
【二十三】搭建SpringCloud项目六(Config)配置中心动态刷新
【二十三】搭建SpringCloud项目六(Config)配置中心动态刷新
19 0