Feign使用Hystrix

  因为feign已经依赖了hystrix,所以可以直接使用,无需添加再次添加依赖。

  1、使用@FeignClient注解中的fallback属性指定回调类

  1. package com.daqsoft;
  2.  
  3. import org.springframework.cloud.netflix.feign.FeignClient;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7.  
  8. /**
  9. * @Description Created by liaoxx on 2017-6-12.
  10. */
  11. @FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
  12. public interface ComputerClient {
  13. @RequestMapping(method = RequestMethod.GET, value = "/add")
  14. Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
  15. }

  2、创建回调类ComputeClientHystrix,实现@FeignClient的接口,此时实现的方法就是对应@FeignClient接口中映射的fallback函数

  1. package com.daqsoft;
  2.  
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5.  
  6. /**
  7. * @Description Created by liaoxx on 2017-6-13.
  8. */
  9. @Component
  10. public class ComputeClientHystrix implements ComputerClient {
  11. @Override
  12. public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
  13. return -1;
  14. }
  15. }

  3、web调用

  1. package com.daqsoft;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. /**
  9. * @Description Created by liaoxx on 2017-6-12.
  10. */
  11. @RestController
  12. public class CustomController {
  13.  
  14. @Autowired
  15. private ComputerClient computerClient;
  16.  
  17. @RequestMapping(value = "/add", method = RequestMethod.GET)
  18. public Integer add(){
  19. return computerClient.add(10,20);
  20.  
  21. }
  22. }

  4、启动服务,访问http://localhost:3333/add

  

  报错,无法进入回调

  5、修改配置文件,添加属性

  1. spring.application.name=ribbon-consumer
  2.  
  3. server.port=3333
  4. #服务注册中心地址
  5. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
  6.  
  7. #开启hystrix支持
  8. feign.hystrix.enabled=true

  6、再次启动服务,访问http://localhost:3333/add (正常访问回调方法)

  

  

spring cloud(断路器——初学五)的更多相关文章

  1. spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

    spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...

  2. spring cloud 入门系列五:使用Feign 实现声明式服务调用

    一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...

  3. Spring Cloud断路器Hystrix

    在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...

  4. Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收

    前言 不写随笔的日子仿佛就是什么都没有产出一般--上节说到要学Spring Cloud Bus,这里发现按照官方文档的顺序反而会更好些,因为不必去后边的章节去为当前章节去打基础,所以我们先学习Spri ...

  5. Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

    接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...

  6. Spring Cloud 微服务五:Spring cloud gateway限流

    前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...

  7. Spring Cloud Alibaba(五)RocketMQ 异步通信实现

    本文探讨如何使用 RocketMQ Binder 完成 Spring Cloud 应用消息的订阅和发布. 介绍 RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的.高 ...

  8. Spring Cloud Gateway(五):路由定位器 RouteLocator

    本文基于 spring cloud gateway 2.0.1 1.简介 直接 获取 路 由 的 方法 是 通过 RouteLocator 接口 获取. 同样, 该 顶 级 接口 有多 个 实现 类, ...

  9. Spring Cloud Eureka(五):Eureka Server 启动流程分析

    启用EurekaServer @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public st ...

  10. spring cloud学习笔记五 网关服务zuul

    网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用.   网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...

随机推荐

  1. Codeforces Round #499 (Div. 2)

    Codeforces Round #499 (Div. 2) https://codeforces.com/contest/1011 A #include <bits/stdc++.h> ...

  2. fortitoken

    1.token状态为error,且不能分配给用户使用 解决: 关联有User的token状态是error的原因是:用户一直并未使用.

  3. F4 help for month

    INCLUDE rmcs0f0m. s_month FOR s001-spmon NO-EXTENSION NO INTERVALS OBLIGATORY. AT SELECTION-SCREEN O ...

  4. Android 面试问答

    Android 面试问答 目录 数据结构和算法 java核心知识 Android核心知识 架构 设计相关问题 相关工具和技术 Android 测试驱动开发 其他 数据结构和算法 ******关于此类问 ...

  5. vue 前端框架 目录

    vue 前端框架 目录   vue-目录 ES6基础语法 vue基础语法 Vue.js的组件化思想 —上 Vue.js的组件化思想 —下 Vue + Vue-Router结合开发 SublimeSer ...

  6. mysq

    https://blog.csdn.net/wwd0501/article/details/71171614 47.105.144.1 iptables -I INPUT -p tcp --dport ...

  7. PAT 甲级 1002 A+B for Polynomials (25 分)

    1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...

  8. git 忽略文件不起作用

    本人需要提交项目文件,发现总有一些东西不需要提交,然后搜索有”.gitignore”文件可以忽略一些提交,但是发现添加上没有起作用. 要贴的是: /build/ target/ .idea/ *.im ...

  9. spring mvc mybatis shiro构建cms系统ios android

    开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...

  10. Debian 使用 Samba 服务为 Windows 客户端和 Linux 客户端提供文件服务

    1 目标 1.1 主机采用 Debian,为 Windows 和 Liunx 客户端提供文件存取服务 1.2 Windows 采用 GB2312 编码,Linux 采用 UTF-8 编码,要求中文不出 ...