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的支持的更多相关文章

  1. Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar

    Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar 前言 公司有一个调研要做,调研如何将Python语言提供的服务纳入到Spring Clou ...

  2. SpringCloud学习笔记(11)----Spring Cloud Netflix之Hystrix断路器的使用

    为什么会有断路器? 在微服务架构中,系 是拆分成 一个的服务单元各间通过注册与发现 的方式互相依 赖.每个单元都在不同的进程中运行, 都是通过远程调用的方式进行信 ,这样就有可能因为网络原或 是依赖服 ...

  3. springCloud学习-消息总线(Spring Cloud Bus)

    1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...

  4. SpringCloud学习笔记(16)----Spring Cloud Netflix之Hystrix Dashboard+Turbine集群监控

    前言: 上一节中,我们使用Hystrix Dashboard,只能看到单个应用内的服务信息.在生产环境中,我们经常是集群状态,所以我们需要用到Turbine这一应用. 作用:汇总系统内的多个服务的数据 ...

  5. SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用

    1. 引入依赖 在前面几节中的消费者中添加pom依赖. <dependency> <groupId>org.springframework.cloud</groupId& ...

  6. SpringCloud学习笔记(13)----Spring Cloud Netflix之Hystrix断路器的隔离策略

    说明 : 1.Hystrix通过舱壁模式来隔离限制依赖的并发量和阻塞扩散 2. Hystrix提供了两种隔离策略:线程池(THREAD)和信号量隔离SEMAPHORE). 1. 线程池隔离(默认策略模 ...

  7. SpringCloud学习笔记(12)----Spring Cloud Netflix之Hystrix断路器的流程和原理

    工作流程(参考:https://github.com/Netflix/Hystrix/wiki/How-it-Works) 1. 创建一个HystrixCommand或HystrixObservabl ...

  8. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

  9. SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用

    1.  Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...

随机推荐

  1. 尝试实现bootstrap3网格系统

    这是一篇搁置了很久的博文,个人实现的关键代码如下: // 这是用sass实现的,只是大致实现了网格系统和offset的功能 $size_list: ( xs: 0, sm: 576, md: 992, ...

  2. javascript中缓存

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Android 高大上的侧滑菜单DrawerLayout,解决了不能全屏滑动的问题

    DrawerLayout预览 DrawerLayout主要功能就是 实现侧滑菜单效果的功能,并且可以通过增加一些设置来实现高大上的效果,那么就请看动态图:   注意左上角那个图标,有木有很好玩,哈哈. ...

  4. Sentry: Python 实时日志平台

    Links https://docs.getsentry.com/on-premise/quickstart/ https://docs.getsentry.com/on-premise/server ...

  5. SSH 项目中 用Hibernate底层 简单的封装DAO层

    废话不多少了,主要是使用hibernate的查询方法,自己封装了DAO层,供service来方便使用. 首先:必须要继承的 public class CommonDao extends Hiberna ...

  6. C++快速读取大文件

    debug的时候需要等很长时间读模型,查资料发现了两种快速读取大文件的方法. test 1:每次读一个字符串 test 2.3一次读取整个文件 {//test 1 string buf; clock_ ...

  7. PHP小常识分享

    PHP 标记 当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php 和 ?>,这告诉 PHP 开始和停止解析二者之间的代码.此种解析方式使得 PHP 可以被嵌入到各种不同的文 ...

  8. GCC中的强符号和弱符号及强引用和弱引用

    1. 强符号和弱符号 1.1 u-boot和kernel中的__weak指令 u-boot和kernel比较普遍地使用了__weak来定义函数. 在include\linux\compiler-gcc ...

  9. 斯特林公式--取N阶乘近似值

    斯特林公式(Stirling's approximation)是一条用来取n的阶乘的近似值的数学公式.一般来说,当n很大的时候,n阶乘的计算量十分大,所以斯特林公式十分好用,而且,即使在n很小的时候, ...

  10. java分页之假分页

    假分页,顾名思义,不是真正的在数据库里进行过滤,而是从数据库查询之后,取得全部结果,在展现的时候做些手脚. import java.util.ArrayList; import java.util.L ...