Feign性能优化注意事项--超时
Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 1.
at feign.Util.checkState(Util.java:128) ~[feign-core-9.5.0.jar:na]
at org.springframework.cloud.netflix.feign.annotation.PathVariableParameterProcessor.processArgument(PathVariableParameterProcessor.java:51) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.netflix.feign.support.SpringMvcContract.processAnnotationsOnParameter(SpringMvcContract.java:238) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:107) ~[feign-core-9.5.0.jar:na]
at org.springframework.cloud.netflix.feign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64) ~[feign-core-9.5.0.jar:na]
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146) ~[feign-core-9.5.0.jar:na]
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53) ~[feign-core-9.5.0.jar:na]
at feign.Feign$Builder.target(Feign.java:218) ~[feign-core-9.5.0.jar:na]
at org.springframework.cloud.netflix.feign.HystrixTargeter.target(HystrixTargeter.java:39) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:211) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:232) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:178) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
... 68 common frames omitted
使用Feign的时候,如果参数中带有
@PathVariable形式的参数,则要用value=""标明对应的参数,否则会抛出IllegalStateException异常
如
@PutMapping("/disuseable/{sn}")
ApiResponse disUseAble(@PathVariable String sn); // wrong
-->
@PutMapping("/disuseable/{sn}")
ApiResponse disUseAble(@PathVariable(value="sn") String sn); // right
-->
@PutMapping("/disuseable/{sn}")
ApiResponse disUseAble(@PathVariable("sn") String sn); // right
一、FeignClient注解
FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上
@FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class)
public interface GitHubClient {
@RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
String searchRepo(@RequestParam("q") String queryStr);
}
声明接口之后,在代码中通过@Resource注入之后即可使用。@FeignClient标签的常用属性如下:
- name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
- url: url一般用于调试,可以手动指定@FeignClient调用的地址
- decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
- configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
- fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
- fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
path: 定义当前FeignClient的统一前缀
@FeignClient(name = "github-client",
url = "https://api.github.com",
configuration = GitHubExampleConfig.class,
fallback = GitHubClient.DefaultFallback.class)
public interface GitHubClient {
@RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
String searchRepo(@RequestParam("q") String queryStr); /**
* 容错处理类,当调用失败时,简单返回空字符串
*/
@Component
public class DefaultFallback implements GitHubClient {
@Override
public String searchRepo(@RequestParam("q") String queryStr) {
return "";
}
}
}
在使用fallback属性时,需要使用@Component注解,保证fallback类被Spring容器扫描到,GitHubExampleConfig内容如下:
@Configuration
public class GitHubExampleConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
在使用FeignClient时,Spring会按name创建不同的ApplicationContext,通过不同的Context来隔离FeignClient的配置信息,在使用配置类时,不能把配置类放到Spring App Component scan的路径下,否则,配置类会对所有FeignClient生效.
@RestController
@RequestMapping("/v1/card")
public class IndexApi { @PostMapping("balance")
@ResponseBody
public Info index() {
Info.Builder builder = new Info.Builder();
builder.withDetail("x", 2);
builder.withDetail("y", 2);
return builder.build();
}
}
Feign Client
@FeignClient(
name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class
)
@RequestMapping(value = "/v1/card")
public interface CardFeignClient { @RequestMapping(value = "/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Info info(); }
if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :
如果 @RequestMapping注解被用在FeignClient类上,当像如下代码请求/v1/card/balance时,注意有Accept header:
Content-Type:application/json
Accept:application/json POST http://localhost:7913/v1/card/balance
那么会返回 404。
如果不包含Accept header时请求,则是OK:
Content-Type:application/json
POST http://localhost:7913/v1/card/balance
或者像下面不在Feign Client上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:
@FeignClient(
name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class
) public interface CardFeignClient { @RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Info info(); }
三、Feign请求超时问题
解决方案有三种,以feign为例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
该配置是让Hystrix的超时时间改为5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
该配置,用于禁用Hystrix的超时时间
方法三
feign.hystrix.enabled: false
该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。
Feign性能优化注意事项--超时的更多相关文章
- Feign性能优化注意事项
一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 @FeignClient(name ...
- Spring Cloud Feign 性能优化
#### 1.替换 tomcat 首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍 **第一步,pom 修改去除tomca ...
- SQL性能优化注意事项
1.选用适合的Oracle优化器 Oracle的优化器共有3种: a.RULE(基于规则) b.COST(基于成本) c.CHOOSE(选择性) 设置缺省的优化器,可以通过对init.ora文件中OP ...
- react 性能优化注意事项
工具: React 16 或更新版本 只需在url 后边加 ?react_perf 后 performance 一栏中会添加 User Timing devtool 分析 state.props ...
- PHP----------PHP自身的性能优化注意事项
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row[’id’] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用ech ...
- Apache性能优化、超时设置,linux 重启apache
在httpd.conf中去掉Include conf/extra/httpd-default.conf前的#以使httpd-default.php生效.其中调节以下参数Timeout 15 (连接超时 ...
- CSS3与页面布局学习总结(八)——浏览器兼容与前端性能优化
一.浏览器兼容 1.1.概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题.不同浏览器其内核亦不尽相同,相同内核的版本不同,相同版本的内核浏览器品牌不一样,各种运 ...
- Android应用性能优化(转)
人类大脑与眼睛对一个画面的连贯性感知其实是有一个界限的,譬如我们看电影会觉得画面很自然连贯(帧率为24fps),用手机当然也需要感知屏幕操作的连贯性(尤其是动画过度),所以Android索性就把达到这 ...
- CSS3与页面布局学习笔记(八)——浏览器兼容性问题与前端性能优化方案
一.浏览器兼容 1.1.概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题.不同浏览器其内核亦不尽相同,相同内核的版本不同,相同版本的内核浏览器品牌不一样,各种运 ...
随机推荐
- [翻译]NUnit---Sequential and SetCulture and SetUICulture Attributes(十八)
Sequential特性用于在测试用例上指定NUnit通过为测试提供的参数选择单一值生产测试用例,并且不会生产额外的组合. Note:如果参数数据由多个特性提供,那么NUnit使用数据项的顺序是随机的 ...
- .net core grpc consul 实现服务注册 服务发现 负载均衡(二)
在上一篇 .net core grpc 实现通信(一) 中,我们实现的grpc通信在.net core中的可行性,但要在微服务中真正使用,还缺少 服务注册,服务发现及负载均衡等,本篇我们将在 .net ...
- 《ASP.NET MVC 5 破境之道》:第一境 ASP.Net MVC5项目初探 — 第一节:运行第一个MVC5项目
第一境 ASP.Net MVC5项目初探 — 第一节:运行第一个MVC5项目 创建一个MVC项目,是很容易的,大部分工作,VS都帮我们完成了.只需要按照如下步骤按部就班就可以了. 打开VS2017,选 ...
- Linq to SQL 练习
public class HomeController : Controller { // // GET: /Home/ empentity entity = new empentity(); pub ...
- 程序媛计划——mysql基本操作
本文适用于mac 在官网上下载community 版mysql,选择dmy这种.在终端中安装好mysql. #进入mysql /usr/local/mysql/bin/mysql -uroot -p ...
- 学习笔记_J2EE_SSM_01_spring+springMVC+Mybatis整合_XML配置示例
spring+springMVC+Mybatis整合_XML配置示例 1.概述 spring+springMVC+Mybatis整合 XML配置方式 1.1 测试环境说明 名称 版本 备注 操作系统 ...
- window主机和centos主机之间相互传送文件
命令实现linux和window文件传送 一:下载配置pscp软件从http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html下载p ...
- react-router-dom 手动控制路由跳转
基于 react-router 4.0 版本,我们想要通过 JS 手动控制路由跳转,分三步: 第一步:引入 propTypes const PropTypes = require('prop-type ...
- LoadLinked/StoreConditional (LL/SC)
MIPS中LL/SC指令介绍 MIPS32中的LL.SC指令说明 理解MIPS指令集中的ll (load linked) 和 sc 你用ll指令读取一个内存中的数据并存到一个寄存器,然后在寄存器修改( ...
- Sublime Text 3快捷键汇总
转自:http://blog.sina.com.cn/s/blog_73c5cfbe0101ldj8.html Sublime Text 3非常实用,但是想要用好,一些快捷键不可或缺,所以转了这个快捷 ...