Spring Cloud【Finchley】-09Feign使用Hystrix

简介: Spring Cloud【Finchley】-09Feign使用Hystrix

20190806093230928.jpg


版本说明

先说下使用的spring cloud和spring boot的版本

2018121622383697.png


Disable HystrixCommands For FeignClients By Default

https://github.com/spring-cloud/spring-cloud-netflix/issues/1277


新建子module


父工程microservice-spring-cloud右键新建Maven Module 命名为:micorservice-consumer-movie-feign-hystrix ,为了简单我们把micorservice-consumer-movie-feign的内容copy到该子模块,修改下application.yml中的spring.application.name即可。


application.yml中开启Hystrix

server:
  port: 7901
spring: 
  application:
    name: micorservice-consumer-movie-feign-hystrix  
#eureka
eureka: 
  client:
    service-url:
      defaultZone: http://artisan:artisan123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
# Disable HystrixCommands For FeignClients By Default
# https://github.com/spring-cloud/spring-cloud-netflix/issues/1277    
feign:
  hystrix:
    enabled: true



如果是application.property ,请设置 feign.hystrix.enabled=true


修改Feign接口

使用fallback属性指定回退类

20181216224536532.png


回退类 也需要实现上面的接口,同时需要标注@Component让其成为受spring管理的bean


2018121622462896.png


测试

  1. 启动microservice-discovery-eureka,注册中心
  2. 启动micorservice-provider-user,服务提供者
  3. 启动micorservice-consumer-movie-feign-hystrix,服务消费者开启了Hystrix


访问http://localhost:8761/ 确认下服务已经注册成功。


2018121622493710.png


访问 http://localhost:7901/movie/1

{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}


功能正常,OK。

现在停掉micorservice-provider-user

20181216224849762.png

访问 http://localhost:7901/movie/1 ,进入了回退方法

{"id":1,"username":"默认用户","name":null,"age":null,"balance":null}


再次启动 micorservice-provider-user


20181216225116320.png

再次访问 http://localhost:7901/movie/1

{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}


功能正常,OK。


代码

https://github.com/yangshangwei/SpringCloudMaster/tree/master/micorservice-consumer-movie-fegin-hystrix

相关文章
|
2月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
47 6
|
2月前
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
81 5
|
2月前
|
缓存 监控 Java
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
60 5
|
5月前
|
Java API 开发者
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
914 0
|
5月前
|
Java 对象存储 开发者
故障隔离与容错处理:Hystrix在Spring Cloud和Netflix OSS中的应用
故障隔离与容错处理:Hystrix在Spring Cloud和Netflix OSS中的应用
82 3
|
5月前
|
XML 监控 Java
Spring Cloud全解析:熔断之Hystrix简介
Hystrix 是由 Netflix 开源的延迟和容错库,用于提高分布式系统的弹性。它通过断路器模式、资源隔离、服务降级及限流等机制防止服务雪崩。Hystrix 基于命令模式,通过 `HystrixCommand` 封装对外部依赖的调用逻辑。断路器能在依赖服务故障时快速返回备选响应,避免长时间等待。此外,Hystrix 还提供了监控功能,能够实时监控运行指标和配置变化。依赖管理方面,可通过 `@EnableHystrix` 启用 Hystrix 支持,并配置全局或局部的降级策略。结合 Feign 可实现客户端的服务降级。
293 23
|
7月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
174 3
|
7月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
766 15
|
7月前
|
消息中间件 Java Nacos
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
97 0
|
8月前
|
消息中间件 Java 持续交付
Spring Cloud Alibaba 项目搭建步骤和注意事项
Spring Cloud Alibaba 项目搭建步骤和注意事项
945 0
Spring Cloud Alibaba 项目搭建步骤和注意事项