feign+hystrix整合配置

1. 准备一个测试服务端

package com.nmm.study.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/*** <p>Title: FeignServiceController</p>* <p>description: 服务提供者测试</p>** @author niemingming 2021/5/14*/
@RestController
public class FeignServiceController {/*** 根据参入参数匹配不同异常类型* 1:正常* 2:超时* 3:异常* @param type* @param name* @return*/@GetMapping("/v1/feign/test")public String feignConn(int type, String name) throws InterruptedException {if (type == 1) {return "hello " + name;}if (type == 2) {Thread.sleep(10000);return "time10: " + name;}throw new RuntimeException("failure");}}

2.导入feign和hystrix的依赖。

这里我们采用httpclient作为rpc,之所有采用httpclient,是因为其与okhttp性能相差不大。而且是Apache家的,feign默认支持的就是他。支持的更好一点。

<!--引入feign相关内容-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>3.0.1</version>
</dependency>
<!--引入配置项-->
<dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId><version>11.0</version>
</dependency>
<!--引入hystrix-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>2.2.7.RELEASE</version>
</dependency>

3.配置feign起效

  1. 在Application类加上@EnableFeignClients 注解
  2. 声明接口API代码如下:
package com.nmm.study.api;import com.nmm.study.api.fallback.ClientFactoryClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;/*** <p>Title: Client</p>* <p>description: 客户端</p>** @author niemingming 2021/5/14*/@FeignClient(name = "feigndemo", url = "http://localhost:7000", fallbackFactory = ClientFactoryClient.class)
public interface Client {@GetMapping("/v1/feign/test")String feignConn(@RequestParam(name = "type") int type, @RequestParam(name = "name") String name);
}

到这一步,如果不考虑熔断,feign的基本调用就可以了。我们直接在服务中调用就好了。

package com.nmm.study.controller;import com.nmm.study.api.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/*** <p>Title: FeingClientController</p>* <p>description: 测试类</p>** @author niemingming 2021/5/14*/
@RestController
public class FeingClientController {@Autowiredprivate Client client;@GetMapping("/v1/feignclient")public String testFeign(int type, String name) {return client.feignConn(type, name);}
}

3.feign连接池管理配置。

feign:httpclient:enabled: true max-connection: 200 max-connections-per-route: 50 connection-timeout: 2000  

参数说明: 具体生效逻辑可以参看FeignAutoConfiguration配置类。

  • enabled:这个参数默认是true,当我们引入了httpclient依赖自动就生效了。
  • max-connection:就是httpclient的配置,配置最大连接数
  • max-connections-per-route:每个路由的最大连接数
  • connection-timeout: httpclient的全局连接超时配置。

4.关于feign超时配置

我们配置了连接池,那么接下来就是超时配置了,我们都不希望因为别人拖垮自己。

feign:client:   #FeignClientPropertiesconfig:default:read-timeout: 20000connect-timeout: 20000feigndemo:  #feignnameread-timeout: 20000connect-timeout: 20000

参数说明:通过参看FeignAutoCOnfiguration类,我们知道具体配置项在FeignClientProperties上。
这里直接说明

  • config:配置项,以下,开头default标识默认配置,feigndemo:我们配置的feignclient的name,是针对该client的具体配置。
  • default:默认配置项
  • read-timeout:读取超时
  • connect-timeout: 连接超时
  • feigndemo:某一个client配置,这里是@FeignClient中的name指定值。

5.通常我们还需要处理异常,以及希望有熔断。启用熔断

feign:circuitbreaker:enabled: true  #升级后的熔断开启。

说明:在我使用的这一版本中,springcloud升级了,2020之后,熔断开启时feign.circuitbreaker.enabled参数不在是feign.hystrix.enabled

6.熔断处理,我们可以指定fallback,如果需要详细的异常信息请事先fallbackFactory

package com.nmm.study.api.fallback;import com.nmm.study.api.Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;/*** <p>Title: ClientFactoryClient</p>* <p>description: </p>** @author niemingming 2021/5/14*/@Slf4j
@Component
public class ClientFactoryClient implements FallbackFactory<Client> {@Autowiredprivate ClientFallback clientFallback;@Overridepublic Client create(Throwable cause) {log.error("获取到的异常,不是原始异常!", cause);//需要将熔断对象返回。return clientFallback;}
}

ClientFallback就是实现了Client的fallback类。

7.熔断的超时怎么办?

hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 20000

8.如果要单独配置某个client的超时怎么办?

这个还是通过FeignAutoConfiguration类,在CircuitBreakerFactory中找到。直接说,我使用的版本中。commandKey规则是feignclientname_methodName;以我的例子说明:

hystrix:command:feigndemo_feignConn: #参考FeignAutoConfiguration里面关于熔断的配置项CircuitBreakerFactory;规则为feignname_methodnameexecution:isolation:strategy: THREAD #隔离方式,默认的线程隔离thread:timeoutInMilliseconds: 20000

9.两个超时怎么生效,实际测试是都生效了。

10.补充一个WebFilter配置问题.

package com.nmm.study;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;/*** <p>Title: TestFilter</p>* <p>description: 连接器不生效问题</p>** @author niemingming 2021/5/14*/@WebFilter(filterName = "testFilter", urlPatterns = "/v1/*")
public class TestFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {}
}
  • 一般来说,当我们实现了Filter,并将bean交给spring管理时,也就是只写一个@Component注解,过滤器就会生效,只不过这时是对所有请求生效。
  • urlPartterns生效问题。首先,去掉@Component,改为@WebFilter注解。
  • 再启动类增加@ServletComponentScan注解。
  • 如果还不生效,请仔细检查urlPartterns是否正确,一个*,不是两个**,两个是无效的。我就是这个问题。

11.版本说明,分析问题时请注意版本问题。

  • springboot:2.4.4
  • springcloudopenfeign: 3.0.1
  • springcloudhystrix: 2.2.7.RELEASE
  1. 一个小问题,开启Hystrix后,如果在项目启动时,调用feign,总是会报超时。初步确定是启动时,hystrix是懒加载,很多配置项没有生效,导致服务一直报超时。解决办法,在项目启动后执行,即实现ApplicationRunner接口即可。

feign+hystrix相关超时时间配置问题相关推荐

  1. hystrix设置超时时间

    feign:file_api:hystrix:enabled: true hystrix:command:default: #也可以针对多个服务execution:timeout:enabled: t ...

  2. 单独某个设置feign接口的超时时间

    1.在配置文件里配置:hystrix:enabled: true feign:httpclient.enable: falseokhttp.enable: true# 开启熔断hystrix:enab ...

  3. SpringCloud Hystrix的超时时间配置以及监控平台搭建

    一.Hystrix接口调用超时时间配置 在application.yml 中设置 hystrix.command.default.execution.isolation.thread.timeoutI ...

  4. Hystrix默认超时时间

  5. Zuul、Ribbon、Feign、Hystrix使用时的超时时间(timeout)设置问题

    本文转载自:https://priesttomb.github.io/,谢谢作者的分享 文章目录 写在前面 注意! 工具 Feign + Hystrix 0. 默认基本配置 1. 不同实例分别配置 R ...

  6. Feign Client的各种超时时间设置

    在Spring Cloud微服务架构中,大部分公司都是利用Open Feign进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果是业务比较复杂,服务要进行比较繁杂的业务计算,那 ...

  7. ribbon基于接口配置超时_feign的ribbon超时配置和hystrix的超时配置说明

    先看下我的配置: ribbon: MaxAutoRetries: 1 #最大重试次数,当Eureka中可以找到服务,但是服务连不上时将会重试 MaxAutoRetriesNextServer: 1 # ...

  8. 新功能:阿里云负载均衡SLB支持HTTP/HTTPS超时时间自定义功能

    2019独角兽企业重金招聘Python工程师标准>>> 摘要: 大家好,很高兴的告诉大家,阿里云负载均衡SLB已经在新加坡.澳大利亚(悉尼).马来西亚(吉隆坡).日本(东京).美国( ...

  9. SpringCloud的各种超时时间配置效果

    1. 前言(以下的springcloud版本是Dalston.RC1) Springcloud框架中,超时时间的设置通常有三个层面: 1) zuul网关 用指定 url 进行路由时,使用下面的方式 # ...

最新文章

  1. 语音网关上AA的配置解析
  2. SwiftUI 3.0调用SDWebImageSwiftUI 第三方框架
  3. linix防火墙设置之顺序设置问题 -- 解决防火墙规则顺序和插入规则到指定序号的问题...
  4. 职高学计算机走单招是,职高学生不用愁了,还有机会上本科,走“单招”或是最佳途径!...
  5. 为什么说RESTful对程序员如此重要
  6. ADOCE打包到WinCE NK.bin镜像中
  7. 本周三、四两场直播丨达梦 vs. Oracle,从快速入门到性能分析优化
  8. AI论文解读:基于Transformer的多目标跟踪方法TrackFormer
  9. java web免费案例_java web 典型项目开发案例
  10. 【C语言函数调用详解】——传值调用传址调用
  11. python playsound 音量_Win32 PlaySound:如何控制音量?
  12. ireport 使用list数据源
  13. Android 之自定义表情
  14. 如何把uniapp的vue小程序项目跑起来
  15. [翻译Joel On Software]Joel测试:12步写出更高质量代码/The Joel Test: 12 Steps to Better Code
  16. office 2016 for Mac打开时 出现隐藏模块中出现编译错误: link
  17. 为什么先序/中序线索二叉树不需要栈的支持,而后序线索二叉树需要栈的支持?
  18. 世界国家中英文对照表(json格式)
  19. projects from git 和 projects from git(with smart import)区别
  20. /usr/include/linux/if_ppp.h:117: error: expected specifier-qualifier-list before 'aligned_u64' 解决方式

热门文章

  1. 攻克弹唱第七课(如何弹奏neon)
  2. 密西西比河谷州立大学:Android应用程序开发(一)
  3. 微信公众平台账号迁移公证书如何办理?GDP30强城市收据全新出炉
  4. k8s创建service
  5. 每一天的邂逅......
  6. 汇编语言0x2c,ARM汇编语言(4) 指令、伪操作、伪指令学习
  7. 工作20天之感悟(嵌入式开发)
  8. 矩阵中的entries是什么
  9. java陆小凤的游戏_陆小凤之金鹏王朝游戏
  10. 刷脸支付互联网巨头纷纷从线上走到线下