spring cloud(断路器——初学五)
Feign使用Hystrix
因为feign已经依赖了hystrix,所以可以直接使用,无需添加再次添加依赖。
1、使用@FeignClient
注解中的fallback属性指定回调类
package com.daqsoft; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
public interface ComputerClient {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
2、创建回调类ComputeClientHystrix
,实现@FeignClient
的接口,此时实现的方法就是对应@FeignClient
接口中映射的fallback函数
package com.daqsoft; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam; /**
* @Description Created by liaoxx on 2017-6-13.
*/
@Component
public class ComputeClientHystrix implements ComputerClient {
@Override
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -1;
}
}
3、web调用
package com.daqsoft; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@RestController
public class CustomController { @Autowired
private ComputerClient computerClient; @RequestMapping(value = "/add", method = RequestMethod.GET)
public Integer add(){
return computerClient.add(10,20); }
}
4、启动服务,访问http://localhost:3333/add
报错,无法进入回调
5、修改配置文件,添加属性
spring.application.name=ribbon-consumer server.port=3333
#服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ #开启hystrix支持
feign.hystrix.enabled=true
6、再次启动服务,访问http://localhost:3333/add (正常访问回调方法)
spring cloud(断路器——初学五)的更多相关文章
- spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...
- spring cloud 入门系列五:使用Feign 实现声明式服务调用
一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...
- Spring Cloud断路器Hystrix
在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...
- Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收
前言 不写随笔的日子仿佛就是什么都没有产出一般--上节说到要学Spring Cloud Bus,这里发现按照官方文档的顺序反而会更好些,因为不必去后边的章节去为当前章节去打基础,所以我们先学习Spri ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- Spring Cloud 微服务五:Spring cloud gateway限流
前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...
- Spring Cloud Alibaba(五)RocketMQ 异步通信实现
本文探讨如何使用 RocketMQ Binder 完成 Spring Cloud 应用消息的订阅和发布. 介绍 RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的.高 ...
- Spring Cloud Gateway(五):路由定位器 RouteLocator
本文基于 spring cloud gateway 2.0.1 1.简介 直接 获取 路 由 的 方法 是 通过 RouteLocator 接口 获取. 同样, 该 顶 级 接口 有多 个 实现 类, ...
- Spring Cloud Eureka(五):Eureka Server 启动流程分析
启用EurekaServer @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public st ...
- spring cloud学习笔记五 网关服务zuul
网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用. 网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...
随机推荐
- python + Jquery,抓取西东网上的Java教程资源网址
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-06-15 14:01:45 # @Author : Chenjun (320 ...
- springcloud ConfigServer的工作原理
前话 根据前文得知,bootstrapContext引入了PropertySourceLocator接口供外部源加载配置,但作用是应用于子级ApplicationContext的环境变量Environ ...
- 249. Group Shifted Strings把迁移后相同的字符串集合起来
[抄题]: Given a string, we can "shift" each of its letter to its successive letter, for exam ...
- [leetcode]156.Binary Tree Upside Down颠倒二叉树
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- 考研部分概念和流程(若不全和错误可提示我补充,另考研帮app推荐)
上大学必须经过全国统一高考,而就读硕士研究生的途径相对而言要多一些,也更灵活一些.已经工作的人,除了放弃工作报考研究生以外,还可以不脱产申请攻读学位,或申请单独考试.不脱产申请攻读学位,通俗的讲,就是 ...
- Pandas 合并merge
pandas中的merge和concat类似,但主要是用于两组有key column的数据,统一索引的数据. 通常也被用在Database的处理当中. 1.依据一组key合并 >>> ...
- stc15w wave
1. 定时器和延时 #include "15W4KxxS4.h" #define FOSC 12000000 #define CLK (65536-FOSC/2/12/1000) ...
- centos7 mysql的安装与配置
用yum直接安装,不用更改配置 1. #yum install mysql #yum install mysql-server #yum install mysql-devel yum运行报错的化有可 ...
- [JAVA]JAVA章2 IOC与AOP是啥
使用Spring框架的过程中,其实就是为了使用IOC(依赖注入),和AOP(面向切面编程),这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC就是典型的工厂模式,通过ses ...
- python参数
1.形参变量和实参 形参变量:只有在被调用时才分配内存单元,在调用结束时,即释放所分配的内存单元,因此,形参只在函数内有效,函数调用结束返回主调用函数后则不能再使用该形参变量. 实参:可以是常量,变量 ...