在前面随笔Spring Cloud 之 Feign的feign工程基础上进行改造

1.pom.xml依赖不变

2.application.yml文件添加feign.hystrix.enabled=true开启Hystrix断路器,即:

spring:
application:
name: feign
server:
port: 8766
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
feign:
hystrix:
enabled: true

3.新建Feign Hystrix 调用失败的回调类HystrixErrorFallBack

package com.dzpykj.hystrixService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import com.dzpykj.feignInterface.HelloInterface; @Component
public class HystrixErrorFallBack implements HelloInterface { @Value("${server.port}")
String port; @Override
public String hello(String name) {
return "Sorry "+name+",when you are visting feign hystrix project,port:"+port+",you meet an error";
} }

4.在@FeignClient接口加入fallback回调类

package com.dzpykj.feignInterface;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.dzpykj.hystrixService.HystrixErrorFallBack; @FeignClient(value = "eurekaclient",fallback = HystrixErrorFallBack.class) //value为要负载均衡的spring.application.name
public interface HelloInterface { @RequestMapping("/hi") //负载均衡目标工程里面的哪个方法
public String hello(@RequestParam(value="name") String name);
}

5.依次启动Eureka服务集群、Eureka单个客户端、Feign工程

5.1 按照Spring Cloud Eureka Server集群Demo级搭建的步骤启动Eureka服务peer1,peer2集群

5.2按照Spring Cloud Eureka服务Demo级搭建启动8763的Eureka客户端

5.3启动Feign工程

6.访问 http://localhost:8766/hello/chaixy

7.模拟eurekaclient服务异常:手动将eurekaclient服务关闭,再次访问 http://localhost:8766/hello/chaixy

可以看到,当eurekaclient服务关闭时,访问遇到异常,回调了异常回调类HystrixErrorFallBack

Spring Cloud Feign 整合 Hystrix的更多相关文章

  1. Spring Cloud Ribbon 整合 Hystrix

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

  2. Spring Cloud Feign 声明式服务调用

    目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? ​ 通过对前面Sp ...

  3. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  4. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  5. SpringCloud---声明式服务调用---Spring Cloud Feign

    1.概述 1.1 Spring Cloud Ribbon.Spring Cloud Hystrix的使用几乎是同时出现的,Spring Cloud提供了一个更高层次的封装这2个工具类框架:Spring ...

  6. Spring cloud Feign 深度学习与应用

    简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...

  7. Spring Cloud Feign高级应用

    1.使用feign进行服务间的调用 spring boot2X整合nacos一使用Feign实现服务调用 2.开启gzip压缩 Feign支持对请求与响应的压缩,以提高通信效率,需要在服务消费者配置文 ...

  8. Greenwich.SR2版本的Spring Cloud Feign实例

    前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通 ...

  9. 微服务实战SpringCloud之Spring Cloud Feign替代HTTP Client

    简介 在项目中我们有时候需要调用第三方的API,微服务架构中这种情况则更是无法避免--各个微服务之间通信.比如一般的项目中,有时候我们会使用 HTTP Client 发送 HTTP 请求来进行调用,而 ...

随机推荐

  1. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  2. Golang访问Redis初体验

    go语言的client在redis官网上有很多l客户端,个人感觉redigo使用起来更人性化,重要的是源代码结构很清晰,重要的是支持管道.发布和订阅.连接池等等,所以我选择redigo作为尝试. 1. ...

  3. swift 之 namespace

    场景:  项目中类名过长,造成不能根据文件名区分出来,并且如果一个模块的类较多时,很难取一个比较优雅的名字.为了使模块名可读, 我们一般的做法就是添加模块前缀.但是如果模块中还有个子模块,如果还继续按 ...

  4. 爱上朴实的CSS细节

    英文原文:Learning to Love the Boring Bits of CSS  未来的CSS太让人兴奋了:一方面,是全新的页面布局方式:另一方面,是酷炫的滤镜.颜色等视觉效果.这些CSS, ...

  5. 老大哥在看着你!我国部署超2000万个AI监控系统

    原文:Big brother is watching you! China installs 'the world's most advanced video surveillance system' ...

  6. 【JAVA零基础入门系列】Day12 Java类的简单应用

    俗话说的好,实践出真知,所以除了理论知识掌握扎实以外,更重要的是要多加操练,这样才能掌握核心科技. 今天我们就用刚学会的类来实践一下,目标便是完成上一篇中的剁手任务. 我们的商品类已经准备好了,代码重 ...

  7. MAC - PhpStorm安装调试环境xdebug

    今天下午一直在捣鼓如何用PhpStorm进行调试,查找了许多资料,零零碎碎的,所以自己弄篇文章记录一下步骤. 安装xdebug 使用brew安装xdebug,语法如下 brew install hom ...

  8. Linux下打造全方位立体监控系统

    前言 本文主要介绍如何使用Grafana和Prometheus以及node_exporter对Linux服务器性能进行监控.下面两张图分别是两台服务器: 服务器A 服务器B 概述 Prometheus ...

  9. java Mybatis框架动态SQL

    1.if+where <select id="getgoods" resultType="Goods" > select * from goods ...

  10. ASP.NET Core集成现有系统认证

    我们现在大多数转向ASP.NET Core来使用开发的团队,应该都不是从0开始搭建系统,而是老的业务系统已经在运行,ASP.NET Core用来开发新模块.那么解决用户认证的问题,成为我们的第一个拦路 ...