SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持
1. Hystrix对Feign的支持
添加Feign中IUserBiz的实现类HystrixFallBack:
package com.wangx.cloud.springcloud02consumer.configure; import com.wangx.cloud.springcloud02consumer.api.UserApi;
import org.springframework.stereotype.Component; @Component
public class HystrixFallBack implements UserApi {
@Override
public String getUser(Integer id) {
System.out.println("连接超时.....");
return "system error";
} }
使用@component注解注册成组件。在@FeignClient注解里面添加fallback属性即可。
如下:
package com.wangx.cloud.springcloud02consumer.api; import com.wangx.cloud.springcloud02consumer.configure.FooConfiguration;
import com.wangx.cloud.springcloud02consumer.configure.HystrixFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "spring-cloud-provider", configuration = FooConfiguration.class, fallback = HystrixFallBack.class)
public interface UserApi { @RequestMapping(value = "/user/getUser")
String getUser(@RequestParam(value = "id") Integer id); }
当出现请求错误或超时时,就会执行实现类中的方法。
熔断设置
#默认为false,如果想用断路由,要打开这个设置
feign.hystrix.enabled=true #断路器线程池超时时间,这个值一定要比ribbon超时时间长,毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=16000
单个应用禁用Hystrix
在FooConfiguration中配置一个bean
package com.wangx.cloud.springcloud02consumer.configure; import feign.Feign;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; @Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
配置隔离级别
# 在feign和Ribbon里面配置隔离策略(全局配置)
#hystrix.command.default.execution.isolation.strategy=SEMAPHORE
# 配置单个 ystrixCommandKey在Ribbon下面默认为方法名,在feign下面默认为类名#方法名(参数类型)
#hystrix.command.HystrixCommandKey.execution.isolation.strategy=SEMAPHORE
说明:HystrixCommandKey在Ribbon下面默认为方法名,在feign下面默认为类名#方法名(参数类型)
Feign下的HystrixCommandKey类的配置
package com.wangx.cloud.springcloud02consumer.configure; import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommand.Setter;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey; import feign.Feign;
import feign.Target;
import feign.hystrix.HystrixFeign;
import feign.hystrix.SetterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; import java.lang.reflect.Method; @Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
class RcSetterFactory implements SetterFactory {
@Override
public Setter create(Target<?> target, Method method) {
String groupKey = target.name();
String commandKey = method.getName();
return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
}
}
return HystrixFeign.builder().setterFactory(new RcSetterFactory());
//return Feign.builder();
}
}
SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持的更多相关文章
- Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar
Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar 前言 公司有一个调研要做,调研如何将Python语言提供的服务纳入到Spring Clou ...
- SpringCloud学习笔记(11)----Spring Cloud Netflix之Hystrix断路器的使用
为什么会有断路器? 在微服务架构中,系 是拆分成 一个的服务单元各间通过注册与发现 的方式互相依 赖.每个单元都在不同的进程中运行, 都是通过远程调用的方式进行信 ,这样就有可能因为网络原或 是依赖服 ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- SpringCloud学习笔记(16)----Spring Cloud Netflix之Hystrix Dashboard+Turbine集群监控
前言: 上一节中,我们使用Hystrix Dashboard,只能看到单个应用内的服务信息.在生产环境中,我们经常是集群状态,所以我们需要用到Turbine这一应用. 作用:汇总系统内的多个服务的数据 ...
- SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用
1. 引入依赖 在前面几节中的消费者中添加pom依赖. <dependency> <groupId>org.springframework.cloud</groupId& ...
- SpringCloud学习笔记(13)----Spring Cloud Netflix之Hystrix断路器的隔离策略
说明 : 1.Hystrix通过舱壁模式来隔离限制依赖的并发量和阻塞扩散 2. Hystrix提供了两种隔离策略:线程池(THREAD)和信号量隔离SEMAPHORE). 1. 线程池隔离(默认策略模 ...
- SpringCloud学习笔记(12)----Spring Cloud Netflix之Hystrix断路器的流程和原理
工作流程(参考:https://github.com/Netflix/Hystrix/wiki/How-it-Works) 1. 创建一个HystrixCommand或HystrixObservabl ...
- SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性
1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...
- SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用
1. Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...
随机推荐
- 七牛上图片总是net::ERR_NAME_NOT_RESOLVED
七牛上图片总是net::ERR_NAME_NOT_RESOLVED >> php这个答案描述的挺清楚的:http://www.goodpm.net/postreply/php/101000 ...
- Caffe CommonLayer分析
Caffe CommonLayer分析 \(Caffe\)中包含了很多通用的功能层,包含了\(concat\),\(slice\),\(split\),\(crop\),\(flip\),\(scal ...
- UVa 11729 Commando War 【贪心】
题意:有n个部下,交待每个部下完成他相应的任务需要bi的时间,然后完成这项任务需要ji的时间, 选择交待任务的顺序,使得总的花费的时间最少 因为不管怎么样,交待所需要的n*bi的时间都是要花费的, 然 ...
- Appstore排名前十的程序员应用软件
程序员又名程序猿,苦逼劳累的代名词,曾经一个朋友这么开玩笑说,如果你是富二代,你当程序员就是脑残,如果你是穷二代,当程序员的话,死的时候一定是趴键盘. 程序员 哦,可怜的程序员.在那山的这边海的那边有 ...
- 浅谈AVL树,红黑树,B树,B+树原理及应用
背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上说mysql的存储引擎InnoDB采用的索引类型是B+Tree,那么,大家有没有产生这样一个疑问,对于数据索引,为什么要使 ...
- Pyhton学习——Day59
参考博客: http://www.cnblogs.com/wupeiqi/articles/6144178.html Form 1. 验证 2. 生成HTML(保留上次输入内容) 3. 初始化默认是 ...
- vue登录
<template> <section class="wrap-page wrap-page-u" style="padding-top:2rem;&q ...
- [USACO 2009 Feb Gold] Fair Shuttle (贪心+优先队列)
题目大意:有N个站点的轻轨站,有一个容量为C的列车起点在1号站点,终点在N号站点,有K组牛群,每组数量为Mi(1≤Mi≤N),行程起点和终点分别为Si和Ei(1≤Si<Ei≤N).计算最多有多少 ...
- Numpy的使用规则
之前安装的python版本是3.7 各种库都是自己一个一个下载安装的 很操心 各种缺功能 后来发现了anaconda 啊 真是一个好东西 简单来说 它就是一个涵盖大部分常用库的python包 一次安装 ...
- 2019-03-20 Python爬取需要登录的有验证码的网站
当你向验证码发起请求的时候,就有session了,记录下这次session 因为每当你请求一次验证码 或者 请求一次登录首页,验证码都在变动 验证码的链接可能不是固定的,可能需要GET/POST请求, ...